string.h 15 KB

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