string.h 14 KB

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