string.h 17 KB

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