1 /* Copyright (C) 1991-1993, 1995-2004, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 * ISO C99 Standard: 7.21 String handling <string.h>
30 /* Get size_t and NULL from <stddef.h>. */
37 /* Copy N bytes of SRC to DEST. */
38 extern void *memcpy (void *__restrict __dest
,
39 __const
void *__restrict __src
, size_t __n
)
40 __THROW
__nonnull ((1, 2));
41 /* Copy N bytes of SRC to DEST, guaranteeing
42 correct behavior for overlapping strings. */
43 extern void *memmove (void *__dest
, __const
void *__src
, size_t __n
)
44 __THROW
__nonnull ((1, 2));
47 /* Copy no more than N bytes of SRC to DEST, stopping when C is found.
48 Return the position in DEST one byte past where C was copied,
49 or NULL if C was not found in the first N bytes of SRC. */
50 #if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN
51 extern void *memccpy (void *__restrict __dest
, __const
void *__restrict __src
,
53 __THROW
__nonnull ((1, 2));
58 /* Set N bytes of S to C. */
59 extern void *memset (void *__s
, int __c
, size_t __n
) __THROW
__nonnull ((1));
61 /* Compare N bytes of S1 and S2. */
62 extern int memcmp (__const
void *__s1
, __const
void *__s2
, size_t __n
)
63 __THROW __attribute_pure__
__nonnull ((1, 2));
65 /* Search N bytes of S for C. */
66 extern void *memchr (__const
void *__s
, int __c
, size_t __n
)
67 __THROW __attribute_pure__
__nonnull ((1));
71 /* Search in S for C. This is similar to `memchr' but there is no
73 extern void *rawmemchr (__const
void *__s
, int __c
)
74 __THROW __attribute_pure__
__nonnull ((1));
76 /* Search N bytes of S for the final occurrence of C. */
77 extern void *memrchr (__const
void *__s
, int __c
, size_t __n
)
78 __THROW __attribute_pure__
__nonnull ((1));
83 /* Copy SRC to DEST. */
84 extern char *strcpy (char *__restrict __dest
, __const
char *__restrict __src
)
85 __THROW
__nonnull ((1, 2));
86 /* Copy no more than N characters of SRC to DEST. */
87 extern char *strncpy (char *__restrict __dest
,
88 __const
char *__restrict __src
, size_t __n
)
89 __THROW
__nonnull ((1, 2));
91 /* Append SRC onto DEST. */
92 extern char *strcat (char *__restrict __dest
, __const
char *__restrict __src
)
93 __THROW
__nonnull ((1, 2));
94 /* Append no more than N characters from SRC onto DEST. */
95 extern char *strncat (char *__restrict __dest
, __const
char *__restrict __src
,
96 size_t __n
) __THROW
__nonnull ((1, 2));
98 /* Compare S1 and S2. */
99 extern int strcmp (__const
char *__s1
, __const
char *__s2
)
100 __THROW __attribute_pure__
__nonnull ((1, 2));
101 /* Compare N characters of S1 and S2. */
102 extern int strncmp (__const
char *__s1
, __const
char *__s2
, size_t __n
)
103 __THROW __attribute_pure__
__nonnull ((1, 2));
105 /* Compare the collated forms of S1 and S2. */
106 extern int strcoll (__const
char *__s1
, __const
char *__s2
)
107 __THROW __attribute_pure__
__nonnull ((1, 2));
108 /* Put a transformation of SRC into no more than N bytes of DEST. */
109 extern size_t strxfrm (char *__restrict __dest
,
110 __const
char *__restrict __src
, size_t __n
)
111 __THROW
__nonnull ((2));
115 /* The following functions are equivalent to the both above but they
116 take the locale they use for the collation as an extra argument.
117 This is not standardsized but something like will come. */
118 # include <xlocale.h>
120 /* Compare the collated forms of S1 and S2 using rules from L. */
121 extern int strcoll_l (__const
char *__s1
, __const
char *__s2
, __locale_t __l
)
122 __THROW __attribute_pure__
__nonnull ((1, 2, 3));
123 /* Put a transformation of SRC into no more than N bytes of DEST. */
124 extern size_t strxfrm_l (char *__dest
, __const
char *__src
, size_t __n
,
125 __locale_t __l
) __THROW
__nonnull ((2, 4));
128 #if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
129 /* Duplicate S, returning an identical malloc'd string. */
130 extern char *strdup (__const
char *__s
)
131 __THROW __attribute_malloc__
__nonnull ((1));
134 /* Return a malloc'd copy of at most N bytes of STRING. The
135 resultant string is terminated even if no null terminator
136 appears before STRING[N]. */
137 #if defined __USE_GNU
138 extern char *strndup (__const
char *__string
, size_t __n
)
139 __THROW __attribute_malloc__
__nonnull ((1));
142 #if defined __USE_GNU && defined __GNUC__
143 /* Duplicate S, returning an identical alloca'd string. */
144 # define strdupa(s) \
147 __const char *__old = (s); \
148 size_t __len = strlen (__old) + 1; \
149 char *__new = (char *) __builtin_alloca (__len); \
150 (char *) memcpy (__new, __old, __len); \
153 /* Return an alloca'd copy of at most N bytes of string. */
154 # define strndupa(s, n) \
157 __const char *__old = (s); \
158 size_t __len = strnlen (__old, (n)); \
159 char *__new = (char *) __builtin_alloca (__len + 1); \
160 __new[__len] = '\0'; \
161 (char *) memcpy (__new, __old, __len); \
165 __BEGIN_NAMESPACE_STD
166 /* Find the first occurrence of C in S. */
167 extern char *strchr (__const
char *__s
, int __c
)
168 __THROW __attribute_pure__
__nonnull ((1));
169 /* Find the last occurrence of C in S. */
170 extern char *strrchr (__const
char *__s
, int __c
)
171 __THROW __attribute_pure__
__nonnull ((1));
175 /* This function is similar to `strchr'. But it returns a pointer to
176 the closing NUL byte in case C is not found in S. */
177 extern char *strchrnul (__const
char *__s
, int __c
)
178 __THROW __attribute_pure__
__nonnull ((1));
181 __BEGIN_NAMESPACE_STD
182 /* Return the length of the initial segment of S which
183 consists entirely of characters not in REJECT. */
184 extern size_t strcspn (__const
char *__s
, __const
char *__reject
)
185 __THROW __attribute_pure__
__nonnull ((1, 2));
186 /* Return the length of the initial segment of S which
187 consists entirely of characters in ACCEPT. */
188 extern size_t strspn (__const
char *__s
, __const
char *__accept
)
189 __THROW __attribute_pure__
__nonnull ((1, 2));
190 /* Find the first occurrence in S of any character in ACCEPT. */
191 extern char *strpbrk (__const
char *__s
, __const
char *__accept
)
192 __THROW __attribute_pure__
__nonnull ((1, 2));
193 /* Find the first occurrence of NEEDLE in HAYSTACK. */
194 extern char *strstr (__const
char *__haystack
, __const
char *__needle
)
195 __THROW __attribute_pure__
__nonnull ((1, 2));
198 /* Divide S into tokens separated by characters in DELIM. */
199 extern char *strtok (char *__restrict __s
, __const
char *__restrict __delim
)
200 __THROW
__nonnull ((2));
203 /* Divide S into tokens separated by characters in DELIM. Information
204 passed between calls are stored in SAVE_PTR. */
205 extern char *__strtok_r (char *__restrict __s
,
206 __const
char *__restrict __delim
,
207 char **__restrict __save_ptr
)
208 __THROW
__nonnull ((2, 3));
209 #if defined __USE_POSIX || defined __USE_MISC
210 extern char *strtok_r (char *__restrict __s
, __const
char *__restrict __delim
,
211 char **__restrict __save_ptr
)
212 __THROW
__nonnull ((2, 3));
216 /* Similar to `strstr' but this function ignores the case of both strings. */
217 extern char *strcasestr (__const
char *__haystack
, __const
char *__needle
)
218 __THROW __attribute_pure__
__nonnull ((1, 2));
222 /* Find the first occurrence of NEEDLE in HAYSTACK.
223 NEEDLE is NEEDLELEN bytes long;
224 HAYSTACK is HAYSTACKLEN bytes long. */
225 extern void *memmem (__const
void *__haystack
, size_t __haystacklen
,
226 __const
void *__needle
, size_t __needlelen
)
227 __THROW __attribute_pure__
__nonnull ((1, 3));
229 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
230 last written byte. */
231 extern void *__mempcpy (void *__restrict __dest
,
232 __const
void *__restrict __src
, size_t __n
)
233 __THROW
__nonnull ((1, 2));
234 extern void *mempcpy (void *__restrict __dest
,
235 __const
void *__restrict __src
, size_t __n
)
236 __THROW
__nonnull ((1, 2));
240 __BEGIN_NAMESPACE_STD
241 /* Return the length of S. */
242 extern size_t strlen (__const
char *__s
)
243 __THROW __attribute_pure__
__nonnull ((1));
247 /* Find the length of STRING, but scan at most MAXLEN characters.
248 If no '\0' terminator is found in that many characters, return MAXLEN. */
249 extern size_t strnlen (__const
char *__string
, size_t __maxlen
)
250 __THROW __attribute_pure__
__nonnull ((1));
254 __BEGIN_NAMESPACE_STD
255 /* Return a string describing the meaning of the `errno' code in ERRNUM. */
256 extern char *strerror (int __errnum
) __THROW
;
258 #if defined __USE_XOPEN2K || defined __USE_MISC
259 /* Reentrant version of `strerror'.
260 There are 2 flavors of `strerror_r', GNU which returns the string
261 and may or may not use the supplied temporary buffer and POSIX one
262 which fills the string into the buffer.
263 To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
264 without -D_GNU_SOURCE is needed, otherwise the GNU version is
266 # if defined __USE_XOPEN2K && !defined __USE_GNU
267 /* Fill BUF with a string describing the meaning of the `errno' code in
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));
274 extern int __xpg_strerror_r (int __errnum
, char *__buf
, size_t __buflen
)
275 __THROW
__nonnull ((2));
276 # define strerror_r __xpg_strerror_r
279 /* If a temporary buffer is required, at most BUFLEN bytes of BUF will be
281 extern char *strerror_r (int __errnum
, char *__buf
, size_t __buflen
)
282 __THROW
__nonnull ((2));
287 /* Translate error number to string according to the locale L. */
288 extern char *strerror_l (int __errnum
, __locale_t __l
) __THROW
;
292 /* We define this function always since `bzero' is sometimes needed when
293 the namespace rules does not allow this. */
294 extern void __bzero (void *__s
, size_t __n
) __THROW
__nonnull ((1));
297 /* Copy N bytes of SRC to DEST (like memmove, but args reversed). */
298 extern void bcopy (__const
void *__src
, void *__dest
, size_t __n
)
299 __THROW
__nonnull ((1, 2));
301 /* Set N bytes of S to 0. */
302 extern void bzero (void *__s
, size_t __n
) __THROW
__nonnull ((1));
304 /* Compare N bytes of S1 and S2 (same as memcmp). */
305 extern int bcmp (__const
void *__s1
, __const
void *__s2
, size_t __n
)
306 __THROW __attribute_pure__
__nonnull ((1, 2));
308 /* Find the first occurrence of C in S (same as strchr). */
309 extern char *index (__const
char *__s
, int __c
)
310 __THROW __attribute_pure__
__nonnull ((1));
312 /* Find the last occurrence of C in S (same as strrchr). */
313 extern char *rindex (__const
char *__s
, int __c
)
314 __THROW __attribute_pure__
__nonnull ((1));
316 /* Return the position of the first bit set in I, or 0 if none are set.
317 The least-significant bit is position 1, the most-significant 32. */
318 extern int ffs (int __i
) __THROW
__attribute__ ((__const__
));
320 /* The following two functions are non-standard but necessary for non-32 bit
323 extern int ffsl (long int __l
) __THROW
__attribute__ ((__const__
));
325 __extension__
extern int ffsll (long long int __ll
)
326 __THROW
__attribute__ ((__const__
));
330 /* Compare S1 and S2, ignoring case. */
331 extern int strcasecmp (__const
char *__s1
, __const
char *__s2
)
332 __THROW __attribute_pure__
__nonnull ((1, 2));
334 /* Compare no more than N chars of S1 and S2, ignoring case. */
335 extern int strncasecmp (__const
char *__s1
, __const
char *__s2
, size_t __n
)
336 __THROW __attribute_pure__
__nonnull ((1, 2));
337 #endif /* Use BSD. */
340 /* Again versions of a few functions which use the given locale instead
341 of the global one. */
342 extern int strcasecmp_l (__const
char *__s1
, __const
char *__s2
,
344 __THROW __attribute_pure__
__nonnull ((1, 2, 3));
346 extern int strncasecmp_l (__const
char *__s1
, __const
char *__s2
,
347 size_t __n
, __locale_t __loc
)
348 __THROW __attribute_pure__
__nonnull ((1, 2, 4));
352 /* Return the next DELIM-delimited token from *STRINGP,
353 terminating it with a '\0', and update *STRINGP to point past it. */
354 extern char *strsep (char **__restrict __stringp
,
355 __const
char *__restrict __delim
)
356 __THROW
__nonnull ((1, 2));
360 /* Compare S1 and S2 as strings holding name & indices/version numbers. */
361 extern int strverscmp (__const
char *__s1
, __const
char *__s2
)
362 __THROW __attribute_pure__
__nonnull ((1, 2));
364 /* Return a string describing the meaning of the signal number in SIG. */
365 extern char *strsignal (int __sig
) __THROW
;
367 /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
368 extern char *__stpcpy (char *__restrict __dest
, __const
char *__restrict __src
)
369 __THROW
__nonnull ((1, 2));
370 extern char *stpcpy (char *__restrict __dest
, __const
char *__restrict __src
)
371 __THROW
__nonnull ((1, 2));
373 /* Copy no more than N characters of SRC to DEST, returning the address of
374 the last character written into DEST. */
375 extern char *__stpncpy (char *__restrict __dest
,
376 __const
char *__restrict __src
, size_t __n
)
377 __THROW
__nonnull ((1, 2));
378 extern char *stpncpy (char *__restrict __dest
,
379 __const
char *__restrict __src
, size_t __n
)
380 __THROW
__nonnull ((1, 2));
382 /* Sautee STRING briskly. */
383 extern char *strfry (char *__string
) __THROW
__nonnull ((1));
385 /* Frobnicate N bytes of S. */
386 extern void *memfrob (void *__s
, size_t __n
) __THROW
__nonnull ((1));
389 /* Return the file name within directory of FILENAME. We don't
390 declare the function if the `basename' macro is available (defined
391 in <libgen.h>) which makes the XPG version of this function
393 extern char *basename (__const
char *__filename
) __THROW
__nonnull ((1));
398 #if defined __GNUC__ && __GNUC__ >= 2
399 # if defined __OPTIMIZE__ && !defined __OPTIMIZE_SIZE__ \
400 && !defined __NO_INLINE__ && !defined __cplusplus
401 /* When using GNU CC we provide some optimized versions of selected
402 functions from this header. There are two kinds of optimizations:
404 - machine-dependent optimizations, most probably using inline
405 assembler code; these might be quite expensive since the code
406 size can increase significantly.
407 These optimizations are not used unless the symbol
409 is defined before including this header.
411 - machine-independent optimizations which do not increase the
412 code size significantly and which optimize mainly situations
413 where one or more arguments are compile-time constants.
414 These optimizations are used always when the compiler is
417 One can inhibit all optimizations by defining __NO_STRING_INLINES. */
419 /* Get the machine-dependent optimizations (if any). */
420 # include <bits/string.h>
422 /* These are generic optimizations which do not add too much inline code. */
423 # include <bits/string2.h>
426 # if __USE_FORTIFY_LEVEL > 0 && !defined __cplusplus
427 /* Functions with security checks. */
428 # include <bits/string3.h>
434 #endif /* string.h */