string.h 16 KB

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