string.h 16 KB

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