1 /* Copyright (C) 1991-2020 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, see
16 <https://www.gnu.org/licenses/>. */
19 * ISO C99 Standard: 7.21 String handling <string.h>
25 #define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
26 #include <bits/libc-header-start.h>
30 /* Get size_t and NULL from <stddef.h>. */
35 /* Tell the caller that we provide correct C++ prototypes. */
36 #if defined __cplusplus && (__GNUC_PREREQ (4, 4) \
37 || __glibc_clang_prereq (3, 5))
38 # define __CORRECT_ISO_CPP_STRING_H_PROTO
42 /* Copy N bytes of SRC to DEST. */
43 extern void *memcpy (void *__restrict __dest
, const void *__restrict __src
,
44 size_t __n
) __THROW
__nonnull ((1, 2));
45 /* Copy N bytes of SRC to DEST, guaranteeing
46 correct behavior for overlapping strings. */
47 extern void *memmove (void *__dest
, const void *__src
, size_t __n
)
48 __THROW
__nonnull ((1, 2));
50 /* Copy no more than N bytes of SRC to DEST, stopping when C is found.
51 Return the position in DEST one byte past where C was copied,
52 or NULL if C was not found in the first N bytes of SRC. */
53 #if defined __USE_MISC || defined __USE_XOPEN || __GLIBC_USE (ISOC2X)
54 extern void *memccpy (void *__restrict __dest
, const void *__restrict __src
,
56 __THROW
__nonnull ((1, 2));
57 #endif /* Misc || X/Open. */
60 /* Set N bytes of S to C. */
61 extern void *memset (void *__s
, int __c
, size_t __n
) __THROW
__nonnull ((1));
63 /* Compare N bytes of S1 and S2. */
64 extern int memcmp (const void *__s1
, const void *__s2
, size_t __n
)
65 __THROW __attribute_pure__
__nonnull ((1, 2));
67 /* Search N bytes of S for C. */
68 #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
71 extern void *memchr (void *__s
, int __c
, size_t __n
)
72 __THROW
__asm ("memchr") __attribute_pure__
__nonnull ((1));
73 extern const void *memchr (const void *__s
, int __c
, size_t __n
)
74 __THROW
__asm ("memchr") __attribute_pure__
__nonnull ((1));
77 __extern_always_inline
void *
78 memchr (void *__s
, int __c
, size_t __n
) __THROW
80 return __builtin_memchr (__s
, __c
, __n
);
83 __extern_always_inline
const void *
84 memchr (const void *__s
, int __c
, size_t __n
) __THROW
86 return __builtin_memchr (__s
, __c
, __n
);
91 extern void *memchr (const void *__s
, int __c
, size_t __n
)
92 __THROW __attribute_pure__
__nonnull ((1));
96 /* Search in S for C. This is similar to `memchr' but there is no
98 # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
99 extern "C++" void *rawmemchr (void *__s
, int __c
)
100 __THROW
__asm ("rawmemchr") __attribute_pure__
__nonnull ((1));
101 extern "C++" const void *rawmemchr (const void *__s
, int __c
)
102 __THROW
__asm ("rawmemchr") __attribute_pure__
__nonnull ((1));
104 extern void *rawmemchr (const void *__s
, int __c
)
105 __THROW __attribute_pure__
__nonnull ((1));
108 /* Search N bytes of S for the final occurrence of C. */
109 # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
110 extern "C++" void *memrchr (void *__s
, int __c
, size_t __n
)
111 __THROW
__asm ("memrchr") __attribute_pure__
__nonnull ((1));
112 extern "C++" const void *memrchr (const void *__s
, int __c
, size_t __n
)
113 __THROW
__asm ("memrchr") __attribute_pure__
__nonnull ((1));
115 extern void *memrchr (const void *__s
, int __c
, size_t __n
)
116 __THROW __attribute_pure__
__nonnull ((1));
121 /* Copy SRC to DEST. */
122 extern char *strcpy (char *__restrict __dest
, const char *__restrict __src
)
123 __THROW
__nonnull ((1, 2));
124 /* Copy no more than N characters of SRC to DEST. */
125 extern char *strncpy (char *__restrict __dest
,
126 const char *__restrict __src
, size_t __n
)
127 __THROW
__nonnull ((1, 2));
129 /* Append SRC onto DEST. */
130 extern char *strcat (char *__restrict __dest
, const char *__restrict __src
)
131 __THROW
__nonnull ((1, 2));
132 /* Append no more than N characters from SRC onto DEST. */
133 extern char *strncat (char *__restrict __dest
, const char *__restrict __src
,
134 size_t __n
) __THROW
__nonnull ((1, 2));
136 /* Compare S1 and S2. */
137 extern int strcmp (const char *__s1
, const char *__s2
)
138 __THROW __attribute_pure__
__nonnull ((1, 2));
139 /* Compare N characters of S1 and S2. */
140 extern int strncmp (const char *__s1
, const char *__s2
, size_t __n
)
141 __THROW __attribute_pure__
__nonnull ((1, 2));
143 /* Compare the collated forms of S1 and S2. */
144 extern int strcoll (const char *__s1
, const char *__s2
)
145 __THROW __attribute_pure__
__nonnull ((1, 2));
146 /* Put a transformation of SRC into no more than N bytes of DEST. */
147 extern size_t strxfrm (char *__restrict __dest
,
148 const char *__restrict __src
, size_t __n
)
149 __THROW
__nonnull ((2));
151 #ifdef __USE_XOPEN2K8
152 /* POSIX.1-2008 extended locale interface (see locale.h). */
153 # include <bits/types/locale_t.h>
155 /* Compare the collated forms of S1 and S2, using sorting rules from L. */
156 extern int strcoll_l (const char *__s1
, const char *__s2
, locale_t __l
)
157 __THROW __attribute_pure__
__nonnull ((1, 2, 3));
158 /* Put a transformation of SRC into no more than N bytes of DEST,
159 using sorting rules from L. */
160 extern size_t strxfrm_l (char *__dest
, const char *__src
, size_t __n
,
161 locale_t __l
) __THROW
__nonnull ((2, 4));
164 #if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8 \
165 || __GLIBC_USE (LIB_EXT2) || __GLIBC_USE (ISOC2X))
166 /* Duplicate S, returning an identical malloc'd string. */
167 extern char *strdup (const char *__s
)
168 __THROW __attribute_malloc__
__nonnull ((1));
171 /* Return a malloc'd copy of at most N bytes of STRING. The
172 resultant string is terminated even if no null terminator
173 appears before STRING[N]. */
174 #if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) || __GLIBC_USE (ISOC2X)
175 extern char *strndup (const char *__string
, size_t __n
)
176 __THROW __attribute_malloc__
__nonnull ((1));
179 #if defined __USE_GNU && defined __GNUC__
180 /* Duplicate S, returning an identical alloca'd string. */
181 # define strdupa(s) \
184 const char *__old = (s); \
185 size_t __len = strlen (__old) + 1; \
186 char *__new = (char *) __builtin_alloca (__len); \
187 (char *) memcpy (__new, __old, __len); \
190 /* Return an alloca'd copy of at most N bytes of string. */
191 # define strndupa(s, n) \
194 const char *__old = (s); \
195 size_t __len = strnlen (__old, (n)); \
196 char *__new = (char *) __builtin_alloca (__len + 1); \
197 __new[__len] = '\0'; \
198 (char *) memcpy (__new, __old, __len); \
202 /* Find the first occurrence of C in S. */
203 #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
206 extern char *strchr (char *__s
, int __c
)
207 __THROW
__asm ("strchr") __attribute_pure__
__nonnull ((1));
208 extern const char *strchr (const char *__s
, int __c
)
209 __THROW
__asm ("strchr") __attribute_pure__
__nonnull ((1));
212 __extern_always_inline
char *
213 strchr (char *__s
, int __c
) __THROW
215 return __builtin_strchr (__s
, __c
);
218 __extern_always_inline
const char *
219 strchr (const char *__s
, int __c
) __THROW
221 return __builtin_strchr (__s
, __c
);
226 extern char *strchr (const char *__s
, int __c
)
227 __THROW __attribute_pure__
__nonnull ((1));
229 /* Find the last occurrence of C in S. */
230 #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
233 extern char *strrchr (char *__s
, int __c
)
234 __THROW
__asm ("strrchr") __attribute_pure__
__nonnull ((1));
235 extern const char *strrchr (const char *__s
, int __c
)
236 __THROW
__asm ("strrchr") __attribute_pure__
__nonnull ((1));
239 __extern_always_inline
char *
240 strrchr (char *__s
, int __c
) __THROW
242 return __builtin_strrchr (__s
, __c
);
245 __extern_always_inline
const char *
246 strrchr (const char *__s
, int __c
) __THROW
248 return __builtin_strrchr (__s
, __c
);
253 extern char *strrchr (const char *__s
, int __c
)
254 __THROW __attribute_pure__
__nonnull ((1));
258 /* This function is similar to `strchr'. But it returns a pointer to
259 the closing NUL byte in case C is not found in S. */
260 # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
261 extern "C++" char *strchrnul (char *__s
, int __c
)
262 __THROW
__asm ("strchrnul") __attribute_pure__
__nonnull ((1));
263 extern "C++" const char *strchrnul (const char *__s
, int __c
)
264 __THROW
__asm ("strchrnul") __attribute_pure__
__nonnull ((1));
266 extern char *strchrnul (const char *__s
, int __c
)
267 __THROW __attribute_pure__
__nonnull ((1));
271 /* Return the length of the initial segment of S which
272 consists entirely of characters not in REJECT. */
273 extern size_t strcspn (const char *__s
, const char *__reject
)
274 __THROW __attribute_pure__
__nonnull ((1, 2));
275 /* Return the length of the initial segment of S which
276 consists entirely of characters in ACCEPT. */
277 extern size_t strspn (const char *__s
, const char *__accept
)
278 __THROW __attribute_pure__
__nonnull ((1, 2));
279 /* Find the first occurrence in S of any character in ACCEPT. */
280 #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
283 extern char *strpbrk (char *__s
, const char *__accept
)
284 __THROW
__asm ("strpbrk") __attribute_pure__
__nonnull ((1, 2));
285 extern const char *strpbrk (const char *__s
, const char *__accept
)
286 __THROW
__asm ("strpbrk") __attribute_pure__
__nonnull ((1, 2));
289 __extern_always_inline
char *
290 strpbrk (char *__s
, const char *__accept
) __THROW
292 return __builtin_strpbrk (__s
, __accept
);
295 __extern_always_inline
const char *
296 strpbrk (const char *__s
, const char *__accept
) __THROW
298 return __builtin_strpbrk (__s
, __accept
);
303 extern char *strpbrk (const char *__s
, const char *__accept
)
304 __THROW __attribute_pure__
__nonnull ((1, 2));
306 /* Find the first occurrence of NEEDLE in HAYSTACK. */
307 #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
310 extern char *strstr (char *__haystack
, const char *__needle
)
311 __THROW
__asm ("strstr") __attribute_pure__
__nonnull ((1, 2));
312 extern const char *strstr (const char *__haystack
, const char *__needle
)
313 __THROW
__asm ("strstr") __attribute_pure__
__nonnull ((1, 2));
316 __extern_always_inline
char *
317 strstr (char *__haystack
, const char *__needle
) __THROW
319 return __builtin_strstr (__haystack
, __needle
);
322 __extern_always_inline
const char *
323 strstr (const char *__haystack
, const char *__needle
) __THROW
325 return __builtin_strstr (__haystack
, __needle
);
330 extern char *strstr (const char *__haystack
, const char *__needle
)
331 __THROW __attribute_pure__
__nonnull ((1, 2));
335 /* Divide S into tokens separated by characters in DELIM. */
336 extern char *strtok (char *__restrict __s
, const char *__restrict __delim
)
337 __THROW
__nonnull ((2));
339 /* Divide S into tokens separated by characters in DELIM. Information
340 passed between calls are stored in SAVE_PTR. */
341 extern char *__strtok_r (char *__restrict __s
,
342 const char *__restrict __delim
,
343 char **__restrict __save_ptr
)
344 __THROW
__nonnull ((2, 3));
346 extern char *strtok_r (char *__restrict __s
, const char *__restrict __delim
,
347 char **__restrict __save_ptr
)
348 __THROW
__nonnull ((2, 3));
352 /* Similar to `strstr' but this function ignores the case of both strings. */
353 # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
354 extern "C++" char *strcasestr (char *__haystack
, const char *__needle
)
355 __THROW
__asm ("strcasestr") __attribute_pure__
__nonnull ((1, 2));
356 extern "C++" const char *strcasestr (const char *__haystack
,
357 const char *__needle
)
358 __THROW
__asm ("strcasestr") __attribute_pure__
__nonnull ((1, 2));
360 extern char *strcasestr (const char *__haystack
, const char *__needle
)
361 __THROW __attribute_pure__
__nonnull ((1, 2));
366 /* Find the first occurrence of NEEDLE in HAYSTACK.
367 NEEDLE is NEEDLELEN bytes long;
368 HAYSTACK is HAYSTACKLEN bytes long. */
369 extern void *memmem (const void *__haystack
, size_t __haystacklen
,
370 const void *__needle
, size_t __needlelen
)
371 __THROW __attribute_pure__
__nonnull ((1, 3));
373 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
374 last written byte. */
375 extern void *__mempcpy (void *__restrict __dest
,
376 const void *__restrict __src
, size_t __n
)
377 __THROW
__nonnull ((1, 2));
378 extern void *mempcpy (void *__restrict __dest
,
379 const void *__restrict __src
, size_t __n
)
380 __THROW
__nonnull ((1, 2));
384 /* Return the length of S. */
385 extern size_t strlen (const char *__s
)
386 __THROW __attribute_pure__
__nonnull ((1));
388 #ifdef __USE_XOPEN2K8
389 /* Find the length of STRING, but scan at most MAXLEN characters.
390 If no '\0' terminator is found in that many characters, return MAXLEN. */
391 extern size_t strnlen (const char *__string
, size_t __maxlen
)
392 __THROW __attribute_pure__
__nonnull ((1));
396 /* Return a string describing the meaning of the `errno' code in ERRNUM. */
397 extern char *strerror (int __errnum
) __THROW
;
399 /* Reentrant version of `strerror'.
400 There are 2 flavors of `strerror_r', GNU which returns the string
401 and may or may not use the supplied temporary buffer and POSIX one
402 which fills the string into the buffer.
403 To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
404 without -D_GNU_SOURCE is needed, otherwise the GNU version is
406 # if defined __USE_XOPEN2K && !defined __USE_GNU
407 /* Fill BUF with a string describing the meaning of the `errno' code in
409 # ifdef __REDIRECT_NTH
410 extern int __REDIRECT_NTH (strerror_r
,
411 (int __errnum
, char *__buf
, size_t __buflen
),
412 __xpg_strerror_r
) __nonnull ((2));
414 extern int __xpg_strerror_r (int __errnum
, char *__buf
, size_t __buflen
)
415 __THROW
__nonnull ((2));
416 # define strerror_r __xpg_strerror_r
419 /* If a temporary buffer is required, at most BUFLEN bytes of BUF will be
421 extern char *strerror_r (int __errnum
, char *__buf
, size_t __buflen
)
422 __THROW
__nonnull ((2)) __wur
;
426 #ifdef __USE_XOPEN2K8
427 /* Translate error number to string according to the locale L. */
428 extern char *strerror_l (int __errnum
, locale_t __l
) __THROW
;
432 # include <strings.h>
434 /* Set N bytes of S to 0. The compiler will not delete a call to this
435 function, even if S is dead after the call. */
436 extern void explicit_bzero (void *__s
, size_t __n
) __THROW
__nonnull ((1));
438 /* Return the next DELIM-delimited token from *STRINGP,
439 terminating it with a '\0', and update *STRINGP to point past it. */
440 extern char *strsep (char **__restrict __stringp
,
441 const char *__restrict __delim
)
442 __THROW
__nonnull ((1, 2));
445 #ifdef __USE_XOPEN2K8
446 /* Return a string describing the meaning of the signal number in SIG. */
447 extern char *strsignal (int __sig
) __THROW
;
449 /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
450 extern char *__stpcpy (char *__restrict __dest
, const char *__restrict __src
)
451 __THROW
__nonnull ((1, 2));
452 extern char *stpcpy (char *__restrict __dest
, const char *__restrict __src
)
453 __THROW
__nonnull ((1, 2));
455 /* Copy no more than N characters of SRC to DEST, returning the address of
456 the last character written into DEST. */
457 extern char *__stpncpy (char *__restrict __dest
,
458 const char *__restrict __src
, size_t __n
)
459 __THROW
__nonnull ((1, 2));
460 extern char *stpncpy (char *__restrict __dest
,
461 const char *__restrict __src
, size_t __n
)
462 __THROW
__nonnull ((1, 2));
466 /* Compare S1 and S2 as strings holding name & indices/version numbers. */
467 extern int strverscmp (const char *__s1
, const char *__s2
)
468 __THROW __attribute_pure__
__nonnull ((1, 2));
470 /* Sautee STRING briskly. */
471 extern char *strfry (char *__string
) __THROW
__nonnull ((1));
473 /* Frobnicate N bytes of S. */
474 extern void *memfrob (void *__s
, size_t __n
) __THROW
__nonnull ((1));
477 /* Return the file name within directory of FILENAME. We don't
478 declare the function if the `basename' macro is available (defined
479 in <libgen.h>) which makes the XPG version of this function
481 # ifdef __CORRECT_ISO_CPP_STRING_H_PROTO
482 extern "C++" char *basename (char *__filename
)
483 __THROW
__asm ("basename") __nonnull ((1));
484 extern "C++" const char *basename (const char *__filename
)
485 __THROW
__asm ("basename") __nonnull ((1));
487 extern char *basename (const char *__filename
) __THROW
__nonnull ((1));
492 #if __GNUC_PREREQ (3,4)
493 # if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
494 /* Functions with security checks. */
495 # include <bits/string_fortified.h>
501 #endif /* string.h */