string.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* Copyright (C) 1991-1993,1995-2004,2007,2009 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. #include <features.h>
  20. __BEGIN_DECLS
  21. /* Get size_t and NULL from <stddef.h>. */
  22. #define __need_size_t
  23. #define __need_NULL
  24. #include <stddef.h>
  25. __BEGIN_NAMESPACE_STD
  26. /* Copy N bytes of SRC to DEST. */
  27. extern void *memcpy (void *__restrict __dest,
  28. const void *__restrict __src, size_t __n)
  29. __THROW __nonnull ((1, 2));
  30. libc_hidden_proto(memcpy)
  31. /* Copy N bytes of SRC to DEST, guaranteeing
  32. correct behavior for overlapping strings. */
  33. extern void *memmove (void *__dest, const void *__src, size_t __n)
  34. __THROW __nonnull ((1, 2));
  35. libc_hidden_proto(memmove)
  36. __END_NAMESPACE_STD
  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_SVID || defined __USE_BSD || 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. libc_hidden_proto(memccpy)
  45. #endif /* SVID. */
  46. __BEGIN_NAMESPACE_STD
  47. /* Set N bytes of S to C. */
  48. extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
  49. libc_hidden_proto(memset)
  50. /* Compare N bytes of S1 and S2. */
  51. extern int memcmp (const void *__s1, const void *__s2, size_t __n)
  52. __THROW __attribute_pure__ __nonnull ((1, 2));
  53. libc_hidden_proto(memcmp)
  54. /* Search N bytes of S for C. */
  55. extern void *memchr (const void *__s, int __c, size_t __n)
  56. __THROW __attribute_pure__ __nonnull ((1));
  57. libc_hidden_proto(memchr)
  58. __END_NAMESPACE_STD
  59. #ifdef __USE_GNU
  60. /* Search in S for C. This is similar to `memchr' but there is no
  61. length limit. */
  62. extern void *rawmemchr (const void *__s, int __c)
  63. __THROW __attribute_pure__ __nonnull ((1));
  64. libc_hidden_proto(rawmemchr)
  65. /* Search N bytes of S for the final occurrence of C. */
  66. extern void *memrchr (const void *__s, int __c, size_t __n)
  67. __THROW __attribute_pure__ __nonnull ((1));
  68. libc_hidden_proto(memrchr)
  69. #endif
  70. __BEGIN_NAMESPACE_STD
  71. /* Copy SRC to DEST. */
  72. extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
  73. __THROW __nonnull ((1, 2));
  74. libc_hidden_proto(strcpy)
  75. /* Copy no more than N characters of SRC to DEST. */
  76. extern char *strncpy (char *__restrict __dest,
  77. const char *__restrict __src, size_t __n)
  78. __THROW __nonnull ((1, 2));
  79. libc_hidden_proto(strncpy)
  80. /* Append SRC onto DEST. */
  81. extern char *strcat (char *__restrict __dest, const char *__restrict __src)
  82. __THROW __nonnull ((1, 2));
  83. libc_hidden_proto(strcat)
  84. /* Append no more than N characters from SRC onto DEST. */
  85. extern char *strncat (char *__restrict __dest, const char *__restrict __src,
  86. size_t __n) __THROW __nonnull ((1, 2));
  87. libc_hidden_proto(strncat)
  88. /* Compare S1 and S2. */
  89. extern int strcmp (const char *__s1, const char *__s2)
  90. __THROW __attribute_pure__ __nonnull ((1, 2));
  91. libc_hidden_proto(strcmp)
  92. /* Compare N characters of S1 and S2. */
  93. extern int strncmp (const char *__s1, const char *__s2, size_t __n)
  94. __THROW __attribute_pure__ __nonnull ((1, 2));
  95. libc_hidden_proto(strncmp)
  96. /* Compare the collated forms of S1 and S2. */
  97. extern int strcoll (const char *__s1, const char *__s2)
  98. __THROW __attribute_pure__ __nonnull ((1, 2));
  99. libc_hidden_proto(strcoll)
  100. /* Put a transformation of SRC into no more than N bytes of DEST. */
  101. extern size_t strxfrm (char *__restrict __dest,
  102. const char *__restrict __src, size_t __n)
  103. __THROW __nonnull ((2));
  104. __END_NAMESPACE_STD
  105. #if defined __USE_XOPEN2K8 && defined __UCLIBC_HAS_XLOCALE__
  106. /* The following functions are equivalent to the both above but they
  107. take the locale they use for the collation as an extra argument.
  108. This is not standardsized but something like will come. */
  109. # include <xlocale.h>
  110. /* Compare the collated forms of S1 and S2 using rules from L. */
  111. extern int strcoll_l (const char *__s1, const char *__s2, __locale_t __l)
  112. __THROW __attribute_pure__ __nonnull ((1, 2, 3));
  113. libc_hidden_proto(strcoll_l)
  114. /* Put a transformation of SRC into no more than N bytes of DEST. */
  115. extern size_t strxfrm_l (char *__dest, const char *__src, size_t __n,
  116. __locale_t __l) __THROW __nonnull ((2, 4));
  117. libc_hidden_proto(strxfrm_l)
  118. #endif
  119. #if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
  120. /* Duplicate S, returning an identical malloc'd string. */
  121. extern char *strdup (const char *__s)
  122. __THROW __attribute_malloc__ __nonnull ((1));
  123. libc_hidden_proto(strdup)
  124. #endif
  125. /* Return a malloc'd copy of at most N bytes of STRING. The
  126. resultant string is terminated even if no null terminator
  127. appears before STRING[N]. */
  128. #if defined __USE_XOPEN2K8
  129. extern char *strndup (const char *__string, size_t __n)
  130. __THROW __attribute_malloc__ __nonnull ((1));
  131. libc_hidden_proto(strndup)
  132. #endif
  133. #if defined __USE_GNU && defined __GNUC__
  134. /* Duplicate S, returning an identical alloca'd string. */
  135. # define strdupa(s) \
  136. (__extension__ \
  137. ({ \
  138. const char *__old = (s); \
  139. size_t __len = strlen (__old) + 1; \
  140. char *__new = (char *) __builtin_alloca (__len); \
  141. (char *) memcpy (__new, __old, __len); \
  142. }))
  143. /* Return an alloca'd copy of at most N bytes of string. */
  144. # define strndupa(s, n) \
  145. (__extension__ \
  146. ({ \
  147. const char *__old = (s); \
  148. size_t __len = strnlen (__old, (n)); \
  149. char *__new = (char *) __builtin_alloca (__len + 1); \
  150. __new[__len] = '\0'; \
  151. (char *) memcpy (__new, __old, __len); \
  152. }))
  153. #endif
  154. __BEGIN_NAMESPACE_STD
  155. /* Find the first occurrence of C in S. */
  156. extern char *strchr (const char *__s, int __c)
  157. __THROW __attribute_pure__ __nonnull ((1));
  158. libc_hidden_proto(strchr)
  159. /* Find the last occurrence of C in S. */
  160. extern char *strrchr (const char *__s, int __c)
  161. __THROW __attribute_pure__ __nonnull ((1));
  162. libc_hidden_proto(strrchr)
  163. __END_NAMESPACE_STD
  164. #ifdef __USE_GNU
  165. /* This function is similar to `strchr'. But it returns a pointer to
  166. the closing NUL byte in case C is not found in S. */
  167. extern char *strchrnul (const char *__s, int __c)
  168. __THROW __attribute_pure__ __nonnull ((1));
  169. libc_hidden_proto(strchrnul)
  170. #endif
  171. __BEGIN_NAMESPACE_STD
  172. /* Return the length of the initial segment of S which
  173. consists entirely of characters not in REJECT. */
  174. extern size_t strcspn (const char *__s, const char *__reject)
  175. __THROW __attribute_pure__ __nonnull ((1, 2));
  176. libc_hidden_proto(strcspn)
  177. /* Return the length of the initial segment of S which
  178. consists entirely of characters in ACCEPT. */
  179. extern size_t strspn (const char *__s, const char *__accept)
  180. __THROW __attribute_pure__ __nonnull ((1, 2));
  181. libc_hidden_proto(strspn)
  182. /* Find the first occurrence in S of any character in ACCEPT. */
  183. extern char *strpbrk (const char *__s, const char *__accept)
  184. __THROW __attribute_pure__ __nonnull ((1, 2));
  185. libc_hidden_proto(strpbrk)
  186. /* Find the first occurrence of NEEDLE in HAYSTACK. */
  187. extern char *strstr (const char *__haystack, const char *__needle)
  188. __THROW __attribute_pure__ __nonnull ((1, 2));
  189. libc_hidden_proto(strstr)
  190. /* Divide S into tokens separated by characters in DELIM. */
  191. extern char *strtok (char *__restrict __s, const char *__restrict __delim)
  192. __THROW __nonnull ((2));
  193. libc_hidden_proto(strtok)
  194. __END_NAMESPACE_STD
  195. /* Divide S into tokens separated by characters in DELIM. Information
  196. passed between calls are stored in SAVE_PTR. */
  197. #if 0 /* uClibc: disabled */
  198. extern char *__strtok_r (char *__restrict __s,
  199. const char *__restrict __delim,
  200. char **__restrict __save_ptr)
  201. __THROW __nonnull ((2, 3));
  202. #endif
  203. #if defined __USE_POSIX || defined __USE_MISC
  204. extern char *strtok_r (char *__restrict __s, const char *__restrict __delim,
  205. char **__restrict __save_ptr)
  206. __THROW __nonnull ((2, 3));
  207. libc_hidden_proto(strtok_r)
  208. #endif
  209. #ifdef __USE_GNU
  210. /* Similar to `strstr' but this function ignores the case of both strings. */
  211. extern char *strcasestr (const char *__haystack, const char *__needle)
  212. __THROW __attribute_pure__ __nonnull ((1, 2));
  213. libc_hidden_proto(strcasestr)
  214. #endif
  215. #ifdef __USE_GNU
  216. /* Find the first occurrence of NEEDLE in HAYSTACK.
  217. NEEDLE is NEEDLELEN bytes long;
  218. HAYSTACK is HAYSTACKLEN bytes long. */
  219. extern void *memmem (const void *__haystack, size_t __haystacklen,
  220. const void *__needle, size_t __needlelen)
  221. __THROW __attribute_pure__ __nonnull ((1, 3));
  222. /* Copy N bytes of SRC to DEST, return pointer to bytes after the
  223. last written byte. */
  224. #if __GNUC_PREREQ (3, 4)
  225. # define __mempcpy(dest, src, n) __builtin_mempcpy(dest, src, n)
  226. #else /* uClibc: disabled */
  227. extern void *__mempcpy (void *__restrict __dest,
  228. const void *__restrict __src, size_t __n)
  229. __THROW __nonnull ((1, 2));
  230. #endif
  231. extern void *mempcpy (void *__restrict __dest,
  232. const void *__restrict __src, size_t __n)
  233. __THROW __nonnull ((1, 2));
  234. libc_hidden_proto(mempcpy)
  235. #endif
  236. __BEGIN_NAMESPACE_STD
  237. /* Return the length of S. */
  238. extern size_t strlen (const char *__s)
  239. __THROW __attribute_pure__ __nonnull ((1));
  240. libc_hidden_proto(strlen)
  241. __END_NAMESPACE_STD
  242. #ifdef __USE_XOPEN2K8
  243. /* Find the length of STRING, but scan at most MAXLEN characters.
  244. If no '\0' terminator is found in that many characters, return MAXLEN. */
  245. extern size_t strnlen (const char *__string, size_t __maxlen)
  246. __THROW __attribute_pure__ __nonnull ((1));
  247. libc_hidden_proto(strnlen)
  248. #endif
  249. __BEGIN_NAMESPACE_STD
  250. /* Return a string describing the meaning of the `errno' code in ERRNUM. */
  251. extern char *strerror (int __errnum) __THROW;
  252. libc_hidden_proto(strerror)
  253. __END_NAMESPACE_STD
  254. #if defined __USE_XOPEN2K || defined __USE_MISC
  255. /* Reentrant version of `strerror'.
  256. There are 2 flavors of `strerror_r', GNU which returns the string
  257. and may or may not use the supplied temporary buffer and POSIX one
  258. which fills the string into the buffer.
  259. To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
  260. without -D_GNU_SOURCE is needed, otherwise the GNU version is
  261. preferred. */
  262. # if defined __USE_XOPEN2K && !defined __USE_GNU
  263. /* Fill BUF with a string describing the meaning of the `errno' code in
  264. ERRNUM. */
  265. extern int __xpg_strerror_r (int __errnum, char *__buf, size_t __buflen)
  266. __THROW __nonnull ((2));
  267. libc_hidden_proto(__xpg_strerror_r)
  268. # ifdef __REDIRECT_NTH
  269. extern int __REDIRECT_NTH (strerror_r,
  270. (int __errnum, char *__buf, size_t __buflen),
  271. __xpg_strerror_r) __nonnull ((2));
  272. # else
  273. # define strerror_r __xpg_strerror_r
  274. # endif
  275. # else
  276. /* If a temporary buffer is required, at most BUFLEN bytes of BUF will be
  277. used. */
  278. extern char *__glibc_strerror_r (int __errnum, char *__buf, size_t __buflen)
  279. __THROW __nonnull ((2));
  280. libc_hidden_proto(__glibc_strerror_r)
  281. # ifdef __REDIRECT_NTH
  282. extern char * __REDIRECT_NTH (strerror_r,
  283. (int __errnum, char *__buf, size_t __buflen),
  284. __glibc_strerror_r) __nonnull ((2));
  285. # else
  286. # define strerror_r __glibc_strerror_r
  287. # endif
  288. # endif
  289. #endif
  290. #if 0 /*defined __USE_XOPEN2K8 && defined __UCLIBC_HAS_XLOCALE__*/
  291. /* Translate error number to string according to the locale L. */
  292. extern char *strerror_l (int __errnum, __locale_t __l) __THROW;
  293. #endif
  294. /* We define this function always since `bzero' is sometimes needed when
  295. the namespace rules does not allow this. */
  296. #if 0 /* uClibc: disabled */
  297. extern void __bzero (void *__s, size_t __n) __THROW __nonnull ((1));
  298. #endif
  299. #ifdef __USE_BSD
  300. # ifdef __UCLIBC_SUSV3_LEGACY__
  301. /* Copy N bytes of SRC to DEST (like memmove, but args reversed). */
  302. extern void bcopy (const void *__src, void *__dest, size_t __n)
  303. __THROW __nonnull ((1, 2));
  304. /* Set N bytes of S to 0. */
  305. extern void bzero (void *__s, size_t __n) __THROW __nonnull ((1));
  306. /* Compare N bytes of S1 and S2 (same as memcmp). */
  307. extern int bcmp (const void *__s1, const void *__s2, size_t __n)
  308. __THROW __attribute_pure__ __nonnull ((1, 2));
  309. /* Find the first occurrence of C in S (same as strchr). */
  310. extern char *index (const char *__s, int __c)
  311. __THROW __attribute_pure__ __nonnull ((1));
  312. /* Find the last occurrence of C in S (same as strrchr). */
  313. extern char *rindex (const char *__s, int __c)
  314. __THROW __attribute_pure__ __nonnull ((1));
  315. # elif defined(__UCLIBC_SUSV3_LEGACY_MACROS__) && !defined(_STRINGS_H)
  316. /* bcopy/bzero/bcmp/index/rindex are marked LEGACY in SuSv3.
  317. * They are replaced as proposed by SuSv3. Don't sync this part
  318. * with glibc and keep it in sync with strings.h. */
  319. /* Copy N bytes of SRC to DEST (like memmove, but args reversed). */
  320. static __inline__ void bcopy (__const void *__src, void *__dest, size_t __n)
  321. {
  322. memmove(__dest, __src, __n);
  323. }
  324. /* Set N bytes of S to 0. */
  325. static __inline__ void bzero (void *__s, size_t __n)
  326. {
  327. memset(__s, 0, __n);
  328. }
  329. /* Compare N bytes of S1 and S2 (same as memcmp). */
  330. static __inline__ int bcmp (__const void *__s1, __const void *__s2, size_t __n)
  331. {
  332. return memcmp(__s1, __s2, __n);
  333. }
  334. /* Find the first occurrence of C in S (same as strchr). */
  335. static __inline__ char *index (__const char *__s, int __c)
  336. {
  337. return strchr(__s, __c);
  338. }
  339. /* Find the last occurrence of C in S (same as strrchr). */
  340. static __inline__ char *rindex (__const char *__s, int __c)
  341. {
  342. return strrchr(__s, __c);
  343. }
  344. # endif
  345. /* Return the position of the first bit set in I, or 0 if none are set.
  346. The least-significant bit is position 1, the most-significant 32. */
  347. extern int ffs (int __i) __THROW __attribute__ ((__const__));
  348. libc_hidden_proto(ffs)
  349. /* The following two functions are non-standard but necessary for non-32 bit
  350. platforms. */
  351. #ifdef __USE_GNU
  352. extern int ffsl (long int __l) __THROW __attribute__ ((__const__));
  353. # ifdef __GNUC__
  354. __extension__ extern int ffsll (long long int __ll)
  355. __THROW __attribute__ ((__const__));
  356. # endif
  357. # endif
  358. /* Compare S1 and S2, ignoring case. */
  359. extern int strcasecmp (const char *__s1, const char *__s2)
  360. __THROW __attribute_pure__ __nonnull ((1, 2));
  361. libc_hidden_proto(strcasecmp)
  362. /* Compare no more than N chars of S1 and S2, ignoring case. */
  363. extern int strncasecmp (const char *__s1, const char *__s2, size_t __n)
  364. __THROW __attribute_pure__ __nonnull ((1, 2));
  365. libc_hidden_proto(strncasecmp)
  366. #endif /* Use BSD. */
  367. #if defined __USE_XOPEN2K8 && defined __UCLIBC_HAS_XLOCALE__
  368. /* Again versions of a few functions which use the given locale instead
  369. of the global one. */
  370. extern int strcasecmp_l (const char *__s1, const char *__s2,
  371. __locale_t __loc)
  372. __THROW __attribute_pure__ __nonnull ((1, 2, 3));
  373. libc_hidden_proto(strcasecmp_l)
  374. extern int strncasecmp_l (const char *__s1, const char *__s2,
  375. size_t __n, __locale_t __loc)
  376. __THROW __attribute_pure__ __nonnull ((1, 2, 4));
  377. libc_hidden_proto(strncasecmp_l)
  378. #endif
  379. #ifdef __USE_BSD
  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. libc_hidden_proto(strsep)
  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. libc_hidden_proto(strsignal)
  391. /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
  392. # if 0 /* uClibc: disabled */
  393. extern char *__stpcpy (char *__restrict __dest, const char *__restrict __src)
  394. __THROW __nonnull ((1, 2));
  395. # endif
  396. extern char *stpcpy (char *__restrict __dest, const char *__restrict __src)
  397. __THROW __nonnull ((1, 2));
  398. libc_hidden_proto(stpcpy)
  399. /* Copy no more than N characters of SRC to DEST, returning the address of
  400. the last character written into DEST. */
  401. # if 0 /* uClibc: disabled */
  402. extern char *__stpncpy (char *__restrict __dest,
  403. const char *__restrict __src, size_t __n)
  404. __THROW __nonnull ((1, 2));
  405. # endif
  406. extern char *stpncpy (char *__restrict __dest,
  407. const char *__restrict __src, size_t __n)
  408. __THROW __nonnull ((1, 2));
  409. #endif
  410. #ifdef __USE_GNU
  411. /* Compare S1 and S2 as strings holding name & indices/version numbers. */
  412. extern int strverscmp (const char *__s1, const char *__s2)
  413. __THROW __attribute_pure__ __nonnull ((1, 2));
  414. libc_hidden_proto(strverscmp)
  415. # if 0 /* uClibc does not support strfry or memfrob. */
  416. /* Sautee STRING briskly. */
  417. extern char *strfry (char *__string) __THROW __nonnull ((1));
  418. /* Frobnicate N bytes of S. */
  419. extern void *memfrob (void *__s, size_t __n) __THROW __nonnull ((1));
  420. # endif
  421. # ifndef basename
  422. /* Return the file name within directory of FILENAME. We don't
  423. declare the function if the `basename' macro is available (defined
  424. in <libgen.h>) which makes the XPG version of this function
  425. available. */
  426. extern char *basename (const char *__filename) __THROW __nonnull ((1));
  427. # endif
  428. #endif /* __USE_GNU */
  429. #ifdef __USE_BSD
  430. /* Two OpenBSD extension functions. */
  431. extern size_t strlcat(char *__restrict dst, const char *__restrict src,
  432. size_t n) __THROW __nonnull ((1, 2));
  433. libc_hidden_proto(strlcat)
  434. extern size_t strlcpy(char *__restrict dst, const char *__restrict src,
  435. size_t n) __THROW __nonnull ((1, 2));
  436. libc_hidden_proto(strlcpy)
  437. #endif
  438. __END_DECLS
  439. #if defined(_LIBC) && defined(__UCLIBC_HAS_STRING_ARCH_OPT__)
  440. # if defined __i386__
  441. # include <../libc/string/i386/string.h>
  442. # endif
  443. #endif
  444. #endif /* string.h */