string.h 18 KB

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