1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2020 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
19 @PRAGMA_SYSTEM_HEADER@
23 #if defined _GL_ALREADY_INCLUDING_STRING_H
24 /* Special invocation convention:
25 - On OS X/NetBSD we have a sequence of nested includes
26 <string.h> -> <strings.h> -> "string.h"
27 In this situation system _chk variants due to -D_FORTIFY_SOURCE
28 might be used after any replacements defined here. */
30 #@INCLUDE_NEXT@ @NEXT_STRING_H@
33 /* Normal invocation convention. */
35 #ifndef _@GUARD_PREFIX@_STRING_H
37 #define _GL_ALREADY_INCLUDING_STRING_H
39 /* The include_next requires a split double-inclusion guard. */
40 #@INCLUDE_NEXT@ @NEXT_STRING_H@
42 #undef _GL_ALREADY_INCLUDING_STRING_H
44 #ifndef _@GUARD_PREFIX@_STRING_H
45 #define _@GUARD_PREFIX@_STRING_H
47 /* NetBSD 5.0 mis-defines NULL. */
50 /* MirBSD defines mbslen as a macro. */
51 #if @GNULIB_MBSLEN@ && defined __MirBSD__
55 /* The __attribute__ feature is available in gcc versions 2.5 and later.
56 The attribute __pure__ was added in gcc 2.96. */
57 #ifndef _GL_ATTRIBUTE_PURE
58 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__
59 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
61 # define _GL_ATTRIBUTE_PURE /* empty */
65 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
66 /* But in any case avoid namespace pollution on glibc systems. */
67 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
68 && ! defined __GLIBC__
72 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
74 /* The definition of _GL_ARG_NONNULL is copied here. */
76 /* The definition of _GL_WARN_ON_USE is copied here. */
79 /* Clear a block of memory. The compiler will not delete a call to
80 this function, even if the block is dead after the call. */
81 #if @GNULIB_EXPLICIT_BZERO@
82 # if ! @HAVE_EXPLICIT_BZERO@
83 _GL_FUNCDECL_SYS (explicit_bzero
, void,
84 (void *__dest
, size_t __n
) _GL_ARG_NONNULL ((1)));
86 _GL_CXXALIAS_SYS (explicit_bzero
, void, (void *__dest
, size_t __n
));
87 _GL_CXXALIASWARN (explicit_bzero
);
88 #elif defined GNULIB_POSIXCHECK
89 # undef explicit_bzero
90 # if HAVE_RAW_DECL_EXPLICIT_BZERO
91 _GL_WARN_ON_USE (explicit_bzero
, "explicit_bzero is unportable - "
92 "use gnulib module explicit_bzero for portability");
96 /* Find the index of the least-significant set bit. */
99 _GL_FUNCDECL_SYS (ffsl
, int, (long int i
));
101 _GL_CXXALIAS_SYS (ffsl
, int, (long int i
));
102 _GL_CXXALIASWARN (ffsl
);
103 #elif defined GNULIB_POSIXCHECK
105 # if HAVE_RAW_DECL_FFSL
106 _GL_WARN_ON_USE (ffsl
, "ffsl is not portable - use the ffsl module");
111 /* Find the index of the least-significant set bit. */
114 _GL_FUNCDECL_SYS (ffsll
, int, (long long int i
));
116 _GL_CXXALIAS_SYS (ffsll
, int, (long long int i
));
117 _GL_CXXALIASWARN (ffsll
);
118 #elif defined GNULIB_POSIXCHECK
120 # if HAVE_RAW_DECL_FFSLL
121 _GL_WARN_ON_USE (ffsll
, "ffsll is not portable - use the ffsll module");
126 #if defined _WIN32 && !defined __CYGWIN__
128 # define memccpy _memccpy
132 /* Return the first instance of C within N bytes of S, or NULL. */
134 # if @REPLACE_MEMCHR@
135 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
136 # define memchr rpl_memchr
138 _GL_FUNCDECL_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
140 _GL_ARG_NONNULL ((1)));
141 _GL_CXXALIAS_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
));
143 /* On some systems, this function is defined as an overloaded function:
144 extern "C" { const void * std::memchr (const void *, int, size_t); }
145 extern "C++" { void * std::memchr (void *, int, size_t); } */
146 _GL_CXXALIAS_SYS_CAST2 (memchr
,
147 void *, (void const *__s
, int __c
, size_t __n
),
148 void const *, (void const *__s
, int __c
, size_t __n
));
150 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
151 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
152 _GL_CXXALIASWARN1 (memchr
, void *, (void *__s
, int __c
, size_t __n
));
153 _GL_CXXALIASWARN1 (memchr
, void const *,
154 (void const *__s
, int __c
, size_t __n
));
155 # elif __GLIBC__ >= 2
156 _GL_CXXALIASWARN (memchr
);
158 #elif defined GNULIB_POSIXCHECK
160 /* Assume memchr is always declared. */
161 _GL_WARN_ON_USE (memchr
, "memchr has platform-specific bugs - "
162 "use gnulib module memchr for portability" );
165 /* Return the first occurrence of NEEDLE in HAYSTACK. */
167 # if @REPLACE_MEMMEM@
168 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
169 # define memmem rpl_memmem
171 _GL_FUNCDECL_RPL (memmem
, void *,
172 (void const *__haystack
, size_t __haystack_len
,
173 void const *__needle
, size_t __needle_len
)
175 _GL_ARG_NONNULL ((1, 3)));
176 _GL_CXXALIAS_RPL (memmem
, void *,
177 (void const *__haystack
, size_t __haystack_len
,
178 void const *__needle
, size_t __needle_len
));
180 # if ! @HAVE_DECL_MEMMEM@
181 _GL_FUNCDECL_SYS (memmem
, void *,
182 (void const *__haystack
, size_t __haystack_len
,
183 void const *__needle
, size_t __needle_len
)
185 _GL_ARG_NONNULL ((1, 3)));
187 _GL_CXXALIAS_SYS (memmem
, void *,
188 (void const *__haystack
, size_t __haystack_len
,
189 void const *__needle
, size_t __needle_len
));
191 _GL_CXXALIASWARN (memmem
);
192 #elif defined GNULIB_POSIXCHECK
194 # if HAVE_RAW_DECL_MEMMEM
195 _GL_WARN_ON_USE (memmem
, "memmem is unportable and often quadratic - "
196 "use gnulib module memmem-simple for portability, "
197 "and module memmem for speed" );
201 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
202 last written byte. */
204 # if ! @HAVE_MEMPCPY@
205 _GL_FUNCDECL_SYS (mempcpy
, void *,
206 (void *restrict __dest
, void const *restrict __src
,
208 _GL_ARG_NONNULL ((1, 2)));
210 _GL_CXXALIAS_SYS (mempcpy
, void *,
211 (void *restrict __dest
, void const *restrict __src
,
213 _GL_CXXALIASWARN (mempcpy
);
214 #elif defined GNULIB_POSIXCHECK
216 # if HAVE_RAW_DECL_MEMPCPY
217 _GL_WARN_ON_USE (mempcpy
, "mempcpy is unportable - "
218 "use gnulib module mempcpy for portability");
222 /* Search backwards through a block for a byte (specified as an int). */
224 # if ! @HAVE_DECL_MEMRCHR@
225 _GL_FUNCDECL_SYS (memrchr
, void *, (void const *, int, size_t)
227 _GL_ARG_NONNULL ((1)));
229 /* On some systems, this function is defined as an overloaded function:
230 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
231 extern "C++" { void * std::memrchr (void *, int, size_t); } */
232 _GL_CXXALIAS_SYS_CAST2 (memrchr
,
233 void *, (void const *, int, size_t),
234 void const *, (void const *, int, size_t));
235 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
236 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
237 _GL_CXXALIASWARN1 (memrchr
, void *, (void *, int, size_t));
238 _GL_CXXALIASWARN1 (memrchr
, void const *, (void const *, int, size_t));
240 _GL_CXXALIASWARN (memrchr
);
242 #elif defined GNULIB_POSIXCHECK
244 # if HAVE_RAW_DECL_MEMRCHR
245 _GL_WARN_ON_USE (memrchr
, "memrchr is unportable - "
246 "use gnulib module memrchr for portability");
250 /* Find the first occurrence of C in S. More efficient than
251 memchr(S,C,N), at the expense of undefined behavior if C does not
252 occur within N bytes. */
253 #if @GNULIB_RAWMEMCHR@
254 # if ! @HAVE_RAWMEMCHR@
255 _GL_FUNCDECL_SYS (rawmemchr
, void *, (void const *__s
, int __c_in
)
257 _GL_ARG_NONNULL ((1)));
259 /* On some systems, this function is defined as an overloaded function:
260 extern "C++" { const void * std::rawmemchr (const void *, int); }
261 extern "C++" { void * std::rawmemchr (void *, int); } */
262 _GL_CXXALIAS_SYS_CAST2 (rawmemchr
,
263 void *, (void const *__s
, int __c_in
),
264 void const *, (void const *__s
, int __c_in
));
265 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
266 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
267 _GL_CXXALIASWARN1 (rawmemchr
, void *, (void *__s
, int __c_in
));
268 _GL_CXXALIASWARN1 (rawmemchr
, void const *, (void const *__s
, int __c_in
));
270 _GL_CXXALIASWARN (rawmemchr
);
272 #elif defined GNULIB_POSIXCHECK
274 # if HAVE_RAW_DECL_RAWMEMCHR
275 _GL_WARN_ON_USE (rawmemchr
, "rawmemchr is unportable - "
276 "use gnulib module rawmemchr for portability");
280 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
283 _GL_FUNCDECL_SYS (stpcpy
, char *,
284 (char *restrict __dst
, char const *restrict __src
)
285 _GL_ARG_NONNULL ((1, 2)));
287 _GL_CXXALIAS_SYS (stpcpy
, char *,
288 (char *restrict __dst
, char const *restrict __src
));
289 _GL_CXXALIASWARN (stpcpy
);
290 #elif defined GNULIB_POSIXCHECK
292 # if HAVE_RAW_DECL_STPCPY
293 _GL_WARN_ON_USE (stpcpy
, "stpcpy is unportable - "
294 "use gnulib module stpcpy for portability");
298 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
299 last non-NUL byte written into DST. */
301 # if @REPLACE_STPNCPY@
302 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
304 # define stpncpy rpl_stpncpy
306 _GL_FUNCDECL_RPL (stpncpy
, char *,
307 (char *restrict __dst
, char const *restrict __src
,
309 _GL_ARG_NONNULL ((1, 2)));
310 _GL_CXXALIAS_RPL (stpncpy
, char *,
311 (char *restrict __dst
, char const *restrict __src
,
314 # if ! @HAVE_STPNCPY@
315 _GL_FUNCDECL_SYS (stpncpy
, char *,
316 (char *restrict __dst
, char const *restrict __src
,
318 _GL_ARG_NONNULL ((1, 2)));
320 _GL_CXXALIAS_SYS (stpncpy
, char *,
321 (char *restrict __dst
, char const *restrict __src
,
324 _GL_CXXALIASWARN (stpncpy
);
325 #elif defined GNULIB_POSIXCHECK
327 # if HAVE_RAW_DECL_STPNCPY
328 _GL_WARN_ON_USE (stpncpy
, "stpncpy is unportable - "
329 "use gnulib module stpncpy for portability");
333 #if defined GNULIB_POSIXCHECK
334 /* strchr() does not work with multibyte strings if the locale encoding is
335 GB18030 and the character to be searched is a digit. */
337 /* Assume strchr is always declared. */
338 _GL_WARN_ON_USE_CXX (strchr
,
339 const char *, char *, (const char *, int),
340 "strchr cannot work correctly on character strings "
341 "in some multibyte locales - "
342 "use mbschr if you care about internationalization");
345 /* Find the first occurrence of C in S or the final NUL byte. */
346 #if @GNULIB_STRCHRNUL@
347 # if @REPLACE_STRCHRNUL@
348 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
349 # define strchrnul rpl_strchrnul
351 _GL_FUNCDECL_RPL (strchrnul
, char *, (const char *__s
, int __c_in
)
353 _GL_ARG_NONNULL ((1)));
354 _GL_CXXALIAS_RPL (strchrnul
, char *,
355 (const char *str
, int ch
));
357 # if ! @HAVE_STRCHRNUL@
358 _GL_FUNCDECL_SYS (strchrnul
, char *, (char const *__s
, int __c_in
)
360 _GL_ARG_NONNULL ((1)));
362 /* On some systems, this function is defined as an overloaded function:
363 extern "C++" { const char * std::strchrnul (const char *, int); }
364 extern "C++" { char * std::strchrnul (char *, int); } */
365 _GL_CXXALIAS_SYS_CAST2 (strchrnul
,
366 char *, (char const *__s
, int __c_in
),
367 char const *, (char const *__s
, int __c_in
));
369 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
370 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
371 _GL_CXXALIASWARN1 (strchrnul
, char *, (char *__s
, int __c_in
));
372 _GL_CXXALIASWARN1 (strchrnul
, char const *, (char const *__s
, int __c_in
));
374 _GL_CXXALIASWARN (strchrnul
);
376 #elif defined GNULIB_POSIXCHECK
378 # if HAVE_RAW_DECL_STRCHRNUL
379 _GL_WARN_ON_USE (strchrnul
, "strchrnul is unportable - "
380 "use gnulib module strchrnul for portability");
384 /* Duplicate S, returning an identical malloc'd string. */
386 # if @REPLACE_STRDUP@
387 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
389 # define strdup rpl_strdup
391 _GL_FUNCDECL_RPL (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
392 _GL_CXXALIAS_RPL (strdup
, char *, (char const *__s
));
393 # elif defined _WIN32 && !defined __CYGWIN__
394 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
396 # define strdup _strdup
398 _GL_CXXALIAS_MDA (strdup
, char *, (char const *__s
));
400 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
401 /* strdup exists as a function and as a macro. Get rid of the macro. */
404 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
405 _GL_FUNCDECL_SYS (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
407 _GL_CXXALIAS_SYS (strdup
, char *, (char const *__s
));
409 _GL_CXXALIASWARN (strdup
);
410 #elif defined GNULIB_POSIXCHECK
412 # if HAVE_RAW_DECL_STRDUP
413 _GL_WARN_ON_USE (strdup
, "strdup is unportable - "
414 "use gnulib module strdup for portability");
416 #elif defined _WIN32 && !defined __CYGWIN__
418 # define strdup _strdup
421 /* Append no more than N characters from SRC onto DEST. */
423 # if @REPLACE_STRNCAT@
424 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
426 # define strncat rpl_strncat
428 _GL_FUNCDECL_RPL (strncat
, char *,
429 (char *restrict dest
, const char *restrict src
, size_t n
)
430 _GL_ARG_NONNULL ((1, 2)));
431 _GL_CXXALIAS_RPL (strncat
, char *,
432 (char *restrict dest
, const char *restrict src
, size_t n
));
434 _GL_CXXALIAS_SYS (strncat
, char *,
435 (char *restrict dest
, const char *restrict src
, size_t n
));
438 _GL_CXXALIASWARN (strncat
);
440 #elif defined GNULIB_POSIXCHECK
442 # if HAVE_RAW_DECL_STRNCAT
443 _GL_WARN_ON_USE (strncat
, "strncat is unportable - "
444 "use gnulib module strncat for portability");
448 /* Return a newly allocated copy of at most N bytes of STRING. */
450 # if @REPLACE_STRNDUP@
451 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
453 # define strndup rpl_strndup
455 _GL_FUNCDECL_RPL (strndup
, char *, (char const *__s
, size_t __n
)
456 _GL_ARG_NONNULL ((1)));
457 _GL_CXXALIAS_RPL (strndup
, char *, (char const *__s
, size_t __n
));
459 # if ! @HAVE_DECL_STRNDUP@
460 _GL_FUNCDECL_SYS (strndup
, char *, (char const *__s
, size_t __n
)
461 _GL_ARG_NONNULL ((1)));
463 _GL_CXXALIAS_SYS (strndup
, char *, (char const *__s
, size_t __n
));
465 _GL_CXXALIASWARN (strndup
);
466 #elif defined GNULIB_POSIXCHECK
468 # if HAVE_RAW_DECL_STRNDUP
469 _GL_WARN_ON_USE (strndup
, "strndup is unportable - "
470 "use gnulib module strndup for portability");
474 /* Find the length (number of bytes) of STRING, but scan at most
475 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
478 # if @REPLACE_STRNLEN@
479 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
481 # define strnlen rpl_strnlen
483 _GL_FUNCDECL_RPL (strnlen
, size_t, (char const *__s
, size_t __maxlen
)
485 _GL_ARG_NONNULL ((1)));
486 _GL_CXXALIAS_RPL (strnlen
, size_t, (char const *__s
, size_t __maxlen
));
488 # if ! @HAVE_DECL_STRNLEN@
489 _GL_FUNCDECL_SYS (strnlen
, size_t, (char const *__s
, size_t __maxlen
)
491 _GL_ARG_NONNULL ((1)));
493 _GL_CXXALIAS_SYS (strnlen
, size_t, (char const *__s
, size_t __maxlen
));
495 _GL_CXXALIASWARN (strnlen
);
496 #elif defined GNULIB_POSIXCHECK
498 # if HAVE_RAW_DECL_STRNLEN
499 _GL_WARN_ON_USE (strnlen
, "strnlen is unportable - "
500 "use gnulib module strnlen for portability");
504 #if defined GNULIB_POSIXCHECK
505 /* strcspn() assumes the second argument is a list of single-byte characters.
506 Even in this simple case, it does not work with multibyte strings if the
507 locale encoding is GB18030 and one of the characters to be searched is a
510 /* Assume strcspn is always declared. */
511 _GL_WARN_ON_USE (strcspn
, "strcspn cannot work correctly on character strings "
512 "in multibyte locales - "
513 "use mbscspn if you care about internationalization");
516 /* Find the first occurrence in S of any character in ACCEPT. */
518 # if ! @HAVE_STRPBRK@
519 _GL_FUNCDECL_SYS (strpbrk
, char *, (char const *__s
, char const *__accept
)
521 _GL_ARG_NONNULL ((1, 2)));
523 /* On some systems, this function is defined as an overloaded function:
524 extern "C" { const char * strpbrk (const char *, const char *); }
525 extern "C++" { char * strpbrk (char *, const char *); } */
526 _GL_CXXALIAS_SYS_CAST2 (strpbrk
,
527 char *, (char const *__s
, char const *__accept
),
528 const char *, (char const *__s
, char const *__accept
));
529 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
530 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
531 _GL_CXXALIASWARN1 (strpbrk
, char *, (char *__s
, char const *__accept
));
532 _GL_CXXALIASWARN1 (strpbrk
, char const *,
533 (char const *__s
, char const *__accept
));
534 # elif __GLIBC__ >= 2
535 _GL_CXXALIASWARN (strpbrk
);
537 # if defined GNULIB_POSIXCHECK
538 /* strpbrk() assumes the second argument is a list of single-byte characters.
539 Even in this simple case, it does not work with multibyte strings if the
540 locale encoding is GB18030 and one of the characters to be searched is a
543 _GL_WARN_ON_USE_CXX (strpbrk
,
544 const char *, char *, (const char *, const char *),
545 "strpbrk cannot work correctly on character strings "
546 "in multibyte locales - "
547 "use mbspbrk if you care about internationalization");
549 #elif defined GNULIB_POSIXCHECK
551 # if HAVE_RAW_DECL_STRPBRK
552 _GL_WARN_ON_USE_CXX (strpbrk
,
553 const char *, char *, (const char *, const char *),
554 "strpbrk is unportable - "
555 "use gnulib module strpbrk for portability");
559 #if defined GNULIB_POSIXCHECK
560 /* strspn() assumes the second argument is a list of single-byte characters.
561 Even in this simple case, it cannot work with multibyte strings. */
563 /* Assume strspn is always declared. */
564 _GL_WARN_ON_USE (strspn
, "strspn cannot work correctly on character strings "
565 "in multibyte locales - "
566 "use mbsspn if you care about internationalization");
569 #if defined GNULIB_POSIXCHECK
570 /* strrchr() does not work with multibyte strings if the locale encoding is
571 GB18030 and the character to be searched is a digit. */
573 /* Assume strrchr is always declared. */
574 _GL_WARN_ON_USE_CXX (strrchr
,
575 const char *, char *, (const char *, int),
576 "strrchr cannot work correctly on character strings "
577 "in some multibyte locales - "
578 "use mbsrchr if you care about internationalization");
581 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
582 If one is found, overwrite it with a NUL, and advance *STRINGP
583 to point to the next char after it. Otherwise, set *STRINGP to NULL.
584 If *STRINGP was already NULL, nothing happens.
585 Return the old value of *STRINGP.
587 This is a variant of strtok() that is multithread-safe and supports
590 Caveat: It modifies the original string.
591 Caveat: These functions cannot be used on constant strings.
592 Caveat: The identity of the delimiting character is lost.
593 Caveat: It doesn't work with multibyte strings unless all of the delimiter
594 characters are ASCII characters < 0x30.
596 See also strtok_r(). */
599 _GL_FUNCDECL_SYS (strsep
, char *,
600 (char **restrict __stringp
, char const *restrict __delim
)
601 _GL_ARG_NONNULL ((1, 2)));
603 _GL_CXXALIAS_SYS (strsep
, char *,
604 (char **restrict __stringp
, char const *restrict __delim
));
605 _GL_CXXALIASWARN (strsep
);
606 # if defined GNULIB_POSIXCHECK
608 _GL_WARN_ON_USE (strsep
, "strsep cannot work correctly on character strings "
609 "in multibyte locales - "
610 "use mbssep if you care about internationalization");
612 #elif defined GNULIB_POSIXCHECK
614 # if HAVE_RAW_DECL_STRSEP
615 _GL_WARN_ON_USE (strsep
, "strsep is unportable - "
616 "use gnulib module strsep for portability");
621 # if @REPLACE_STRSTR@
622 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
623 # define strstr rpl_strstr
625 _GL_FUNCDECL_RPL (strstr
, char *, (const char *haystack
, const char *needle
)
627 _GL_ARG_NONNULL ((1, 2)));
628 _GL_CXXALIAS_RPL (strstr
, char *, (const char *haystack
, const char *needle
));
630 /* On some systems, this function is defined as an overloaded function:
631 extern "C++" { const char * strstr (const char *, const char *); }
632 extern "C++" { char * strstr (char *, const char *); } */
633 _GL_CXXALIAS_SYS_CAST2 (strstr
,
634 char *, (const char *haystack
, const char *needle
),
635 const char *, (const char *haystack
, const char *needle
));
637 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
638 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
639 _GL_CXXALIASWARN1 (strstr
, char *, (char *haystack
, const char *needle
));
640 _GL_CXXALIASWARN1 (strstr
, const char *,
641 (const char *haystack
, const char *needle
));
642 # elif __GLIBC__ >= 2
643 _GL_CXXALIASWARN (strstr
);
645 #elif defined GNULIB_POSIXCHECK
646 /* strstr() does not work with multibyte strings if the locale encoding is
647 different from UTF-8:
648 POSIX says that it operates on "strings", and "string" in POSIX is defined
649 as a sequence of bytes, not of characters. */
651 /* Assume strstr is always declared. */
652 _GL_WARN_ON_USE (strstr
, "strstr is quadratic on many systems, and cannot "
653 "work correctly on character strings in most "
654 "multibyte locales - "
655 "use mbsstr if you care about internationalization, "
656 "or use strstr if you care about speed");
659 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
661 #if @GNULIB_STRCASESTR@
662 # if @REPLACE_STRCASESTR@
663 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
664 # define strcasestr rpl_strcasestr
666 _GL_FUNCDECL_RPL (strcasestr
, char *,
667 (const char *haystack
, const char *needle
)
669 _GL_ARG_NONNULL ((1, 2)));
670 _GL_CXXALIAS_RPL (strcasestr
, char *,
671 (const char *haystack
, const char *needle
));
673 # if ! @HAVE_STRCASESTR@
674 _GL_FUNCDECL_SYS (strcasestr
, char *,
675 (const char *haystack
, const char *needle
)
677 _GL_ARG_NONNULL ((1, 2)));
679 /* On some systems, this function is defined as an overloaded function:
680 extern "C++" { const char * strcasestr (const char *, const char *); }
681 extern "C++" { char * strcasestr (char *, const char *); } */
682 _GL_CXXALIAS_SYS_CAST2 (strcasestr
,
683 char *, (const char *haystack
, const char *needle
),
684 const char *, (const char *haystack
, const char *needle
));
686 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
687 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
688 _GL_CXXALIASWARN1 (strcasestr
, char *, (char *haystack
, const char *needle
));
689 _GL_CXXALIASWARN1 (strcasestr
, const char *,
690 (const char *haystack
, const char *needle
));
692 _GL_CXXALIASWARN (strcasestr
);
694 #elif defined GNULIB_POSIXCHECK
695 /* strcasestr() does not work with multibyte strings:
696 It is a glibc extension, and glibc implements it only for unibyte
699 # if HAVE_RAW_DECL_STRCASESTR
700 _GL_WARN_ON_USE (strcasestr
, "strcasestr does work correctly on character "
701 "strings in multibyte locales - "
702 "use mbscasestr if you care about "
703 "internationalization, or use c-strcasestr if you want "
704 "a locale independent function");
708 /* Parse S into tokens separated by characters in DELIM.
709 If S is NULL, the saved pointer in SAVE_PTR is used as
710 the next starting point. For example:
711 char s[] = "-abc-=-def";
713 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
714 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
715 x = strtok_r(NULL, "=", &sp); // x = NULL
718 This is a variant of strtok() that is multithread-safe.
720 For the POSIX documentation for this function, see:
721 https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html
723 Caveat: It modifies the original string.
724 Caveat: These functions cannot be used on constant strings.
725 Caveat: The identity of the delimiting character is lost.
726 Caveat: It doesn't work with multibyte strings unless all of the delimiter
727 characters are ASCII characters < 0x30.
729 See also strsep(). */
730 #if @GNULIB_STRTOK_R@
731 # if @REPLACE_STRTOK_R@
732 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
734 # define strtok_r rpl_strtok_r
736 _GL_FUNCDECL_RPL (strtok_r
, char *,
737 (char *restrict s
, char const *restrict delim
,
738 char **restrict save_ptr
)
739 _GL_ARG_NONNULL ((2, 3)));
740 _GL_CXXALIAS_RPL (strtok_r
, char *,
741 (char *restrict s
, char const *restrict delim
,
742 char **restrict save_ptr
));
744 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
747 # if ! @HAVE_DECL_STRTOK_R@
748 _GL_FUNCDECL_SYS (strtok_r
, char *,
749 (char *restrict s
, char const *restrict delim
,
750 char **restrict save_ptr
)
751 _GL_ARG_NONNULL ((2, 3)));
753 _GL_CXXALIAS_SYS (strtok_r
, char *,
754 (char *restrict s
, char const *restrict delim
,
755 char **restrict save_ptr
));
757 _GL_CXXALIASWARN (strtok_r
);
758 # if defined GNULIB_POSIXCHECK
759 _GL_WARN_ON_USE (strtok_r
, "strtok_r cannot work correctly on character "
760 "strings in multibyte locales - "
761 "use mbstok_r if you care about internationalization");
763 #elif defined GNULIB_POSIXCHECK
765 # if HAVE_RAW_DECL_STRTOK_R
766 _GL_WARN_ON_USE (strtok_r
, "strtok_r is unportable - "
767 "use gnulib module strtok_r for portability");
772 /* The following functions are not specified by POSIX. They are gnulib
776 /* Return the number of multibyte characters in the character string STRING.
777 This considers multibyte characters, unlike strlen, which counts bytes. */
778 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
781 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
782 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
783 # define mbslen rpl_mbslen
785 _GL_FUNCDECL_RPL (mbslen
, size_t, (const char *string
)
787 _GL_ARG_NONNULL ((1)));
788 _GL_CXXALIAS_RPL (mbslen
, size_t, (const char *string
));
790 _GL_FUNCDECL_SYS (mbslen
, size_t, (const char *string
)
792 _GL_ARG_NONNULL ((1)));
793 _GL_CXXALIAS_SYS (mbslen
, size_t, (const char *string
));
795 _GL_CXXALIASWARN (mbslen
);
799 /* Return the number of multibyte characters in the character string starting
800 at STRING and ending at STRING + LEN. */
801 _GL_EXTERN_C
size_t mbsnlen (const char *string
, size_t len
)
803 _GL_ARG_NONNULL ((1));
807 /* Locate the first single-byte character C in the character string STRING,
808 and return a pointer to it. Return NULL if C is not found in STRING.
809 Unlike strchr(), this function works correctly in multibyte locales with
810 encodings such as GB18030. */
812 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
813 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
815 _GL_FUNCDECL_RPL (mbschr
, char *, (const char *string
, int c
)
817 _GL_ARG_NONNULL ((1)));
818 _GL_CXXALIAS_RPL (mbschr
, char *, (const char *string
, int c
));
820 _GL_FUNCDECL_SYS (mbschr
, char *, (const char *string
, int c
)
822 _GL_ARG_NONNULL ((1)));
823 _GL_CXXALIAS_SYS (mbschr
, char *, (const char *string
, int c
));
825 _GL_CXXALIASWARN (mbschr
);
829 /* Locate the last single-byte character C in the character string STRING,
830 and return a pointer to it. Return NULL if C is not found in STRING.
831 Unlike strrchr(), this function works correctly in multibyte locales with
832 encodings such as GB18030. */
833 # if defined __hpux || defined __INTERIX
834 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
835 # define mbsrchr rpl_mbsrchr /* avoid collision with system function */
837 _GL_FUNCDECL_RPL (mbsrchr
, char *, (const char *string
, int c
)
839 _GL_ARG_NONNULL ((1)));
840 _GL_CXXALIAS_RPL (mbsrchr
, char *, (const char *string
, int c
));
842 _GL_FUNCDECL_SYS (mbsrchr
, char *, (const char *string
, int c
)
844 _GL_ARG_NONNULL ((1)));
845 _GL_CXXALIAS_SYS (mbsrchr
, char *, (const char *string
, int c
));
847 _GL_CXXALIASWARN (mbsrchr
);
851 /* Find the first occurrence of the character string NEEDLE in the character
852 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
853 Unlike strstr(), this function works correctly in multibyte locales with
854 encodings different from UTF-8. */
855 _GL_EXTERN_C
char * mbsstr (const char *haystack
, const char *needle
)
857 _GL_ARG_NONNULL ((1, 2));
860 #if @GNULIB_MBSCASECMP@
861 /* Compare the character strings S1 and S2, ignoring case, returning less than,
862 equal to or greater than zero if S1 is lexicographically less than, equal to
864 Note: This function may, in multibyte locales, return 0 for strings of
866 Unlike strcasecmp(), this function works correctly in multibyte locales. */
867 _GL_EXTERN_C
int mbscasecmp (const char *s1
, const char *s2
)
869 _GL_ARG_NONNULL ((1, 2));
872 #if @GNULIB_MBSNCASECMP@
873 /* Compare the initial segment of the character string S1 consisting of at most
874 N characters with the initial segment of the character string S2 consisting
875 of at most N characters, ignoring case, returning less than, equal to or
876 greater than zero if the initial segment of S1 is lexicographically less
877 than, equal to or greater than the initial segment of S2.
878 Note: This function may, in multibyte locales, return 0 for initial segments
879 of different lengths!
880 Unlike strncasecmp(), this function works correctly in multibyte locales.
881 But beware that N is not a byte count but a character count! */
882 _GL_EXTERN_C
int mbsncasecmp (const char *s1
, const char *s2
, size_t n
)
884 _GL_ARG_NONNULL ((1, 2));
887 #if @GNULIB_MBSPCASECMP@
888 /* Compare the initial segment of the character string STRING consisting of
889 at most mbslen (PREFIX) characters with the character string PREFIX,
890 ignoring case. If the two match, return a pointer to the first byte
891 after this prefix in STRING. Otherwise, return NULL.
892 Note: This function may, in multibyte locales, return non-NULL if STRING
893 is of smaller length than PREFIX!
894 Unlike strncasecmp(), this function works correctly in multibyte
896 _GL_EXTERN_C
char * mbspcasecmp (const char *string
, const char *prefix
)
898 _GL_ARG_NONNULL ((1, 2));
901 #if @GNULIB_MBSCASESTR@
902 /* Find the first occurrence of the character string NEEDLE in the character
903 string HAYSTACK, using case-insensitive comparison.
904 Note: This function may, in multibyte locales, return success even if
905 strlen (haystack) < strlen (needle) !
906 Unlike strcasestr(), this function works correctly in multibyte locales. */
907 _GL_EXTERN_C
char * mbscasestr (const char *haystack
, const char *needle
)
909 _GL_ARG_NONNULL ((1, 2));
913 /* Find the first occurrence in the character string STRING of any character
914 in the character string ACCEPT. Return the number of bytes from the
915 beginning of the string to this occurrence, or to the end of the string
917 Unlike strcspn(), this function works correctly in multibyte locales. */
918 _GL_EXTERN_C
size_t mbscspn (const char *string
, const char *accept
)
920 _GL_ARG_NONNULL ((1, 2));
924 /* Find the first occurrence in the character string STRING of any character
925 in the character string ACCEPT. Return the pointer to it, or NULL if none
927 Unlike strpbrk(), this function works correctly in multibyte locales. */
929 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
930 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
932 _GL_FUNCDECL_RPL (mbspbrk
, char *, (const char *string
, const char *accept
)
934 _GL_ARG_NONNULL ((1, 2)));
935 _GL_CXXALIAS_RPL (mbspbrk
, char *, (const char *string
, const char *accept
));
937 _GL_FUNCDECL_SYS (mbspbrk
, char *, (const char *string
, const char *accept
)
939 _GL_ARG_NONNULL ((1, 2)));
940 _GL_CXXALIAS_SYS (mbspbrk
, char *, (const char *string
, const char *accept
));
942 _GL_CXXALIASWARN (mbspbrk
);
946 /* Find the first occurrence in the character string STRING of any character
947 not in the character string REJECT. Return the number of bytes from the
948 beginning of the string to this occurrence, or to the end of the string
950 Unlike strspn(), this function works correctly in multibyte locales. */
951 _GL_EXTERN_C
size_t mbsspn (const char *string
, const char *reject
)
953 _GL_ARG_NONNULL ((1, 2));
957 /* Search the next delimiter (multibyte character listed in the character
958 string DELIM) starting at the character string *STRINGP.
959 If one is found, overwrite it with a NUL, and advance *STRINGP to point
960 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
961 If *STRINGP was already NULL, nothing happens.
962 Return the old value of *STRINGP.
964 This is a variant of mbstok_r() that supports empty fields.
966 Caveat: It modifies the original string.
967 Caveat: These functions cannot be used on constant strings.
968 Caveat: The identity of the delimiting character is lost.
970 See also mbstok_r(). */
971 _GL_EXTERN_C
char * mbssep (char **stringp
, const char *delim
)
972 _GL_ARG_NONNULL ((1, 2));
975 #if @GNULIB_MBSTOK_R@
976 /* Parse the character string STRING into tokens separated by characters in
977 the character string DELIM.
978 If STRING is NULL, the saved pointer in SAVE_PTR is used as
979 the next starting point. For example:
980 char s[] = "-abc-=-def";
982 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
983 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
984 x = mbstok_r(NULL, "=", &sp); // x = NULL
987 Caveat: It modifies the original string.
988 Caveat: These functions cannot be used on constant strings.
989 Caveat: The identity of the delimiting character is lost.
991 See also mbssep(). */
992 _GL_EXTERN_C
char * mbstok_r (char *restrict string
, const char *delim
,
994 _GL_ARG_NONNULL ((2, 3));
997 /* Map any int, typically from errno, into an error message. */
998 #if @GNULIB_STRERROR@
999 # if @REPLACE_STRERROR@
1000 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1002 # define strerror rpl_strerror
1004 _GL_FUNCDECL_RPL (strerror
, char *, (int));
1005 _GL_CXXALIAS_RPL (strerror
, char *, (int));
1007 _GL_CXXALIAS_SYS (strerror
, char *, (int));
1010 _GL_CXXALIASWARN (strerror
);
1012 #elif defined GNULIB_POSIXCHECK
1014 /* Assume strerror is always declared. */
1015 _GL_WARN_ON_USE (strerror
, "strerror is unportable - "
1016 "use gnulib module strerror to guarantee non-NULL result");
1019 /* Map any int, typically from errno, into an error message. Multithread-safe.
1020 Uses the POSIX declaration, not the glibc declaration. */
1021 #if @GNULIB_STRERROR_R@
1022 # if @REPLACE_STRERROR_R@
1023 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1025 # define strerror_r rpl_strerror_r
1027 _GL_FUNCDECL_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
1028 _GL_ARG_NONNULL ((2)));
1029 _GL_CXXALIAS_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
1031 # if !@HAVE_DECL_STRERROR_R@
1032 _GL_FUNCDECL_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
1033 _GL_ARG_NONNULL ((2)));
1035 _GL_CXXALIAS_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
1037 # if @HAVE_DECL_STRERROR_R@
1038 _GL_CXXALIASWARN (strerror_r
);
1040 #elif defined GNULIB_POSIXCHECK
1042 # if HAVE_RAW_DECL_STRERROR_R
1043 _GL_WARN_ON_USE (strerror_r
, "strerror_r is unportable - "
1044 "use gnulib module strerror_r-posix for portability");
1048 /* Return the name of the system error code ERRNUM. */
1049 #if @GNULIB_STRERRORNAME_NP@
1050 # if @REPLACE_STRERRORNAME_NP@
1051 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1052 # undef strerrorname_np
1053 # define strerrorname_np rpl_strerrorname_np
1055 _GL_FUNCDECL_RPL (strerrorname_np
, const char *, (int errnum
));
1056 _GL_CXXALIAS_RPL (strerrorname_np
, const char *, (int errnum
));
1058 # if !@HAVE_STRERRORNAME_NP@
1059 _GL_FUNCDECL_SYS (strerrorname_np
, const char *, (int errnum
));
1061 _GL_CXXALIAS_SYS (strerrorname_np
, const char *, (int errnum
));
1063 _GL_CXXALIASWARN (strerrorname_np
);
1064 #elif defined GNULIB_POSIXCHECK
1065 # undef strerrorname_np
1066 # if HAVE_RAW_DECL_STRERRORNAME_NP
1067 _GL_WARN_ON_USE (strerrorname_np
, "strerrorname_np is unportable - "
1068 "use gnulib module strerrorname_np for portability");
1072 /* Return an abbreviation string for the signal number SIG. */
1073 #if @GNULIB_SIGABBREV_NP@
1074 # if ! @HAVE_SIGABBREV_NP@
1075 _GL_FUNCDECL_SYS (sigabbrev_np
, const char *, (int sig
));
1077 _GL_CXXALIAS_SYS (sigabbrev_np
, const char *, (int sig
));
1078 _GL_CXXALIASWARN (sigabbrev_np
);
1079 #elif defined GNULIB_POSIXCHECK
1080 # undef sigabbrev_np
1081 # if HAVE_RAW_DECL_SIGABBREV_NP
1082 _GL_WARN_ON_USE (sigabbrev_np
, "sigabbrev_np is unportable - "
1083 "use gnulib module sigabbrev_np for portability");
1087 /* Return an English description string for the signal number SIG. */
1088 #if @GNULIB_SIGDESCR_NP@
1089 # if ! @HAVE_SIGDESCR_NP@
1090 _GL_FUNCDECL_SYS (sigdescr_np
, const char *, (int sig
));
1092 _GL_CXXALIAS_SYS (sigdescr_np
, const char *, (int sig
));
1093 _GL_CXXALIASWARN (sigdescr_np
);
1094 #elif defined GNULIB_POSIXCHECK
1096 # if HAVE_RAW_DECL_SIGDESCR_NP
1097 _GL_WARN_ON_USE (sigdescr_np
, "sigdescr_np is unportable - "
1098 "use gnulib module sigdescr_np for portability");
1102 #if @GNULIB_STRSIGNAL@
1103 # if @REPLACE_STRSIGNAL@
1104 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1105 # define strsignal rpl_strsignal
1107 _GL_FUNCDECL_RPL (strsignal
, char *, (int __sig
));
1108 _GL_CXXALIAS_RPL (strsignal
, char *, (int __sig
));
1110 # if ! @HAVE_DECL_STRSIGNAL@
1111 _GL_FUNCDECL_SYS (strsignal
, char *, (int __sig
));
1113 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
1115 _GL_CXXALIAS_SYS_CAST (strsignal
, char *, (int __sig
));
1117 _GL_CXXALIASWARN (strsignal
);
1118 #elif defined GNULIB_POSIXCHECK
1120 # if HAVE_RAW_DECL_STRSIGNAL
1121 _GL_WARN_ON_USE (strsignal
, "strsignal is unportable - "
1122 "use gnulib module strsignal for portability");
1126 #if @GNULIB_STRVERSCMP@
1127 # if !@HAVE_STRVERSCMP@
1128 _GL_FUNCDECL_SYS (strverscmp
, int, (const char *, const char *)
1130 _GL_ARG_NONNULL ((1, 2)));
1132 _GL_CXXALIAS_SYS (strverscmp
, int, (const char *, const char *));
1133 _GL_CXXALIASWARN (strverscmp
);
1134 #elif defined GNULIB_POSIXCHECK
1136 # if HAVE_RAW_DECL_STRVERSCMP
1137 _GL_WARN_ON_USE (strverscmp
, "strverscmp is unportable - "
1138 "use gnulib module strverscmp for portability");
1143 #endif /* _@GUARD_PREFIX@_STRING_H */
1144 #endif /* _@GUARD_PREFIX@_STRING_H */