string.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* Copyright (C) 1991-2020 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));
  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. extern "C++" const void *memrchr (const void *__s, int __c, size_t __n)
  93. __THROW __asm ("memrchr") __attribute_pure__ __nonnull ((1));
  94. # else
  95. extern void *memrchr (const void *__s, int __c, size_t __n)
  96. __THROW __attribute_pure__ __nonnull ((1));
  97. # endif
  98. #endif
  99. /* Copy SRC to DEST. */
  100. extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
  101. __THROW __nonnull ((1, 2));
  102. /* Copy no more than N characters of SRC to DEST. */
  103. extern char *strncpy (char *__restrict __dest,
  104. const char *__restrict __src, size_t __n)
  105. __THROW __nonnull ((1, 2));
  106. /* Append SRC onto DEST. */
  107. extern char *strcat (char *__restrict __dest, const char *__restrict __src)
  108. __THROW __nonnull ((1, 2));
  109. /* Append no more than N characters from SRC onto DEST. */
  110. extern char *strncat (char *__restrict __dest, const char *__restrict __src,
  111. size_t __n) __THROW __nonnull ((1, 2));
  112. /* Compare S1 and S2. */
  113. extern int strcmp (const char *__s1, const char *__s2)
  114. __THROW __attribute_pure__ __nonnull ((1, 2));
  115. /* Compare N characters of S1 and S2. */
  116. extern int strncmp (const char *__s1, const char *__s2, size_t __n)
  117. __THROW __attribute_pure__ __nonnull ((1, 2));
  118. /* Compare the collated forms of S1 and S2. */
  119. extern int strcoll (const char *__s1, const char *__s2)
  120. __THROW __attribute_pure__ __nonnull ((1, 2));
  121. /* Put a transformation of SRC into no more than N bytes of DEST. */
  122. extern size_t strxfrm (char *__restrict __dest,
  123. const char *__restrict __src, size_t __n)
  124. __THROW __nonnull ((2));
  125. #ifdef __USE_XOPEN2K8
  126. /* POSIX.1-2008 extended locale interface (see locale.h). */
  127. # include <bits/types/locale_t.h>
  128. /* Compare the collated forms of S1 and S2, using sorting rules from L. */
  129. extern int strcoll_l (const char *__s1, const char *__s2, locale_t __l)
  130. __THROW __attribute_pure__ __nonnull ((1, 2, 3));
  131. /* Put a transformation of SRC into no more than N bytes of DEST,
  132. using sorting rules from L. */
  133. extern size_t strxfrm_l (char *__dest, const char *__src, size_t __n,
  134. locale_t __l) __THROW __nonnull ((2, 4));
  135. #endif
  136. #if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8 \
  137. || __GLIBC_USE (LIB_EXT2) || __GLIBC_USE (ISOC2X))
  138. /* Duplicate S, returning an identical malloc'd string. */
  139. extern char *strdup (const char *__s)
  140. __THROW __attribute_malloc__ __nonnull ((1));
  141. #endif
  142. /* Return a malloc'd copy of at most N bytes of STRING. The
  143. resultant string is terminated even if no null terminator
  144. appears before STRING[N]. */
  145. #if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) || __GLIBC_USE (ISOC2X)
  146. extern char *strndup (const char *__string, size_t __n)
  147. __THROW __attribute_malloc__ __nonnull ((1));
  148. #endif
  149. #if defined __USE_GNU && defined __GNUC__
  150. /* Duplicate S, returning an identical alloca'd string. */
  151. # define strdupa(s) \
  152. (__extension__ \
  153. ({ \
  154. const char *__old = (s); \
  155. size_t __len = strlen (__old) + 1; \
  156. char *__new = (char *) __builtin_alloca (__len); \
  157. (char *) memcpy (__new, __old, __len); \
  158. }))
  159. /* Return an alloca'd copy of at most N bytes of string. */
  160. # define strndupa(s, n) \
  161. (__extension__ \
  162. ({ \
  163. const char *__old = (s); \
  164. size_t __len = strnlen (__old, (n)); \
  165. char *__new = (char *) __builtin_alloca (__len + 1); \
  166. __new[__len] = '\0'; \
  167. (char *) memcpy (__new, __old, __len); \
  168. }))
  169. #endif
  170. /* Find the first occurrence of C in S. */
  171. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  172. extern "C++"
  173. {
  174. extern char *strchr (char *__s, int __c)
  175. __THROW __asm ("strchr") __attribute_pure__ __nonnull ((1));
  176. extern const char *strchr (const char *__s, int __c)
  177. __THROW __asm ("strchr") __attribute_pure__ __nonnull ((1));
  178. # ifdef __OPTIMIZE__
  179. __extern_always_inline char *
  180. strchr (char *__s, int __c) __THROW
  181. {
  182. return __builtin_strchr (__s, __c);
  183. }
  184. __extern_always_inline const char *
  185. strchr (const char *__s, int __c) __THROW
  186. {
  187. return __builtin_strchr (__s, __c);
  188. }
  189. # endif
  190. }
  191. #else
  192. extern char *strchr (const char *__s, int __c)
  193. __THROW __attribute_pure__ __nonnull ((1));
  194. #endif
  195. /* Find the last occurrence of C in S. */
  196. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  197. extern "C++"
  198. {
  199. extern char *strrchr (char *__s, int __c)
  200. __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));
  201. extern const char *strrchr (const char *__s, int __c)
  202. __THROW __asm ("strrchr") __attribute_pure__ __nonnull ((1));
  203. # ifdef __OPTIMIZE__
  204. __extern_always_inline char *
  205. strrchr (char *__s, int __c) __THROW
  206. {
  207. return __builtin_strrchr (__s, __c);
  208. }
  209. __extern_always_inline const char *
  210. strrchr (const char *__s, int __c) __THROW
  211. {
  212. return __builtin_strrchr (__s, __c);
  213. }
  214. # endif
  215. }
  216. #else
  217. extern char *strrchr (const char *__s, int __c)
  218. __THROW __attribute_pure__ __nonnull ((1));
  219. #endif
  220. #ifdef __USE_GNU
  221. /* This function is similar to `strchr'. But it returns a pointer to
  222. the closing NUL byte in case C is not found in S. */
  223. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  224. extern "C++" char *strchrnul (char *__s, int __c)
  225. __THROW __asm ("strchrnul") __attribute_pure__ __nonnull ((1));
  226. extern "C++" const char *strchrnul (const char *__s, int __c)
  227. __THROW __asm ("strchrnul") __attribute_pure__ __nonnull ((1));
  228. # else
  229. extern char *strchrnul (const char *__s, int __c)
  230. __THROW __attribute_pure__ __nonnull ((1));
  231. # endif
  232. #endif
  233. /* Return the length of the initial segment of S which
  234. consists entirely of characters not in REJECT. */
  235. extern size_t strcspn (const char *__s, const char *__reject)
  236. __THROW __attribute_pure__ __nonnull ((1, 2));
  237. /* Return the length of the initial segment of S which
  238. consists entirely of characters in ACCEPT. */
  239. extern size_t strspn (const char *__s, const char *__accept)
  240. __THROW __attribute_pure__ __nonnull ((1, 2));
  241. /* Find the first occurrence in S of any character in ACCEPT. */
  242. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  243. extern "C++"
  244. {
  245. extern char *strpbrk (char *__s, const char *__accept)
  246. __THROW __asm ("strpbrk") __attribute_pure__ __nonnull ((1, 2));
  247. extern const char *strpbrk (const char *__s, const char *__accept)
  248. __THROW __asm ("strpbrk") __attribute_pure__ __nonnull ((1, 2));
  249. # ifdef __OPTIMIZE__
  250. __extern_always_inline char *
  251. strpbrk (char *__s, const char *__accept) __THROW
  252. {
  253. return __builtin_strpbrk (__s, __accept);
  254. }
  255. __extern_always_inline const char *
  256. strpbrk (const char *__s, const char *__accept) __THROW
  257. {
  258. return __builtin_strpbrk (__s, __accept);
  259. }
  260. # endif
  261. }
  262. #else
  263. extern char *strpbrk (const char *__s, const char *__accept)
  264. __THROW __attribute_pure__ __nonnull ((1, 2));
  265. #endif
  266. /* Find the first occurrence of NEEDLE in HAYSTACK. */
  267. #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  268. extern "C++"
  269. {
  270. extern char *strstr (char *__haystack, const char *__needle)
  271. __THROW __asm ("strstr") __attribute_pure__ __nonnull ((1, 2));
  272. extern const char *strstr (const char *__haystack, const char *__needle)
  273. __THROW __asm ("strstr") __attribute_pure__ __nonnull ((1, 2));
  274. # ifdef __OPTIMIZE__
  275. __extern_always_inline char *
  276. strstr (char *__haystack, const char *__needle) __THROW
  277. {
  278. return __builtin_strstr (__haystack, __needle);
  279. }
  280. __extern_always_inline const char *
  281. strstr (const char *__haystack, const char *__needle) __THROW
  282. {
  283. return __builtin_strstr (__haystack, __needle);
  284. }
  285. # endif
  286. }
  287. #else
  288. extern char *strstr (const char *__haystack, const char *__needle)
  289. __THROW __attribute_pure__ __nonnull ((1, 2));
  290. #endif
  291. /* Divide S into tokens separated by characters in DELIM. */
  292. extern char *strtok (char *__restrict __s, const char *__restrict __delim)
  293. __THROW __nonnull ((2));
  294. /* Divide S into tokens separated by characters in DELIM. Information
  295. passed between calls are stored in SAVE_PTR. */
  296. extern char *__strtok_r (char *__restrict __s,
  297. const char *__restrict __delim,
  298. char **__restrict __save_ptr)
  299. __THROW __nonnull ((2, 3));
  300. #ifdef __USE_POSIX
  301. extern char *strtok_r (char *__restrict __s, const char *__restrict __delim,
  302. char **__restrict __save_ptr)
  303. __THROW __nonnull ((2, 3));
  304. #endif
  305. #ifdef __USE_GNU
  306. /* Similar to `strstr' but this function ignores the case of both strings. */
  307. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  308. extern "C++" char *strcasestr (char *__haystack, const char *__needle)
  309. __THROW __asm ("strcasestr") __attribute_pure__ __nonnull ((1, 2));
  310. extern "C++" const char *strcasestr (const char *__haystack,
  311. const char *__needle)
  312. __THROW __asm ("strcasestr") __attribute_pure__ __nonnull ((1, 2));
  313. # else
  314. extern char *strcasestr (const char *__haystack, const char *__needle)
  315. __THROW __attribute_pure__ __nonnull ((1, 2));
  316. # endif
  317. #endif
  318. #ifdef __USE_GNU
  319. /* Find the first occurrence of NEEDLE in HAYSTACK.
  320. NEEDLE is NEEDLELEN bytes long;
  321. HAYSTACK is HAYSTACKLEN bytes long. */
  322. extern void *memmem (const void *__haystack, size_t __haystacklen,
  323. const void *__needle, size_t __needlelen)
  324. __THROW __attribute_pure__ __nonnull ((1, 3));
  325. /* Copy N bytes of SRC to DEST, return pointer to bytes after the
  326. last written byte. */
  327. extern void *__mempcpy (void *__restrict __dest,
  328. const void *__restrict __src, size_t __n)
  329. __THROW __nonnull ((1, 2));
  330. extern void *mempcpy (void *__restrict __dest,
  331. const void *__restrict __src, size_t __n)
  332. __THROW __nonnull ((1, 2));
  333. #endif
  334. /* Return the length of S. */
  335. extern size_t strlen (const char *__s)
  336. __THROW __attribute_pure__ __nonnull ((1));
  337. #ifdef __USE_XOPEN2K8
  338. /* Find the length of STRING, but scan at most MAXLEN characters.
  339. If no '\0' terminator is found in that many characters, return MAXLEN. */
  340. extern size_t strnlen (const char *__string, size_t __maxlen)
  341. __THROW __attribute_pure__ __nonnull ((1));
  342. #endif
  343. /* Return a string describing the meaning of the `errno' code in ERRNUM. */
  344. extern char *strerror (int __errnum) __THROW;
  345. #ifdef __USE_XOPEN2K
  346. /* Reentrant version of `strerror'.
  347. There are 2 flavors of `strerror_r', GNU which returns the string
  348. and may or may not use the supplied temporary buffer and POSIX one
  349. which fills the string into the buffer.
  350. To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
  351. without -D_GNU_SOURCE is needed, otherwise the GNU version is
  352. preferred. */
  353. # if defined __USE_XOPEN2K && !defined __USE_GNU
  354. /* Fill BUF with a string describing the meaning of the `errno' code in
  355. ERRNUM. */
  356. # ifdef __REDIRECT_NTH
  357. extern int __REDIRECT_NTH (strerror_r,
  358. (int __errnum, char *__buf, size_t __buflen),
  359. __xpg_strerror_r) __nonnull ((2));
  360. # else
  361. extern int __xpg_strerror_r (int __errnum, char *__buf, size_t __buflen)
  362. __THROW __nonnull ((2));
  363. # define strerror_r __xpg_strerror_r
  364. # endif
  365. # else
  366. /* If a temporary buffer is required, at most BUFLEN bytes of BUF will be
  367. used. */
  368. extern char *strerror_r (int __errnum, char *__buf, size_t __buflen)
  369. __THROW __nonnull ((2)) __wur;
  370. # endif
  371. #endif
  372. #ifdef __USE_XOPEN2K8
  373. /* Translate error number to string according to the locale L. */
  374. extern char *strerror_l (int __errnum, locale_t __l) __THROW;
  375. #endif
  376. #ifdef __USE_MISC
  377. # include <strings.h>
  378. /* Set N bytes of S to 0. The compiler will not delete a call to this
  379. function, even if S is dead after the call. */
  380. extern void explicit_bzero (void *__s, size_t __n) __THROW __nonnull ((1));
  381. /* Return the next DELIM-delimited token from *STRINGP,
  382. terminating it with a '\0', and update *STRINGP to point past it. */
  383. extern char *strsep (char **__restrict __stringp,
  384. const char *__restrict __delim)
  385. __THROW __nonnull ((1, 2));
  386. #endif
  387. #ifdef __USE_XOPEN2K8
  388. /* Return a string describing the meaning of the signal number in SIG. */
  389. extern char *strsignal (int __sig) __THROW;
  390. /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
  391. extern char *__stpcpy (char *__restrict __dest, const char *__restrict __src)
  392. __THROW __nonnull ((1, 2));
  393. extern char *stpcpy (char *__restrict __dest, const char *__restrict __src)
  394. __THROW __nonnull ((1, 2));
  395. /* Copy no more than N characters of SRC to DEST, returning the address of
  396. the last character written into DEST. */
  397. extern char *__stpncpy (char *__restrict __dest,
  398. const char *__restrict __src, size_t __n)
  399. __THROW __nonnull ((1, 2));
  400. extern char *stpncpy (char *__restrict __dest,
  401. const char *__restrict __src, size_t __n)
  402. __THROW __nonnull ((1, 2));
  403. #endif
  404. #ifdef __USE_GNU
  405. /* Compare S1 and S2 as strings holding name & indices/version numbers. */
  406. extern int strverscmp (const char *__s1, const char *__s2)
  407. __THROW __attribute_pure__ __nonnull ((1, 2));
  408. /* Sautee STRING briskly. */
  409. extern char *strfry (char *__string) __THROW __nonnull ((1));
  410. /* Frobnicate N bytes of S. */
  411. extern void *memfrob (void *__s, size_t __n) __THROW __nonnull ((1));
  412. # ifndef basename
  413. /* Return the file name within directory of FILENAME. We don't
  414. declare the function if the `basename' macro is available (defined
  415. in <libgen.h>) which makes the XPG version of this function
  416. available. */
  417. # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
  418. extern "C++" char *basename (char *__filename)
  419. __THROW __asm ("basename") __nonnull ((1));
  420. extern "C++" const char *basename (const char *__filename)
  421. __THROW __asm ("basename") __nonnull ((1));
  422. # else
  423. extern char *basename (const char *__filename) __THROW __nonnull ((1));
  424. # endif
  425. # endif
  426. #endif
  427. #if __GNUC_PREREQ (3,4)
  428. # if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
  429. /* Functions with security checks. */
  430. # include <bits/string_fortified.h>
  431. # endif
  432. #endif
  433. __END_DECLS
  434. #endif /* string.h */