1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2016 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 3, 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 <http://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 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
58 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
60 # define _GL_ATTRIBUTE_PURE /* empty */
63 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
64 /* But in any case avoid namespace pollution on glibc systems. */
65 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
66 && ! defined __GLIBC__
70 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
72 /* The definition of _GL_ARG_NONNULL is copied here. */
74 /* The definition of _GL_WARN_ON_USE is copied here. */
77 /* Find the index of the least-significant set bit. */
80 _GL_FUNCDECL_SYS (ffsl
, int, (long int i
));
82 _GL_CXXALIAS_SYS (ffsl
, int, (long int i
));
83 _GL_CXXALIASWARN (ffsl
);
84 #elif defined GNULIB_POSIXCHECK
86 # if HAVE_RAW_DECL_FFSL
87 _GL_WARN_ON_USE (ffsl
, "ffsl is not portable - use the ffsl module");
92 /* Find the index of the least-significant set bit. */
95 _GL_FUNCDECL_SYS (ffsll
, int, (long long int i
));
97 _GL_CXXALIAS_SYS (ffsll
, int, (long long int i
));
98 _GL_CXXALIASWARN (ffsll
);
99 #elif defined GNULIB_POSIXCHECK
101 # if HAVE_RAW_DECL_FFSLL
102 _GL_WARN_ON_USE (ffsll
, "ffsll is not portable - use the ffsll module");
107 /* Return the first instance of C within N bytes of S, or NULL. */
109 # if @REPLACE_MEMCHR@
110 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
111 # define memchr rpl_memchr
113 _GL_FUNCDECL_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
115 _GL_ARG_NONNULL ((1)));
116 _GL_CXXALIAS_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
));
119 _GL_FUNCDECL_SYS (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
121 _GL_ARG_NONNULL ((1)));
123 /* On some systems, this function is defined as an overloaded function:
124 extern "C" { const void * std::memchr (const void *, int, size_t); }
125 extern "C++" { void * std::memchr (void *, int, size_t); } */
126 _GL_CXXALIAS_SYS_CAST2 (memchr
,
127 void *, (void const *__s
, int __c
, size_t __n
),
128 void const *, (void const *__s
, int __c
, size_t __n
));
130 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
131 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
132 _GL_CXXALIASWARN1 (memchr
, void *, (void *__s
, int __c
, size_t __n
));
133 _GL_CXXALIASWARN1 (memchr
, void const *,
134 (void const *__s
, int __c
, size_t __n
));
136 _GL_CXXALIASWARN (memchr
);
138 #elif defined GNULIB_POSIXCHECK
140 /* Assume memchr is always declared. */
141 _GL_WARN_ON_USE (memchr
, "memchr has platform-specific bugs - "
142 "use gnulib module memchr for portability" );
145 /* Return the first occurrence of NEEDLE in HAYSTACK. */
147 # if @REPLACE_MEMMEM@
148 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
149 # define memmem rpl_memmem
151 _GL_FUNCDECL_RPL (memmem
, void *,
152 (void const *__haystack
, size_t __haystack_len
,
153 void const *__needle
, size_t __needle_len
)
155 _GL_ARG_NONNULL ((1, 3)));
156 _GL_CXXALIAS_RPL (memmem
, void *,
157 (void const *__haystack
, size_t __haystack_len
,
158 void const *__needle
, size_t __needle_len
));
160 # if ! @HAVE_DECL_MEMMEM@
161 _GL_FUNCDECL_SYS (memmem
, void *,
162 (void const *__haystack
, size_t __haystack_len
,
163 void const *__needle
, size_t __needle_len
)
165 _GL_ARG_NONNULL ((1, 3)));
167 _GL_CXXALIAS_SYS (memmem
, void *,
168 (void const *__haystack
, size_t __haystack_len
,
169 void const *__needle
, size_t __needle_len
));
171 _GL_CXXALIASWARN (memmem
);
172 #elif defined GNULIB_POSIXCHECK
174 # if HAVE_RAW_DECL_MEMMEM
175 _GL_WARN_ON_USE (memmem
, "memmem is unportable and often quadratic - "
176 "use gnulib module memmem-simple for portability, "
177 "and module memmem for speed" );
181 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
182 last written byte. */
184 # if ! @HAVE_MEMPCPY@
185 _GL_FUNCDECL_SYS (mempcpy
, void *,
186 (void *restrict __dest
, void const *restrict __src
,
188 _GL_ARG_NONNULL ((1, 2)));
190 _GL_CXXALIAS_SYS (mempcpy
, void *,
191 (void *restrict __dest
, void const *restrict __src
,
193 _GL_CXXALIASWARN (mempcpy
);
194 #elif defined GNULIB_POSIXCHECK
196 # if HAVE_RAW_DECL_MEMPCPY
197 _GL_WARN_ON_USE (mempcpy
, "mempcpy is unportable - "
198 "use gnulib module mempcpy for portability");
202 /* Search backwards through a block for a byte (specified as an int). */
204 # if ! @HAVE_DECL_MEMRCHR@
205 _GL_FUNCDECL_SYS (memrchr
, void *, (void const *, int, size_t)
207 _GL_ARG_NONNULL ((1)));
209 /* On some systems, this function is defined as an overloaded function:
210 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
211 extern "C++" { void * std::memrchr (void *, int, size_t); } */
212 _GL_CXXALIAS_SYS_CAST2 (memrchr
,
213 void *, (void const *, int, size_t),
214 void const *, (void const *, int, size_t));
215 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
216 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
217 _GL_CXXALIASWARN1 (memrchr
, void *, (void *, int, size_t));
218 _GL_CXXALIASWARN1 (memrchr
, void const *, (void const *, int, size_t));
220 _GL_CXXALIASWARN (memrchr
);
222 #elif defined GNULIB_POSIXCHECK
224 # if HAVE_RAW_DECL_MEMRCHR
225 _GL_WARN_ON_USE (memrchr
, "memrchr is unportable - "
226 "use gnulib module memrchr for portability");
230 /* Find the first occurrence of C in S. More efficient than
231 memchr(S,C,N), at the expense of undefined behavior if C does not
232 occur within N bytes. */
233 #if @GNULIB_RAWMEMCHR@
234 # if ! @HAVE_RAWMEMCHR@
235 _GL_FUNCDECL_SYS (rawmemchr
, void *, (void const *__s
, int __c_in
)
237 _GL_ARG_NONNULL ((1)));
239 /* On some systems, this function is defined as an overloaded function:
240 extern "C++" { const void * std::rawmemchr (const void *, int); }
241 extern "C++" { void * std::rawmemchr (void *, int); } */
242 _GL_CXXALIAS_SYS_CAST2 (rawmemchr
,
243 void *, (void const *__s
, int __c_in
),
244 void const *, (void const *__s
, int __c_in
));
245 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
246 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
247 _GL_CXXALIASWARN1 (rawmemchr
, void *, (void *__s
, int __c_in
));
248 _GL_CXXALIASWARN1 (rawmemchr
, void const *, (void const *__s
, int __c_in
));
250 _GL_CXXALIASWARN (rawmemchr
);
252 #elif defined GNULIB_POSIXCHECK
254 # if HAVE_RAW_DECL_RAWMEMCHR
255 _GL_WARN_ON_USE (rawmemchr
, "rawmemchr is unportable - "
256 "use gnulib module rawmemchr for portability");
260 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
263 _GL_FUNCDECL_SYS (stpcpy
, char *,
264 (char *restrict __dst
, char const *restrict __src
)
265 _GL_ARG_NONNULL ((1, 2)));
267 _GL_CXXALIAS_SYS (stpcpy
, char *,
268 (char *restrict __dst
, char const *restrict __src
));
269 _GL_CXXALIASWARN (stpcpy
);
270 #elif defined GNULIB_POSIXCHECK
272 # if HAVE_RAW_DECL_STPCPY
273 _GL_WARN_ON_USE (stpcpy
, "stpcpy is unportable - "
274 "use gnulib module stpcpy for portability");
278 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
279 last non-NUL byte written into DST. */
281 # if @REPLACE_STPNCPY@
282 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
284 # define stpncpy rpl_stpncpy
286 _GL_FUNCDECL_RPL (stpncpy
, char *,
287 (char *restrict __dst
, char const *restrict __src
,
289 _GL_ARG_NONNULL ((1, 2)));
290 _GL_CXXALIAS_RPL (stpncpy
, char *,
291 (char *restrict __dst
, char const *restrict __src
,
294 # if ! @HAVE_STPNCPY@
295 _GL_FUNCDECL_SYS (stpncpy
, char *,
296 (char *restrict __dst
, char const *restrict __src
,
298 _GL_ARG_NONNULL ((1, 2)));
300 _GL_CXXALIAS_SYS (stpncpy
, char *,
301 (char *restrict __dst
, char const *restrict __src
,
304 _GL_CXXALIASWARN (stpncpy
);
305 #elif defined GNULIB_POSIXCHECK
307 # if HAVE_RAW_DECL_STPNCPY
308 _GL_WARN_ON_USE (stpncpy
, "stpncpy is unportable - "
309 "use gnulib module stpncpy for portability");
313 #if defined GNULIB_POSIXCHECK
314 /* strchr() does not work with multibyte strings if the locale encoding is
315 GB18030 and the character to be searched is a digit. */
317 /* Assume strchr is always declared. */
318 _GL_WARN_ON_USE (strchr
, "strchr cannot work correctly on character strings "
319 "in some multibyte locales - "
320 "use mbschr if you care about internationalization");
323 /* Find the first occurrence of C in S or the final NUL byte. */
324 #if @GNULIB_STRCHRNUL@
325 # if @REPLACE_STRCHRNUL@
326 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
327 # define strchrnul rpl_strchrnul
329 _GL_FUNCDECL_RPL (strchrnul
, char *, (const char *__s
, int __c_in
)
331 _GL_ARG_NONNULL ((1)));
332 _GL_CXXALIAS_RPL (strchrnul
, char *,
333 (const char *str
, int ch
));
335 # if ! @HAVE_STRCHRNUL@
336 _GL_FUNCDECL_SYS (strchrnul
, char *, (char const *__s
, int __c_in
)
338 _GL_ARG_NONNULL ((1)));
340 /* On some systems, this function is defined as an overloaded function:
341 extern "C++" { const char * std::strchrnul (const char *, int); }
342 extern "C++" { char * std::strchrnul (char *, int); } */
343 _GL_CXXALIAS_SYS_CAST2 (strchrnul
,
344 char *, (char const *__s
, int __c_in
),
345 char const *, (char const *__s
, int __c_in
));
347 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
348 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
349 _GL_CXXALIASWARN1 (strchrnul
, char *, (char *__s
, int __c_in
));
350 _GL_CXXALIASWARN1 (strchrnul
, char const *, (char const *__s
, int __c_in
));
352 _GL_CXXALIASWARN (strchrnul
);
354 #elif defined GNULIB_POSIXCHECK
356 # if HAVE_RAW_DECL_STRCHRNUL
357 _GL_WARN_ON_USE (strchrnul
, "strchrnul is unportable - "
358 "use gnulib module strchrnul for portability");
362 /* Duplicate S, returning an identical malloc'd string. */
364 # if @REPLACE_STRDUP@
365 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
367 # define strdup rpl_strdup
369 _GL_FUNCDECL_RPL (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
370 _GL_CXXALIAS_RPL (strdup
, char *, (char const *__s
));
372 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
373 /* strdup exists as a function and as a macro. Get rid of the macro. */
376 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
377 _GL_FUNCDECL_SYS (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
379 _GL_CXXALIAS_SYS (strdup
, char *, (char const *__s
));
381 _GL_CXXALIASWARN (strdup
);
382 #elif defined GNULIB_POSIXCHECK
384 # if HAVE_RAW_DECL_STRDUP
385 _GL_WARN_ON_USE (strdup
, "strdup is unportable - "
386 "use gnulib module strdup for portability");
390 /* Append no more than N characters from SRC onto DEST. */
392 # if @REPLACE_STRNCAT@
393 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
395 # define strncat rpl_strncat
397 _GL_FUNCDECL_RPL (strncat
, char *, (char *dest
, const char *src
, size_t n
)
398 _GL_ARG_NONNULL ((1, 2)));
399 _GL_CXXALIAS_RPL (strncat
, char *, (char *dest
, const char *src
, size_t n
));
401 _GL_CXXALIAS_SYS (strncat
, char *, (char *dest
, const char *src
, size_t n
));
403 _GL_CXXALIASWARN (strncat
);
404 #elif defined GNULIB_POSIXCHECK
406 # if HAVE_RAW_DECL_STRNCAT
407 _GL_WARN_ON_USE (strncat
, "strncat is unportable - "
408 "use gnulib module strncat for portability");
412 /* Return a newly allocated copy of at most N bytes of STRING. */
414 # if @REPLACE_STRNDUP@
415 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
417 # define strndup rpl_strndup
419 _GL_FUNCDECL_RPL (strndup
, char *, (char const *__string
, size_t __n
)
420 _GL_ARG_NONNULL ((1)));
421 _GL_CXXALIAS_RPL (strndup
, char *, (char const *__string
, size_t __n
));
423 # if ! @HAVE_DECL_STRNDUP@
424 _GL_FUNCDECL_SYS (strndup
, char *, (char const *__string
, size_t __n
)
425 _GL_ARG_NONNULL ((1)));
427 _GL_CXXALIAS_SYS (strndup
, char *, (char const *__string
, size_t __n
));
429 _GL_CXXALIASWARN (strndup
);
430 #elif defined GNULIB_POSIXCHECK
432 # if HAVE_RAW_DECL_STRNDUP
433 _GL_WARN_ON_USE (strndup
, "strndup is unportable - "
434 "use gnulib module strndup for portability");
438 /* Find the length (number of bytes) of STRING, but scan at most
439 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
442 # if @REPLACE_STRNLEN@
443 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
445 # define strnlen rpl_strnlen
447 _GL_FUNCDECL_RPL (strnlen
, size_t, (char const *__string
, size_t __maxlen
)
449 _GL_ARG_NONNULL ((1)));
450 _GL_CXXALIAS_RPL (strnlen
, size_t, (char const *__string
, size_t __maxlen
));
452 # if ! @HAVE_DECL_STRNLEN@
453 _GL_FUNCDECL_SYS (strnlen
, size_t, (char const *__string
, size_t __maxlen
)
455 _GL_ARG_NONNULL ((1)));
457 _GL_CXXALIAS_SYS (strnlen
, size_t, (char const *__string
, size_t __maxlen
));
459 _GL_CXXALIASWARN (strnlen
);
460 #elif defined GNULIB_POSIXCHECK
462 # if HAVE_RAW_DECL_STRNLEN
463 _GL_WARN_ON_USE (strnlen
, "strnlen is unportable - "
464 "use gnulib module strnlen for portability");
468 #if defined GNULIB_POSIXCHECK
469 /* strcspn() assumes the second argument is a list of single-byte characters.
470 Even in this simple case, it does not work with multibyte strings if the
471 locale encoding is GB18030 and one of the characters to be searched is a
474 /* Assume strcspn is always declared. */
475 _GL_WARN_ON_USE (strcspn
, "strcspn cannot work correctly on character strings "
476 "in multibyte locales - "
477 "use mbscspn if you care about internationalization");
480 /* Find the first occurrence in S of any character in ACCEPT. */
482 # if ! @HAVE_STRPBRK@
483 _GL_FUNCDECL_SYS (strpbrk
, char *, (char const *__s
, char const *__accept
)
485 _GL_ARG_NONNULL ((1, 2)));
487 /* On some systems, this function is defined as an overloaded function:
488 extern "C" { const char * strpbrk (const char *, const char *); }
489 extern "C++" { char * strpbrk (char *, const char *); } */
490 _GL_CXXALIAS_SYS_CAST2 (strpbrk
,
491 char *, (char const *__s
, char const *__accept
),
492 const char *, (char const *__s
, char const *__accept
));
493 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
494 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
495 _GL_CXXALIASWARN1 (strpbrk
, char *, (char *__s
, char const *__accept
));
496 _GL_CXXALIASWARN1 (strpbrk
, char const *,
497 (char const *__s
, char const *__accept
));
499 _GL_CXXALIASWARN (strpbrk
);
501 # if defined GNULIB_POSIXCHECK
502 /* strpbrk() assumes the second argument is a list of single-byte characters.
503 Even in this simple case, it does not work with multibyte strings if the
504 locale encoding is GB18030 and one of the characters to be searched is a
507 _GL_WARN_ON_USE (strpbrk
, "strpbrk cannot work correctly on character strings "
508 "in multibyte locales - "
509 "use mbspbrk if you care about internationalization");
511 #elif defined GNULIB_POSIXCHECK
513 # if HAVE_RAW_DECL_STRPBRK
514 _GL_WARN_ON_USE (strpbrk
, "strpbrk is unportable - "
515 "use gnulib module strpbrk for portability");
519 #if defined GNULIB_POSIXCHECK
520 /* strspn() assumes the second argument is a list of single-byte characters.
521 Even in this simple case, it cannot work with multibyte strings. */
523 /* Assume strspn is always declared. */
524 _GL_WARN_ON_USE (strspn
, "strspn cannot work correctly on character strings "
525 "in multibyte locales - "
526 "use mbsspn if you care about internationalization");
529 #if defined GNULIB_POSIXCHECK
530 /* strrchr() does not work with multibyte strings if the locale encoding is
531 GB18030 and the character to be searched is a digit. */
533 /* Assume strrchr is always declared. */
534 _GL_WARN_ON_USE (strrchr
, "strrchr cannot work correctly on character strings "
535 "in some multibyte locales - "
536 "use mbsrchr if you care about internationalization");
539 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
540 If one is found, overwrite it with a NUL, and advance *STRINGP
541 to point to the next char after it. Otherwise, set *STRINGP to NULL.
542 If *STRINGP was already NULL, nothing happens.
543 Return the old value of *STRINGP.
545 This is a variant of strtok() that is multithread-safe and supports
548 Caveat: It modifies the original string.
549 Caveat: These functions cannot be used on constant strings.
550 Caveat: The identity of the delimiting character is lost.
551 Caveat: It doesn't work with multibyte strings unless all of the delimiter
552 characters are ASCII characters < 0x30.
554 See also strtok_r(). */
557 _GL_FUNCDECL_SYS (strsep
, char *,
558 (char **restrict __stringp
, char const *restrict __delim
)
559 _GL_ARG_NONNULL ((1, 2)));
561 _GL_CXXALIAS_SYS (strsep
, char *,
562 (char **restrict __stringp
, char const *restrict __delim
));
563 _GL_CXXALIASWARN (strsep
);
564 # if defined GNULIB_POSIXCHECK
566 _GL_WARN_ON_USE (strsep
, "strsep cannot work correctly on character strings "
567 "in multibyte locales - "
568 "use mbssep if you care about internationalization");
570 #elif defined GNULIB_POSIXCHECK
572 # if HAVE_RAW_DECL_STRSEP
573 _GL_WARN_ON_USE (strsep
, "strsep is unportable - "
574 "use gnulib module strsep for portability");
579 # if @REPLACE_STRSTR@
580 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
581 # define strstr rpl_strstr
583 _GL_FUNCDECL_RPL (strstr
, char *, (const char *haystack
, const char *needle
)
585 _GL_ARG_NONNULL ((1, 2)));
586 _GL_CXXALIAS_RPL (strstr
, char *, (const char *haystack
, const char *needle
));
588 /* On some systems, this function is defined as an overloaded function:
589 extern "C++" { const char * strstr (const char *, const char *); }
590 extern "C++" { char * strstr (char *, const char *); } */
591 _GL_CXXALIAS_SYS_CAST2 (strstr
,
592 char *, (const char *haystack
, const char *needle
),
593 const char *, (const char *haystack
, const char *needle
));
595 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
596 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
597 _GL_CXXALIASWARN1 (strstr
, char *, (char *haystack
, const char *needle
));
598 _GL_CXXALIASWARN1 (strstr
, const char *,
599 (const char *haystack
, const char *needle
));
601 _GL_CXXALIASWARN (strstr
);
603 #elif defined GNULIB_POSIXCHECK
604 /* strstr() does not work with multibyte strings if the locale encoding is
605 different from UTF-8:
606 POSIX says that it operates on "strings", and "string" in POSIX is defined
607 as a sequence of bytes, not of characters. */
609 /* Assume strstr is always declared. */
610 _GL_WARN_ON_USE (strstr
, "strstr is quadratic on many systems, and cannot "
611 "work correctly on character strings in most "
612 "multibyte locales - "
613 "use mbsstr if you care about internationalization, "
614 "or use strstr if you care about speed");
617 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
619 #if @GNULIB_STRCASESTR@
620 # if @REPLACE_STRCASESTR@
621 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
622 # define strcasestr rpl_strcasestr
624 _GL_FUNCDECL_RPL (strcasestr
, char *,
625 (const char *haystack
, const char *needle
)
627 _GL_ARG_NONNULL ((1, 2)));
628 _GL_CXXALIAS_RPL (strcasestr
, char *,
629 (const char *haystack
, const char *needle
));
631 # if ! @HAVE_STRCASESTR@
632 _GL_FUNCDECL_SYS (strcasestr
, char *,
633 (const char *haystack
, const char *needle
)
635 _GL_ARG_NONNULL ((1, 2)));
637 /* On some systems, this function is defined as an overloaded function:
638 extern "C++" { const char * strcasestr (const char *, const char *); }
639 extern "C++" { char * strcasestr (char *, const char *); } */
640 _GL_CXXALIAS_SYS_CAST2 (strcasestr
,
641 char *, (const char *haystack
, const char *needle
),
642 const char *, (const char *haystack
, const char *needle
));
644 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
645 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
646 _GL_CXXALIASWARN1 (strcasestr
, char *, (char *haystack
, const char *needle
));
647 _GL_CXXALIASWARN1 (strcasestr
, const char *,
648 (const char *haystack
, const char *needle
));
650 _GL_CXXALIASWARN (strcasestr
);
652 #elif defined GNULIB_POSIXCHECK
653 /* strcasestr() does not work with multibyte strings:
654 It is a glibc extension, and glibc implements it only for unibyte
657 # if HAVE_RAW_DECL_STRCASESTR
658 _GL_WARN_ON_USE (strcasestr
, "strcasestr does work correctly on character "
659 "strings in multibyte locales - "
660 "use mbscasestr if you care about "
661 "internationalization, or use c-strcasestr if you want "
662 "a locale independent function");
666 /* Parse S into tokens separated by characters in DELIM.
667 If S is NULL, the saved pointer in SAVE_PTR is used as
668 the next starting point. For example:
669 char s[] = "-abc-=-def";
671 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
672 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
673 x = strtok_r(NULL, "=", &sp); // x = NULL
676 This is a variant of strtok() that is multithread-safe.
678 For the POSIX documentation for this function, see:
679 http://www.opengroup.org/susv3xsh/strtok.html
681 Caveat: It modifies the original string.
682 Caveat: These functions cannot be used on constant strings.
683 Caveat: The identity of the delimiting character is lost.
684 Caveat: It doesn't work with multibyte strings unless all of the delimiter
685 characters are ASCII characters < 0x30.
687 See also strsep(). */
688 #if @GNULIB_STRTOK_R@
689 # if @REPLACE_STRTOK_R@
690 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
692 # define strtok_r rpl_strtok_r
694 _GL_FUNCDECL_RPL (strtok_r
, char *,
695 (char *restrict s
, char const *restrict delim
,
696 char **restrict save_ptr
)
697 _GL_ARG_NONNULL ((2, 3)));
698 _GL_CXXALIAS_RPL (strtok_r
, char *,
699 (char *restrict s
, char const *restrict delim
,
700 char **restrict save_ptr
));
702 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
705 # if ! @HAVE_DECL_STRTOK_R@
706 _GL_FUNCDECL_SYS (strtok_r
, char *,
707 (char *restrict s
, char const *restrict delim
,
708 char **restrict save_ptr
)
709 _GL_ARG_NONNULL ((2, 3)));
711 _GL_CXXALIAS_SYS (strtok_r
, char *,
712 (char *restrict s
, char const *restrict delim
,
713 char **restrict save_ptr
));
715 _GL_CXXALIASWARN (strtok_r
);
716 # if defined GNULIB_POSIXCHECK
717 _GL_WARN_ON_USE (strtok_r
, "strtok_r cannot work correctly on character "
718 "strings in multibyte locales - "
719 "use mbstok_r if you care about internationalization");
721 #elif defined GNULIB_POSIXCHECK
723 # if HAVE_RAW_DECL_STRTOK_R
724 _GL_WARN_ON_USE (strtok_r
, "strtok_r is unportable - "
725 "use gnulib module strtok_r for portability");
730 /* The following functions are not specified by POSIX. They are gnulib
734 /* Return the number of multibyte characters in the character string STRING.
735 This considers multibyte characters, unlike strlen, which counts bytes. */
736 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
739 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
740 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
741 # define mbslen rpl_mbslen
743 _GL_FUNCDECL_RPL (mbslen
, size_t, (const char *string
)
745 _GL_ARG_NONNULL ((1)));
746 _GL_CXXALIAS_RPL (mbslen
, size_t, (const char *string
));
748 _GL_FUNCDECL_SYS (mbslen
, size_t, (const char *string
)
750 _GL_ARG_NONNULL ((1)));
751 _GL_CXXALIAS_SYS (mbslen
, size_t, (const char *string
));
753 _GL_CXXALIASWARN (mbslen
);
757 /* Return the number of multibyte characters in the character string starting
758 at STRING and ending at STRING + LEN. */
759 _GL_EXTERN_C
size_t mbsnlen (const char *string
, size_t len
)
761 _GL_ARG_NONNULL ((1));
765 /* Locate the first single-byte character C in the character string STRING,
766 and return a pointer to it. Return NULL if C is not found in STRING.
767 Unlike strchr(), this function works correctly in multibyte locales with
768 encodings such as GB18030. */
770 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
771 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
773 _GL_FUNCDECL_RPL (mbschr
, char *, (const char *string
, int c
)
775 _GL_ARG_NONNULL ((1)));
776 _GL_CXXALIAS_RPL (mbschr
, char *, (const char *string
, int c
));
778 _GL_FUNCDECL_SYS (mbschr
, char *, (const char *string
, int c
)
780 _GL_ARG_NONNULL ((1)));
781 _GL_CXXALIAS_SYS (mbschr
, char *, (const char *string
, int c
));
783 _GL_CXXALIASWARN (mbschr
);
787 /* Locate the last single-byte character C in the character string STRING,
788 and return a pointer to it. Return NULL if C is not found in STRING.
789 Unlike strrchr(), this function works correctly in multibyte locales with
790 encodings such as GB18030. */
791 # if defined __hpux || defined __INTERIX
792 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
793 # define mbsrchr rpl_mbsrchr /* avoid collision with system function */
795 _GL_FUNCDECL_RPL (mbsrchr
, char *, (const char *string
, int c
)
797 _GL_ARG_NONNULL ((1)));
798 _GL_CXXALIAS_RPL (mbsrchr
, char *, (const char *string
, int c
));
800 _GL_FUNCDECL_SYS (mbsrchr
, char *, (const char *string
, int c
)
802 _GL_ARG_NONNULL ((1)));
803 _GL_CXXALIAS_SYS (mbsrchr
, char *, (const char *string
, int c
));
805 _GL_CXXALIASWARN (mbsrchr
);
809 /* Find the first occurrence of the character string NEEDLE in the character
810 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
811 Unlike strstr(), this function works correctly in multibyte locales with
812 encodings different from UTF-8. */
813 _GL_EXTERN_C
char * mbsstr (const char *haystack
, const char *needle
)
815 _GL_ARG_NONNULL ((1, 2));
818 #if @GNULIB_MBSCASECMP@
819 /* Compare the character strings S1 and S2, ignoring case, returning less than,
820 equal to or greater than zero if S1 is lexicographically less than, equal to
822 Note: This function may, in multibyte locales, return 0 for strings of
824 Unlike strcasecmp(), this function works correctly in multibyte locales. */
825 _GL_EXTERN_C
int mbscasecmp (const char *s1
, const char *s2
)
827 _GL_ARG_NONNULL ((1, 2));
830 #if @GNULIB_MBSNCASECMP@
831 /* Compare the initial segment of the character string S1 consisting of at most
832 N characters with the initial segment of the character string S2 consisting
833 of at most N characters, ignoring case, returning less than, equal to or
834 greater than zero if the initial segment of S1 is lexicographically less
835 than, equal to or greater than the initial segment of S2.
836 Note: This function may, in multibyte locales, return 0 for initial segments
837 of different lengths!
838 Unlike strncasecmp(), this function works correctly in multibyte locales.
839 But beware that N is not a byte count but a character count! */
840 _GL_EXTERN_C
int mbsncasecmp (const char *s1
, const char *s2
, size_t n
)
842 _GL_ARG_NONNULL ((1, 2));
845 #if @GNULIB_MBSPCASECMP@
846 /* Compare the initial segment of the character string STRING consisting of
847 at most mbslen (PREFIX) characters with the character string PREFIX,
848 ignoring case. If the two match, return a pointer to the first byte
849 after this prefix in STRING. Otherwise, return NULL.
850 Note: This function may, in multibyte locales, return non-NULL if STRING
851 is of smaller length than PREFIX!
852 Unlike strncasecmp(), this function works correctly in multibyte
854 _GL_EXTERN_C
char * mbspcasecmp (const char *string
, const char *prefix
)
856 _GL_ARG_NONNULL ((1, 2));
859 #if @GNULIB_MBSCASESTR@
860 /* Find the first occurrence of the character string NEEDLE in the character
861 string HAYSTACK, using case-insensitive comparison.
862 Note: This function may, in multibyte locales, return success even if
863 strlen (haystack) < strlen (needle) !
864 Unlike strcasestr(), this function works correctly in multibyte locales. */
865 _GL_EXTERN_C
char * mbscasestr (const char *haystack
, const char *needle
)
867 _GL_ARG_NONNULL ((1, 2));
871 /* Find the first occurrence in the character string STRING of any character
872 in the character string ACCEPT. Return the number of bytes from the
873 beginning of the string to this occurrence, or to the end of the string
875 Unlike strcspn(), this function works correctly in multibyte locales. */
876 _GL_EXTERN_C
size_t mbscspn (const char *string
, const char *accept
)
878 _GL_ARG_NONNULL ((1, 2));
882 /* Find the first occurrence in the character string STRING of any character
883 in the character string ACCEPT. Return the pointer to it, or NULL if none
885 Unlike strpbrk(), this function works correctly in multibyte locales. */
887 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
888 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
890 _GL_FUNCDECL_RPL (mbspbrk
, char *, (const char *string
, const char *accept
)
892 _GL_ARG_NONNULL ((1, 2)));
893 _GL_CXXALIAS_RPL (mbspbrk
, char *, (const char *string
, const char *accept
));
895 _GL_FUNCDECL_SYS (mbspbrk
, char *, (const char *string
, const char *accept
)
897 _GL_ARG_NONNULL ((1, 2)));
898 _GL_CXXALIAS_SYS (mbspbrk
, char *, (const char *string
, const char *accept
));
900 _GL_CXXALIASWARN (mbspbrk
);
904 /* Find the first occurrence in the character string STRING of any character
905 not in the character string REJECT. Return the number of bytes from the
906 beginning of the string to this occurrence, or to the end of the string
908 Unlike strspn(), this function works correctly in multibyte locales. */
909 _GL_EXTERN_C
size_t mbsspn (const char *string
, const char *reject
)
911 _GL_ARG_NONNULL ((1, 2));
915 /* Search the next delimiter (multibyte character listed in the character
916 string DELIM) starting at the character string *STRINGP.
917 If one is found, overwrite it with a NUL, and advance *STRINGP to point
918 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
919 If *STRINGP was already NULL, nothing happens.
920 Return the old value of *STRINGP.
922 This is a variant of mbstok_r() that supports empty fields.
924 Caveat: It modifies the original string.
925 Caveat: These functions cannot be used on constant strings.
926 Caveat: The identity of the delimiting character is lost.
928 See also mbstok_r(). */
929 _GL_EXTERN_C
char * mbssep (char **stringp
, const char *delim
)
930 _GL_ARG_NONNULL ((1, 2));
933 #if @GNULIB_MBSTOK_R@
934 /* Parse the character string STRING into tokens separated by characters in
935 the character string DELIM.
936 If STRING is NULL, the saved pointer in SAVE_PTR is used as
937 the next starting point. For example:
938 char s[] = "-abc-=-def";
940 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
941 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
942 x = mbstok_r(NULL, "=", &sp); // x = NULL
945 Caveat: It modifies the original string.
946 Caveat: These functions cannot be used on constant strings.
947 Caveat: The identity of the delimiting character is lost.
949 See also mbssep(). */
950 _GL_EXTERN_C
char * mbstok_r (char *string
, const char *delim
, char **save_ptr
)
951 _GL_ARG_NONNULL ((2, 3));
954 /* Map any int, typically from errno, into an error message. */
955 #if @GNULIB_STRERROR@
956 # if @REPLACE_STRERROR@
957 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
959 # define strerror rpl_strerror
961 _GL_FUNCDECL_RPL (strerror
, char *, (int));
962 _GL_CXXALIAS_RPL (strerror
, char *, (int));
964 _GL_CXXALIAS_SYS (strerror
, char *, (int));
966 _GL_CXXALIASWARN (strerror
);
967 #elif defined GNULIB_POSIXCHECK
969 /* Assume strerror is always declared. */
970 _GL_WARN_ON_USE (strerror
, "strerror is unportable - "
971 "use gnulib module strerror to guarantee non-NULL result");
974 /* Map any int, typically from errno, into an error message. Multithread-safe.
975 Uses the POSIX declaration, not the glibc declaration. */
976 #if @GNULIB_STRERROR_R@
977 # if @REPLACE_STRERROR_R@
978 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
980 # define strerror_r rpl_strerror_r
982 _GL_FUNCDECL_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
983 _GL_ARG_NONNULL ((2)));
984 _GL_CXXALIAS_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
986 # if !@HAVE_DECL_STRERROR_R@
987 _GL_FUNCDECL_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
988 _GL_ARG_NONNULL ((2)));
990 _GL_CXXALIAS_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
992 # if @HAVE_DECL_STRERROR_R@
993 _GL_CXXALIASWARN (strerror_r
);
995 #elif defined GNULIB_POSIXCHECK
997 # if HAVE_RAW_DECL_STRERROR_R
998 _GL_WARN_ON_USE (strerror_r
, "strerror_r is unportable - "
999 "use gnulib module strerror_r-posix for portability");
1003 #if @GNULIB_STRSIGNAL@
1004 # if @REPLACE_STRSIGNAL@
1005 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1006 # define strsignal rpl_strsignal
1008 _GL_FUNCDECL_RPL (strsignal
, char *, (int __sig
));
1009 _GL_CXXALIAS_RPL (strsignal
, char *, (int __sig
));
1011 # if ! @HAVE_DECL_STRSIGNAL@
1012 _GL_FUNCDECL_SYS (strsignal
, char *, (int __sig
));
1014 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
1016 _GL_CXXALIAS_SYS_CAST (strsignal
, char *, (int __sig
));
1018 _GL_CXXALIASWARN (strsignal
);
1019 #elif defined GNULIB_POSIXCHECK
1021 # if HAVE_RAW_DECL_STRSIGNAL
1022 _GL_WARN_ON_USE (strsignal
, "strsignal is unportable - "
1023 "use gnulib module strsignal for portability");
1027 #if @GNULIB_STRVERSCMP@
1028 # if !@HAVE_STRVERSCMP@
1029 _GL_FUNCDECL_SYS (strverscmp
, int, (const char *, const char *)
1031 _GL_ARG_NONNULL ((1, 2)));
1033 _GL_CXXALIAS_SYS (strverscmp
, int, (const char *, const char *));
1034 _GL_CXXALIASWARN (strverscmp
);
1035 #elif defined GNULIB_POSIXCHECK
1037 # if HAVE_RAW_DECL_STRVERSCMP
1038 _GL_WARN_ON_USE (strverscmp
, "strverscmp is unportable - "
1039 "use gnulib module strverscmp for portability");
1044 #endif /* _@GUARD_PREFIX@_STRING_H */
1045 #endif /* _@GUARD_PREFIX@_STRING_H */