string.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Copyright (C) 1991-2021 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. /*
  15. * ISO C99 Standard: 7.21 String handling <string.h>
  16. */
  17. #ifndef _STRING_H
  18. #define _STRING_H 1
  19. #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
  20. #include <bits/libc-header-start.h>
  21. __BEGIN_DECLS
  22. /* Get size_t and NULL from <stddef.h>. */
  23. #define __need_size_t
  24. #define __need_NULL
  25. #include <stddef.h>
  26. /* Tell the caller that we provide correct C++ prototypes. */
  27. #if defined __cplusplus && (__GNUC_PREREQ (4, 4) \
  28. || __glibc_clang_prereq (3, 5))
  29. # define __CORRECT_ISO_CPP_STRING_H_PROTO
  30. #endif
  31. /* Copy N bytes of SRC to DEST. */
  32. extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
  33. size_t __n) __THROW __nonnull ((1, 2));
  34. /* Copy N bytes of SRC to DEST, guaranteeing
  35. correct behavior for overlapping strings. */
  36. extern void *memmove (void *__dest, const void *__src, size_t __n)
  37. __THROW __nonnull ((1, 2));
  38. /* Copy no more than N bytes of SRC to DEST, stopping when C is found.
  39. Return the position in DEST one byte past where C was copied,
  40. or NULL if C was not found in the first N bytes of SRC. */
  41. #if defined __USE_MISC || defined __USE_XOPEN || __GLIBC_USE (ISOC2X)
  42. extern void *memccpy (void *__restrict __dest, const void *__restrict __src,
  43. int __c, size_t __n)
  44. __THROW __nonnull ((1, 2)) __attr_access ((__write_only__, 1, 4));
  45. #endif /* Misc || X/Open. */
  46. /* Set N bytes of S to C. */
  47. extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
  48. /* Compare N bytes of S1 and S2. */
  49. extern int memcmp (const void *__s1, const void *__s2, size_t __n)
  50. __THROW __attribute_pure__ __nonnull ((1, 2));
  51. /* Search N bytes of S for C. */
  52. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  53. extern "C++"
  54. {
  55. extern void *memchr (void *__s, int __c, size_t __n)
  56. __THROW __asm ("memchr") __attribute_pure__ __nonnull ((1));
  57. extern const void *memchr (const void *__s, int __c, size_t __n)
  58. __THROW __asm ("memchr") __attribute_pure__ __nonnull ((1));
  59. # ifdef __OPTIMIZE__
  60. __extern_always_inline void *
  61. memchr (void *__s, int __c, size_t __n) __THROW
  62. {
  63. return __builtin_memchr (__s, __c, __n);
  64. }
  65. __extern_always_inline const void *
  66. memchr (const void *__s, int __c, size_t __n) __THROW
  67. {
  68. return __builtin_memchr (__s, __c, __n);
  69. }
  70. # endif
  71. }
  72. #else
  73. extern void *memchr (const void *__s, int __c, size_t __n)
  74. __THROW __attribute_pure__ __nonnull ((1));
  75. #endif
  76. #ifdef __USE_GNU
  77. /* Search in S for C. This is similar to `memchr' but there is no
  78. length limit. */
  79. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  80. extern "C++" void *rawmemchr (void *__s, int __c)
  81. __THROW __asm ("rawmemchr") __attribute_pure__ __nonnull ((1));
  82. extern "C++" const void *rawmemchr (const void *__s, int __c)
  83. __THROW __asm ("rawmemchr") __attribute_pure__ __nonnull ((1));
  84. # else
  85. extern void *rawmemchr (const void *__s, int __c)
  86. __THROW __attribute_pure__ __nonnull ((1));
  87. # endif
  88. /* Search N bytes of S for the final occurrence of C. */
  89. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  90. extern "C++" void *memrchr (void *__s, int __c, size_t __n)
  91. __THROW __asm ("memrchr") __attribute_pure__ __nonnull ((1))
  92. __attr_access ((__read_only__, 1, 3));
  93. extern "C++" const void *memrchr (const void *__s, int __c, size_t __n)
  94. __THROW __asm ("memrchr") __attribute_pure__ __nonnull ((1))
  95. __attr_access ((__read_only__, 1, 3));
  96. # else
  97. extern void *memrchr (const void *__s, int __c, size_t __n)
  98. __THROW __attribute_pure__ __nonnull ((1))
  99. __attr_access ((__read_only__, 1, 3));
  100. # endif
  101. #endif
  102. /* Copy SRC to DEST. */
  103. extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
  104. __THROW __nonnull ((1, 2));
  105. /* Copy no more than N characters of SRC to DEST. */
  106. extern char *strncpy (char *__restrict __dest,
  107. const char *__restrict __src, size_t __n)
  108. __THROW __nonnull ((1, 2));
  109. /* Append SRC onto DEST. */
  110. extern char *strcat (char *__restrict __dest, const char *__restrict __src)
  111. __THROW __nonnull ((1, 2));
  112. /* Append no more than N characters from SRC onto DEST. */
  113. extern char *strncat (char *__restrict __dest, const char *__restrict __src,
  114. size_t __n) __THROW __nonnull ((1, 2));
  115. /* Compare S1 and S2. */
  116. extern int strcmp (const char *__s1, const char *__s2)
  117. __THROW __attribute_pure__ __nonnull ((1, 2));
  118. /* Compare N characters of S1 and S2. */
  119. extern int strncmp (const char *__s1, const char *__s2, size_t __n)
  120. __THROW __attribute_pure__ __nonnull ((1, 2));
  121. /* Compare the collated forms of S1 and S2. */
  122. extern int strcoll (const char *__s1, const char *__s2)
  123. __THROW __attribute_pure__ __nonnull ((1, 2));
  124. /* Put a transformation of SRC into no more than N bytes of DEST. */
  125. extern size_t strxfrm (char *__restrict __dest,
  126. const char *__restrict __src, size_t __n)
  127. __THROW __nonnull ((2)) __attr_access ((__write_only__, 1, 3));
  128. #ifdef __USE_XOPEN2K8
  129. /* POSIX.1-2008 extended locale interface (see locale.h). */
  130. # include <bits/types/locale_t.h>
  131. /* Compare the collated forms of S1 and S2, using sorting rules from L. */
  132. extern int strcoll_l (const char *__s1, const char *__s2, locale_t __l)
  133. __THROW __attribute_pure__ __nonnull ((1, 2, 3));
  134. /* Put a transformation of SRC into no more than N bytes of DEST,
  135. using sorting rules from L. */
  136. extern size_t strxfrm_l (char *__dest, const char *__src, size_t __n,
  137. locale_t __l) __THROW __nonnull ((2, 4))
  138. __attr_access ((__write_only__, 1, 3));
  139. #endif
  140. #if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8 \
  141. || __GLIBC_USE (LIB_EXT2) || __GLIBC_USE (ISOC2X))
  142. /* Duplicate S, returning an identical malloc'd string. */
  143. extern char *strdup (const char *__s)
  144. __THROW __attribute_malloc__ __nonnull ((1));
  145. #endif
  146. /* Return a malloc'd copy of at most N bytes of STRING. The
  147. resultant string is terminated even if no null terminator
  148. appears before STRING[N]. */
  149. #if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) || __GLIBC_USE (ISOC2X)
  150. extern char *strndup (const char *__string, size_t __n)
  151. __THROW __attribute_malloc__ __nonnull ((1));
  152. #endif
  153. #if defined __USE_GNU && defined __GNUC__
  154. /* Duplicate S, returning an identical alloca'd string. */
  155. # define strdupa(s) \
  156. (__extension__ \
  157. ({ \
  158. const char *__old = (s); \
  159. size_t __len = strlen (__old) + 1; \
  160. char *__new = (char *) __builtin_alloca (__len); \
  161. (char *) memcpy (__new, __old, __len); \
  162. }))
  163. /* Return an alloca'd copy of at most N bytes of string. */
  164. # define strndupa(s, n) \
  165. (__extension__ \
  166. ({ \
  167. const char *__old = (s); \
  168. size_t __len = strnlen (__old, (n)); \
  169. char *__new = (char *) __builtin_alloca (__len + 1); \
  170. __new[__len] = '\0'; \
  171. (char *) memcpy (__new, __old, __len); \
  172. }))
  173. #endif
  174. /* Find the first occurrence of C in S. */
  175. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  176. extern "C++"
  177. {
  178. extern char *strchr (char *__s, int __c)
  179. __THROW __asm ("strchr") __attribute_pure__ __nonnull ((1));
  180. extern const char *strchr (const char *__s, int __c)
  181. __THROW __asm ("strchr") __attribute_pure__ __nonnull ((1));
  182. # ifdef __OPTIMIZE__
  183. __extern_always_inline char *
  184. strchr (char *__s, int __c) __THROW
  185. {
  186. return __builtin_strchr (__s, __c);
  187. }
  188. __extern_always_inline const char *
  189. strchr (const char *__s, int __c) __THROW
  190. {
  191. return __builtin_strchr (__s, __c);
  192. }
  193. # endif
  194. }
  195. #else
  196. extern char *strchr (const char *__s, int __c)
  197. __THROW __attribute_pure__ __nonnull ((1));
  198. #endif
  199. /* Find the last occurrence of C in S. */
  200. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  201. extern "C++"
  202. {
  203. extern char *strrchr (char *__s, int __c)
  204. __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));
  205. extern const char *strrchr (const char *__s, int __c)
  206. __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));
  207. # ifdef __OPTIMIZE__
  208. __extern_always_inline char *
  209. strrchr (char *__s, int __c) __THROW
  210. {
  211. return __builtin_strrchr (__s, __c);
  212. }
  213. __extern_always_inline const char *
  214. strrchr (const char *__s, int __c) __THROW
  215. {
  216. return __builtin_strrchr (__s, __c);
  217. }
  218. # endif
  219. }
  220. #else
  221. extern char *strrchr (const char *__s, int __c)
  222. __THROW __attribute_pure__ __nonnull ((1));
  223. #endif
  224. #ifdef __USE_GNU
  225. /* This function is similar to `strchr'. But it returns a pointer to
  226. the closing NUL byte in case C is not found in S. */
  227. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  228. extern "C++" char *strchrnul (char *__s, int __c)
  229. __THROW __asm ("strchrnul") __attribute_pure__ __nonnull ((1));
  230. extern "C++" const char *strchrnul (const char *__s, int __c)
  231. __THROW __asm ("strchrnul") __attribute_pure__ __nonnull ((1));
  232. # else
  233. extern char *strchrnul (const char *__s, int __c)
  234. __THROW __attribute_pure__ __nonnull ((1));
  235. # endif
  236. #endif
  237. /* Return the length of the initial segment of S which
  238. consists entirely of characters not in REJECT. */
  239. extern size_t strcspn (const char *__s, const char *__reject)
  240. __THROW __attribute_pure__ __nonnull ((1, 2));
  241. /* Return the length of the initial segment of S which
  242. consists entirely of characters in ACCEPT. */
  243. extern size_t strspn (const char *__s, const char *__accept)
  244. __THROW __attribute_pure__ __nonnull ((1, 2));
  245. /* Find the first occurrence in S of any character in ACCEPT. */
  246. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  247. extern "C++"
  248. {
  249. extern char *strpbrk (char *__s, const char *__accept)
  250. __THROW __asm ("strpbrk") __attribute_pure__ __nonnull ((1, 2));
  251. extern const char *strpbrk (const char *__s, const char *__accept)
  252. __THROW __asm ("strpbrk") __attribute_pure__ __nonnull ((1, 2));
  253. # ifdef __OPTIMIZE__
  254. __extern_always_inline char *
  255. strpbrk (char *__s, const char *__accept) __THROW
  256. {
  257. return __builtin_strpbrk (__s, __accept);
  258. }
  259. __extern_always_inline const char *
  260. strpbrk (const char *__s, const char *__accept) __THROW
  261. {
  262. return __builtin_strpbrk (__s, __accept);
  263. }
  264. # endif
  265. }
  266. #else
  267. extern char *strpbrk (const char *__s, const char *__accept)
  268. __THROW __attribute_pure__ __nonnull ((1, 2));
  269. #endif
  270. /* Find the first occurrence of NEEDLE in HAYSTACK. */
  271. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  272. extern "C++"
  273. {
  274. extern char *strstr (char *__haystack, const char *__needle)
  275. __THROW __asm ("strstr") __attribute_pure__ __nonnull ((1, 2));
  276. extern const char *strstr (const char *__haystack, const char *__needle)
  277. __THROW __asm ("strstr") __attribute_pure__ __nonnull ((1, 2));
  278. # ifdef __OPTIMIZE__
  279. __extern_always_inline char *
  280. strstr (char *__haystack, const char *__needle) __THROW
  281. {
  282. return __builtin_strstr (__haystack, __needle);
  283. }
  284. __extern_always_inline const char *
  285. strstr (const char *__haystack, const char *__needle) __THROW
  286. {
  287. return __builtin_strstr (__haystack, __needle);
  288. }
  289. # endif
  290. }
  291. #else
  292. extern char *strstr (const char *__haystack, const char *__needle)
  293. __THROW __attribute_pure__ __nonnull ((1, 2));
  294. #endif
  295. /* Divide S into tokens separated by characters in DELIM. */
  296. extern char *strtok (char *__restrict __s, const char *__restrict __delim)
  297. __THROW __nonnull ((2));
  298. /* Divide S into tokens separated by characters in DELIM. Information
  299. passed between calls are stored in SAVE_PTR. */
  300. extern char *__strtok_r (char *__restrict __s,
  301. const char *__restrict __delim,
  302. char **__restrict __save_ptr)
  303. __THROW __nonnull ((2, 3));
  304. #ifdef __USE_POSIX
  305. extern char *strtok_r (char *__restrict __s, const char *__restrict __delim,
  306. char **__restrict __save_ptr)
  307. __THROW __nonnull ((2, 3));
  308. #endif
  309. #ifdef __USE_GNU
  310. /* Similar to `strstr' but this function ignores the case of both strings. */
  311. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  312. extern "C++" char *strcasestr (char *__haystack, const char *__needle)
  313. __THROW __asm ("strcasestr") __attribute_pure__ __nonnull ((1, 2));
  314. extern "C++" const char *strcasestr (const char *__haystack,
  315. const char *__needle)
  316. __THROW __asm ("strcasestr") __attribute_pure__ __nonnull ((1, 2));
  317. # else
  318. extern char *strcasestr (const char *__haystack, const char *__needle)
  319. __THROW __attribute_pure__ __nonnull ((1, 2));
  320. # endif
  321. #endif
  322. #ifdef __USE_GNU
  323. /* Find the first occurrence of NEEDLE in HAYSTACK.
  324. NEEDLE is NEEDLELEN bytes long;
  325. HAYSTACK is HAYSTACKLEN bytes long. */
  326. extern void *memmem (const void *__haystack, size_t __haystacklen,
  327. const void *__needle, size_t __needlelen)
  328. __THROW __attribute_pure__ __nonnull ((1, 3))
  329. __attr_access ((__read_only__, 1, 2))
  330. __attr_access ((__read_only__, 3, 4));
  331. /* Copy N bytes of SRC to DEST, return pointer to bytes after the
  332. last written byte. */
  333. extern void *__mempcpy (void *__restrict __dest,
  334. const void *__restrict __src, size_t __n)
  335. __THROW __nonnull ((1, 2));
  336. extern void *mempcpy (void *__restrict __dest,
  337. const void *__restrict __src, size_t __n)
  338. __THROW __nonnull ((1, 2));
  339. #endif
  340. /* Return the length of S. */
  341. extern size_t strlen (const char *__s)
  342. __THROW __attribute_pure__ __nonnull ((1));
  343. #ifdef __USE_XOPEN2K8
  344. /* Find the length of STRING, but scan at most MAXLEN characters.
  345. If no '\0' terminator is found in that many characters, return MAXLEN. */
  346. extern size_t strnlen (const char *__string, size_t __maxlen)
  347. __THROW __attribute_pure__ __nonnull ((1));
  348. #endif
  349. /* Return a string describing the meaning of the `errno' code in ERRNUM. */
  350. extern char *strerror (int __errnum) __THROW;
  351. #ifdef __USE_XOPEN2K
  352. /* Reentrant version of `strerror'.
  353. There are 2 flavors of `strerror_r', GNU which returns the string
  354. and may or may not use the supplied temporary buffer and POSIX one
  355. which fills the string into the buffer.
  356. To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
  357. without -D_GNU_SOURCE is needed, otherwise the GNU version is
  358. preferred. */
  359. # if defined __USE_XOPEN2K && !defined __USE_GNU
  360. /* Fill BUF with a string describing the meaning of the `errno' code in
  361. ERRNUM. */
  362. # ifdef __REDIRECT_NTH
  363. extern int __REDIRECT_NTH (strerror_r,
  364. (int __errnum, char *__buf, size_t __buflen),
  365. __xpg_strerror_r) __nonnull ((2))
  366. __attr_access ((__write_only__, 2, 3));
  367. # else
  368. extern int __xpg_strerror_r (int __errnum, char *__buf, size_t __buflen)
  369. __THROW __nonnull ((2)) __attr_access ((__write_only__, 2, 3));
  370. # define strerror_r __xpg_strerror_r
  371. # endif
  372. # else
  373. /* If a temporary buffer is required, at most BUFLEN bytes of BUF will be
  374. used. */
  375. extern char *strerror_r (int __errnum, char *__buf, size_t __buflen)
  376. __THROW __nonnull ((2)) __wur __attr_access ((__write_only__, 2, 3));
  377. # endif
  378. # ifdef __USE_GNU
  379. /* Return a string describing the meaning of tthe error in ERR. */
  380. extern const char *strerrordesc_np (int __err) __THROW;
  381. /* Return a string with the error name in ERR. */
  382. extern const char *strerrorname_np (int __err) __THROW;
  383. # endif
  384. #endif
  385. #ifdef __USE_XOPEN2K8
  386. /* Translate error number to string according to the locale L. */
  387. extern char *strerror_l (int __errnum, locale_t __l) __THROW;
  388. #endif
  389. #ifdef __USE_MISC
  390. # include <strings.h>
  391. /* Set N bytes of S to 0. The compiler will not delete a call to this
  392. function, even if S is dead after the call. */
  393. extern void explicit_bzero (void *__s, size_t __n) __THROW __nonnull ((1))
  394. __attr_access ((__write_only__, 1, 2));
  395. /* Return the next DELIM-delimited token from *STRINGP,
  396. terminating it with a '\0', and update *STRINGP to point past it. */
  397. extern char *strsep (char **__restrict __stringp,
  398. const char *__restrict __delim)
  399. __THROW __nonnull ((1, 2));
  400. #endif
  401. #ifdef __USE_XOPEN2K8
  402. /* Return a string describing the meaning of the signal number in SIG. */
  403. extern char *strsignal (int __sig) __THROW;
  404. # ifdef __USE_GNU
  405. /* Return an abbreviation string for the signal number SIG. */
  406. extern const char *sigabbrev_np (int __sig) __THROW;
  407. /* Return a string describing the meaning of the signal number in SIG,
  408. the result is not translated. */
  409. extern const char *sigdescr_np (int __sig) __THROW;
  410. # endif
  411. /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
  412. extern char *__stpcpy (char *__restrict __dest, const char *__restrict __src)
  413. __THROW __nonnull ((1, 2));
  414. extern char *stpcpy (char *__restrict __dest, const char *__restrict __src)
  415. __THROW __nonnull ((1, 2));
  416. /* Copy no more than N characters of SRC to DEST, returning the address of
  417. the last character written into DEST. */
  418. extern char *__stpncpy (char *__restrict __dest,
  419. const char *__restrict __src, size_t __n)
  420. __THROW __nonnull ((1, 2));
  421. extern char *stpncpy (char *__restrict __dest,
  422. const char *__restrict __src, size_t __n)
  423. __THROW __nonnull ((1, 2));
  424. #endif
  425. #ifdef __USE_GNU
  426. /* Compare S1 and S2 as strings holding name & indices/version numbers. */
  427. extern int strverscmp (const char *__s1, const char *__s2)
  428. __THROW __attribute_pure__ __nonnull ((1, 2));
  429. /* Sautee STRING briskly. */
  430. extern char *strfry (char *__string) __THROW __nonnull ((1));
  431. /* Frobnicate N bytes of S. */
  432. extern void *memfrob (void *__s, size_t __n) __THROW __nonnull ((1))
  433. __attr_access ((__write_only__, 1, 2));
  434. # ifndef basename
  435. /* Return the file name within directory of FILENAME. We don't
  436. declare the function if the `basename' macro is available (defined
  437. in <libgen.h>) which makes the XPG version of this function
  438. available. */
  439. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  440. extern "C++" char *basename (char *__filename)
  441. __THROW __asm ("basename") __nonnull ((1));
  442. extern "C++" const char *basename (const char *__filename)
  443. __THROW __asm ("basename") __nonnull ((1));
  444. # else
  445. extern char *basename (const char *__filename) __THROW __nonnull ((1));
  446. # endif
  447. # endif
  448. #endif
  449. #if __GNUC_PREREQ (3,4)
  450. # if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
  451. /* Functions with security checks. */
  452. # include <bits/string_fortified.h>
  453. # endif
  454. #endif
  455. __END_DECLS
  456. #endif /* string.h */