1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2011 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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 @PRAGMA_SYSTEM_HEADER@
26 /* The include_next requires a split double-inclusion guard. */
27 #@INCLUDE_NEXT@ @NEXT_STRING_H@
32 /* NetBSD 5.0 mis-defines NULL. */
35 /* MirBSD defines mbslen as a macro. */
36 #if @GNULIB_MBSLEN@ && defined __MirBSD__
40 /* The __attribute__ feature is available in gcc versions 2.5 and later.
41 The attribute __pure__ was added in gcc 2.96. */
42 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
43 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
45 # define _GL_ATTRIBUTE_PURE /* empty */
48 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
49 /* But in any case avoid namespace pollution on glibc systems. */
50 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
51 && ! defined __GLIBC__
55 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
57 /* The definition of _GL_ARG_NONNULL is copied here. */
59 /* The definition of _GL_WARN_ON_USE is copied here. */
62 /* Return the first instance of C within N bytes of S, or NULL. */
65 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
66 # define memchr rpl_memchr
68 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
70 _GL_ARG_NONNULL ((1)));
71 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
74 _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
76 _GL_ARG_NONNULL ((1)));
78 /* On some systems, this function is defined as an overloaded function:
79 extern "C" { const void * std::memchr (const void *, int, size_t); }
80 extern "C++" { void * std::memchr (void *, int, size_t); } */
81 _GL_CXXALIAS_SYS_CAST2 (memchr,
82 void *, (void const *__s, int __c, size_t __n),
83 void const *, (void const *__s, int __c, size_t __n));
85 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
86 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
87 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
88 _GL_CXXALIASWARN1 (memchr, void const *,
89 (void const *__s, int __c, size_t __n));
91 _GL_CXXALIASWARN (memchr);
93 #elif defined GNULIB_POSIXCHECK
95 /* Assume memchr is always declared. */
96 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
97 "use gnulib module memchr for portability" );
100 /* Return the first occurrence of NEEDLE in HAYSTACK. */
102 # if @REPLACE_MEMMEM@
103 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
104 # define memmem rpl_memmem
106 _GL_FUNCDECL_RPL (memmem, void *,
107 (void const *__haystack, size_t __haystack_len,
108 void const *__needle, size_t __needle_len)
110 _GL_ARG_NONNULL ((1, 3)));
111 _GL_CXXALIAS_RPL (memmem, void *,
112 (void const *__haystack, size_t __haystack_len,
113 void const *__needle, size_t __needle_len));
115 # if ! @HAVE_DECL_MEMMEM@
116 _GL_FUNCDECL_SYS (memmem, void *,
117 (void const *__haystack, size_t __haystack_len,
118 void const *__needle, size_t __needle_len)
120 _GL_ARG_NONNULL ((1, 3)));
122 _GL_CXXALIAS_SYS (memmem, void *,
123 (void const *__haystack, size_t __haystack_len,
124 void const *__needle, size_t __needle_len));
126 _GL_CXXALIASWARN (memmem);
127 #elif defined GNULIB_POSIXCHECK
129 # if HAVE_RAW_DECL_MEMMEM
130 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
131 "use gnulib module memmem-simple for portability, "
132 "and module memmem for speed" );
136 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
137 last written byte. */
139 # if ! @HAVE_MEMPCPY@
140 _GL_FUNCDECL_SYS (mempcpy, void *,
141 (void *restrict __dest, void const *restrict __src,
143 _GL_ARG_NONNULL ((1, 2)));
145 _GL_CXXALIAS_SYS (mempcpy, void *,
146 (void *restrict __dest, void const *restrict __src,
148 _GL_CXXALIASWARN (mempcpy);
149 #elif defined GNULIB_POSIXCHECK
151 # if HAVE_RAW_DECL_MEMPCPY
152 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
153 "use gnulib module mempcpy for portability");
157 /* Search backwards through a block for a byte (specified as an int). */
159 # if ! @HAVE_DECL_MEMRCHR@
160 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
162 _GL_ARG_NONNULL ((1)));
164 /* On some systems, this function is defined as an overloaded function:
165 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
166 extern "C++" { void * std::memrchr (void *, int, size_t); } */
167 _GL_CXXALIAS_SYS_CAST2 (memrchr,
168 void *, (void const *, int, size_t),
169 void const *, (void const *, int, size_t));
170 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
171 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
172 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
173 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
175 _GL_CXXALIASWARN (memrchr);
177 #elif defined GNULIB_POSIXCHECK
179 # if HAVE_RAW_DECL_MEMRCHR
180 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
181 "use gnulib module memrchr for portability");
185 /* Find the first occurrence of C in S. More efficient than
186 memchr(S,C,N), at the expense of undefined behavior if C does not
187 occur within N bytes. */
188 #if @GNULIB_RAWMEMCHR@
189 # if ! @HAVE_RAWMEMCHR@
190 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
192 _GL_ARG_NONNULL ((1)));
194 /* On some systems, this function is defined as an overloaded function:
195 extern "C++" { const void * std::rawmemchr (const void *, int); }
196 extern "C++" { void * std::rawmemchr (void *, int); } */
197 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
198 void *, (void const *__s, int __c_in),
199 void const *, (void const *__s, int __c_in));
200 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
201 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
202 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
203 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
205 _GL_CXXALIASWARN (rawmemchr);
207 #elif defined GNULIB_POSIXCHECK
209 # if HAVE_RAW_DECL_RAWMEMCHR
210 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
211 "use gnulib module rawmemchr for portability");
215 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
218 _GL_FUNCDECL_SYS (stpcpy, char *,
219 (char *restrict __dst, char const *restrict __src)
220 _GL_ARG_NONNULL ((1, 2)));
222 _GL_CXXALIAS_SYS (stpcpy, char *,
223 (char *restrict __dst, char const *restrict __src));
224 _GL_CXXALIASWARN (stpcpy);
225 #elif defined GNULIB_POSIXCHECK
227 # if HAVE_RAW_DECL_STPCPY
228 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
229 "use gnulib module stpcpy for portability");
233 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
234 last non-NUL byte written into DST. */
236 # if @REPLACE_STPNCPY@
237 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
239 # define stpncpy rpl_stpncpy
241 _GL_FUNCDECL_RPL (stpncpy, char *,
242 (char *restrict __dst, char const *restrict __src,
244 _GL_ARG_NONNULL ((1, 2)));
245 _GL_CXXALIAS_RPL (stpncpy, char *,
246 (char *restrict __dst, char const *restrict __src,
249 # if ! @HAVE_STPNCPY@
250 _GL_FUNCDECL_SYS (stpncpy, char *,
251 (char *restrict __dst, char const *restrict __src,
253 _GL_ARG_NONNULL ((1, 2)));
255 _GL_CXXALIAS_SYS (stpncpy, char *,
256 (char *restrict __dst, char const *restrict __src,
259 _GL_CXXALIASWARN (stpncpy);
260 #elif defined GNULIB_POSIXCHECK
262 # if HAVE_RAW_DECL_STPNCPY
263 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
264 "use gnulib module stpncpy for portability");
268 #if defined GNULIB_POSIXCHECK
269 /* strchr() does not work with multibyte strings if the locale encoding is
270 GB18030 and the character to be searched is a digit. */
272 /* Assume strchr is always declared. */
273 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
274 "in some multibyte locales - "
275 "use mbschr if you care about internationalization");
278 /* Find the first occurrence of C in S or the final NUL byte. */
279 #if @GNULIB_STRCHRNUL@
280 # if @REPLACE_STRCHRNUL@
281 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
282 # define strchrnul rpl_strchrnul
284 _GL_FUNCDECL_RPL (strchrnul, char *,
285 (const char *str, int ch)
287 _GL_ARG_NONNULL ((1)));
288 _GL_CXXALIAS_RPL (strchrnul, char *,
289 (const char *str, int ch));
291 # if ! @HAVE_STRCHRNUL@
292 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
294 _GL_ARG_NONNULL ((1)));
296 /* On some systems, this function is defined as an overloaded function:
297 extern "C++" { const char * std::strchrnul (const char *, int); }
298 extern "C++" { char * std::strchrnul (char *, int); } */
299 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
300 char *, (char const *__s, int __c_in),
301 char const *, (char const *__s, int __c_in));
303 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
304 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
305 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
306 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
308 _GL_CXXALIASWARN (strchrnul);
310 #elif defined GNULIB_POSIXCHECK
312 # if HAVE_RAW_DECL_STRCHRNUL
313 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
314 "use gnulib module strchrnul for portability");
318 /* Duplicate S, returning an identical malloc'd string. */
320 # if @REPLACE_STRDUP@
321 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
323 # define strdup rpl_strdup
325 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
326 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
328 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
329 /* strdup exists as a function and as a macro. Get rid of the macro. */
332 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
333 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
335 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
337 _GL_CXXALIASWARN (strdup);
338 #elif defined GNULIB_POSIXCHECK
340 # if HAVE_RAW_DECL_STRDUP
341 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
342 "use gnulib module strdup for portability");
346 /* Append no more than N characters from SRC onto DEST. */
348 # if @REPLACE_STRNCAT@
349 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
351 # define strncat rpl_strncat
353 _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
354 _GL_ARG_NONNULL ((1, 2)));
355 _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
357 _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
359 _GL_CXXALIASWARN (strncat);
360 #elif defined GNULIB_POSIXCHECK
362 # if HAVE_RAW_DECL_STRNCAT
363 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
364 "use gnulib module strncat for portability");
368 /* Return a newly allocated copy of at most N bytes of STRING. */
370 # if @REPLACE_STRNDUP@
371 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
373 # define strndup rpl_strndup
375 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
376 _GL_ARG_NONNULL ((1)));
377 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
379 # if ! @HAVE_DECL_STRNDUP@
380 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
381 _GL_ARG_NONNULL ((1)));
383 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
385 _GL_CXXALIASWARN (strndup);
386 #elif defined GNULIB_POSIXCHECK
388 # if HAVE_RAW_DECL_STRNDUP
389 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
390 "use gnulib module strndup for portability");
394 /* Find the length (number of bytes) of STRING, but scan at most
395 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
398 # if @REPLACE_STRNLEN@
399 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
401 # define strnlen rpl_strnlen
403 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__string, size_t __maxlen)
405 _GL_ARG_NONNULL ((1)));
406 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__string, size_t __maxlen));
408 # if ! @HAVE_DECL_STRNLEN@
409 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
411 _GL_ARG_NONNULL ((1)));
413 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
415 _GL_CXXALIASWARN (strnlen);
416 #elif defined GNULIB_POSIXCHECK
418 # if HAVE_RAW_DECL_STRNLEN
419 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
420 "use gnulib module strnlen for portability");
424 #if defined GNULIB_POSIXCHECK
425 /* strcspn() assumes the second argument is a list of single-byte characters.
426 Even in this simple case, it does not work with multibyte strings if the
427 locale encoding is GB18030 and one of the characters to be searched is a
430 /* Assume strcspn is always declared. */
431 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
432 "in multibyte locales - "
433 "use mbscspn if you care about internationalization");
436 /* Find the first occurrence in S of any character in ACCEPT. */
438 # if ! @HAVE_STRPBRK@
439 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
441 _GL_ARG_NONNULL ((1, 2)));
443 /* On some systems, this function is defined as an overloaded function:
444 extern "C" { const char * strpbrk (const char *, const char *); }
445 extern "C++" { char * strpbrk (char *, const char *); } */
446 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
447 char *, (char const *__s, char const *__accept),
448 const char *, (char const *__s, char const *__accept));
449 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
450 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
451 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
452 _GL_CXXALIASWARN1 (strpbrk, char const *,
453 (char const *__s, char const *__accept));
455 _GL_CXXALIASWARN (strpbrk);
457 # if defined GNULIB_POSIXCHECK
458 /* strpbrk() assumes the second argument is a list of single-byte characters.
459 Even in this simple case, it does not work with multibyte strings if the
460 locale encoding is GB18030 and one of the characters to be searched is a
463 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
464 "in multibyte locales - "
465 "use mbspbrk if you care about internationalization");
467 #elif defined GNULIB_POSIXCHECK
469 # if HAVE_RAW_DECL_STRPBRK
470 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
471 "use gnulib module strpbrk for portability");
475 #if defined GNULIB_POSIXCHECK
476 /* strspn() assumes the second argument is a list of single-byte characters.
477 Even in this simple case, it cannot work with multibyte strings. */
479 /* Assume strspn is always declared. */
480 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
481 "in multibyte locales - "
482 "use mbsspn if you care about internationalization");
485 #if defined GNULIB_POSIXCHECK
486 /* strrchr() does not work with multibyte strings if the locale encoding is
487 GB18030 and the character to be searched is a digit. */
489 /* Assume strrchr is always declared. */
490 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
491 "in some multibyte locales - "
492 "use mbsrchr if you care about internationalization");
495 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
496 If one is found, overwrite it with a NUL, and advance *STRINGP
497 to point to the next char after it. Otherwise, set *STRINGP to NULL.
498 If *STRINGP was already NULL, nothing happens.
499 Return the old value of *STRINGP.
501 This is a variant of strtok() that is multithread-safe and supports
504 Caveat: It modifies the original string.
505 Caveat: These functions cannot be used on constant strings.
506 Caveat: The identity of the delimiting character is lost.
507 Caveat: It doesn't work with multibyte strings unless all of the delimiter
508 characters are ASCII characters < 0x30.
510 See also strtok_r(). */
513 _GL_FUNCDECL_SYS (strsep, char *,
514 (char **restrict __stringp, char const *restrict __delim)
515 _GL_ARG_NONNULL ((1, 2)));
517 _GL_CXXALIAS_SYS (strsep, char *,
518 (char **restrict __stringp, char const *restrict __delim));
519 _GL_CXXALIASWARN (strsep);
520 # if defined GNULIB_POSIXCHECK
522 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
523 "in multibyte locales - "
524 "use mbssep if you care about internationalization");
526 #elif defined GNULIB_POSIXCHECK
528 # if HAVE_RAW_DECL_STRSEP
529 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
530 "use gnulib module strsep for portability");
535 # if @REPLACE_STRSTR@
536 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
537 # define strstr rpl_strstr
539 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
541 _GL_ARG_NONNULL ((1, 2)));
542 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
544 /* On some systems, this function is defined as an overloaded function:
545 extern "C++" { const char * strstr (const char *, const char *); }
546 extern "C++" { char * strstr (char *, const char *); } */
547 _GL_CXXALIAS_SYS_CAST2 (strstr,
548 char *, (const char *haystack, const char *needle),
549 const char *, (const char *haystack, const char *needle));
551 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
552 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
553 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
554 _GL_CXXALIASWARN1 (strstr, const char *,
555 (const char *haystack, const char *needle));
557 _GL_CXXALIASWARN (strstr);
559 #elif defined GNULIB_POSIXCHECK
560 /* strstr() does not work with multibyte strings if the locale encoding is
561 different from UTF-8:
562 POSIX says that it operates on "strings", and "string" in POSIX is defined
563 as a sequence of bytes, not of characters. */
565 /* Assume strstr is always declared. */
566 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
567 "work correctly on character strings in most "
568 "multibyte locales - "
569 "use mbsstr if you care about internationalization, "
570 "or use strstr if you care about speed");
573 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
575 #if @GNULIB_STRCASESTR@
576 # if @REPLACE_STRCASESTR@
577 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
578 # define strcasestr rpl_strcasestr
580 _GL_FUNCDECL_RPL (strcasestr, char *,
581 (const char *haystack, const char *needle)
583 _GL_ARG_NONNULL ((1, 2)));
584 _GL_CXXALIAS_RPL (strcasestr, char *,
585 (const char *haystack, const char *needle));
587 # if ! @HAVE_STRCASESTR@
588 _GL_FUNCDECL_SYS (strcasestr, char *,
589 (const char *haystack, const char *needle)
591 _GL_ARG_NONNULL ((1, 2)));
593 /* On some systems, this function is defined as an overloaded function:
594 extern "C++" { const char * strcasestr (const char *, const char *); }
595 extern "C++" { char * strcasestr (char *, const char *); } */
596 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
597 char *, (const char *haystack, const char *needle),
598 const char *, (const char *haystack, const char *needle));
600 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
601 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
602 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
603 _GL_CXXALIASWARN1 (strcasestr, const char *,
604 (const char *haystack, const char *needle));
606 _GL_CXXALIASWARN (strcasestr);
608 #elif defined GNULIB_POSIXCHECK
609 /* strcasestr() does not work with multibyte strings:
610 It is a glibc extension, and glibc implements it only for unibyte
613 # if HAVE_RAW_DECL_STRCASESTR
614 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
615 "strings in multibyte locales - "
616 "use mbscasestr if you care about "
617 "internationalization, or use c-strcasestr if you want "
618 "a locale independent function");
622 /* Parse S into tokens separated by characters in DELIM.
623 If S is NULL, the saved pointer in SAVE_PTR is used as
624 the next starting point. For example:
625 char s[] = "-abc-=-def";
627 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
628 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
629 x = strtok_r(NULL, "=", &sp); // x = NULL
632 This is a variant of strtok() that is multithread-safe.
634 For the POSIX documentation for this function, see:
635 http://www.opengroup.org/susv3xsh/strtok.html
637 Caveat: It modifies the original string.
638 Caveat: These functions cannot be used on constant strings.
639 Caveat: The identity of the delimiting character is lost.
640 Caveat: It doesn't work with multibyte strings unless all of the delimiter
641 characters are ASCII characters < 0x30.
643 See also strsep(). */
644 #if @GNULIB_STRTOK_R@
645 # if @REPLACE_STRTOK_R@
646 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
648 # define strtok_r rpl_strtok_r
650 _GL_FUNCDECL_RPL (strtok_r, char *,
651 (char *restrict s, char const *restrict delim,
652 char **restrict save_ptr)
653 _GL_ARG_NONNULL ((2, 3)));
654 _GL_CXXALIAS_RPL (strtok_r, char *,
655 (char *restrict s, char const *restrict delim,
656 char **restrict save_ptr));
658 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
661 # if ! @HAVE_DECL_STRTOK_R@
662 _GL_FUNCDECL_SYS (strtok_r, char *,
663 (char *restrict s, char const *restrict delim,
664 char **restrict save_ptr)
665 _GL_ARG_NONNULL ((2, 3)));
667 _GL_CXXALIAS_SYS (strtok_r, char *,
668 (char *restrict s, char const *restrict delim,
669 char **restrict save_ptr));
671 _GL_CXXALIASWARN (strtok_r);
672 # if defined GNULIB_POSIXCHECK
673 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
674 "strings in multibyte locales - "
675 "use mbstok_r if you care about internationalization");
677 #elif defined GNULIB_POSIXCHECK
679 # if HAVE_RAW_DECL_STRTOK_R
680 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
681 "use gnulib module strtok_r for portability");
686 /* The following functions are not specified by POSIX. They are gnulib
690 /* Return the number of multibyte characters in the character string STRING.
691 This considers multibyte characters, unlike strlen, which counts bytes. */
692 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
695 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
696 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
697 # define mbslen rpl_mbslen
699 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
700 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
702 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
703 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
705 _GL_CXXALIASWARN (mbslen);
709 /* Return the number of multibyte characters in the character string starting
710 at STRING and ending at STRING + LEN. */
711 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
712 _GL_ARG_NONNULL ((1));
716 /* Locate the first single-byte character C in the character string STRING,
717 and return a pointer to it. Return NULL if C is not found in STRING.
718 Unlike strchr(), this function works correctly in multibyte locales with
719 encodings such as GB18030. */
721 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
722 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
724 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
725 _GL_ARG_NONNULL ((1)));
726 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
728 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
729 _GL_ARG_NONNULL ((1)));
730 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
732 _GL_CXXALIASWARN (mbschr);
736 /* Locate the last single-byte character C in the character string STRING,
737 and return a pointer to it. Return NULL if C is not found in STRING.
738 Unlike strrchr(), this function works correctly in multibyte locales with
739 encodings such as GB18030. */
741 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
742 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
744 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
745 _GL_ARG_NONNULL ((1)));
746 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
748 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
749 _GL_ARG_NONNULL ((1)));
750 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
752 _GL_CXXALIASWARN (mbsrchr);
756 /* Find the first occurrence of the character string NEEDLE in the character
757 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
758 Unlike strstr(), this function works correctly in multibyte locales with
759 encodings different from UTF-8. */
760 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
761 _GL_ARG_NONNULL ((1, 2));
764 #if @GNULIB_MBSCASECMP@
765 /* Compare the character strings S1 and S2, ignoring case, returning less than,
766 equal to or greater than zero if S1 is lexicographically less than, equal to
768 Note: This function may, in multibyte locales, return 0 for strings of
770 Unlike strcasecmp(), this function works correctly in multibyte locales. */
771 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
772 _GL_ARG_NONNULL ((1, 2));
775 #if @GNULIB_MBSNCASECMP@
776 /* Compare the initial segment of the character string S1 consisting of at most
777 N characters with the initial segment of the character string S2 consisting
778 of at most N characters, ignoring case, returning less than, equal to or
779 greater than zero if the initial segment of S1 is lexicographically less
780 than, equal to or greater than the initial segment of S2.
781 Note: This function may, in multibyte locales, return 0 for initial segments
782 of different lengths!
783 Unlike strncasecmp(), this function works correctly in multibyte locales.
784 But beware that N is not a byte count but a character count! */
785 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
786 _GL_ARG_NONNULL ((1, 2));
789 #if @GNULIB_MBSPCASECMP@
790 /* Compare the initial segment of the character string STRING consisting of
791 at most mbslen (PREFIX) characters with the character string PREFIX,
792 ignoring case. If the two match, return a pointer to the first byte
793 after this prefix in STRING. Otherwise, return NULL.
794 Note: This function may, in multibyte locales, return non-NULL if STRING
795 is of smaller length than PREFIX!
796 Unlike strncasecmp(), this function works correctly in multibyte
798 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
799 _GL_ARG_NONNULL ((1, 2));
802 #if @GNULIB_MBSCASESTR@
803 /* Find the first occurrence of the character string NEEDLE in the character
804 string HAYSTACK, using case-insensitive comparison.
805 Note: This function may, in multibyte locales, return success even if
806 strlen (haystack) < strlen (needle) !
807 Unlike strcasestr(), this function works correctly in multibyte locales. */
808 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
809 _GL_ARG_NONNULL ((1, 2));
813 /* Find the first occurrence in the character string STRING of any character
814 in the character string ACCEPT. Return the number of bytes from the
815 beginning of the string to this occurrence, or to the end of the string
817 Unlike strcspn(), this function works correctly in multibyte locales. */
818 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
819 _GL_ARG_NONNULL ((1, 2));
823 /* Find the first occurrence in the character string STRING of any character
824 in the character string ACCEPT. Return the pointer to it, or NULL if none
826 Unlike strpbrk(), this function works correctly in multibyte locales. */
828 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
829 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
831 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
832 _GL_ARG_NONNULL ((1, 2)));
833 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
835 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
836 _GL_ARG_NONNULL ((1, 2)));
837 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
839 _GL_CXXALIASWARN (mbspbrk);
843 /* Find the first occurrence in the character string STRING of any character
844 not in the character string REJECT. Return the number of bytes from the
845 beginning of the string to this occurrence, or to the end of the string
847 Unlike strspn(), this function works correctly in multibyte locales. */
848 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
849 _GL_ARG_NONNULL ((1, 2));
853 /* Search the next delimiter (multibyte character listed in the character
854 string DELIM) starting at the character string *STRINGP.
855 If one is found, overwrite it with a NUL, and advance *STRINGP to point
856 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
857 If *STRINGP was already NULL, nothing happens.
858 Return the old value of *STRINGP.
860 This is a variant of mbstok_r() that supports empty fields.
862 Caveat: It modifies the original string.
863 Caveat: These functions cannot be used on constant strings.
864 Caveat: The identity of the delimiting character is lost.
866 See also mbstok_r(). */
867 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
868 _GL_ARG_NONNULL ((1, 2));
871 #if @GNULIB_MBSTOK_R@
872 /* Parse the character string STRING into tokens separated by characters in
873 the character string DELIM.
874 If STRING is NULL, the saved pointer in SAVE_PTR is used as
875 the next starting point. For example:
876 char s[] = "-abc-=-def";
878 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
879 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
880 x = mbstok_r(NULL, "=", &sp); // x = NULL
883 Caveat: It modifies the original string.
884 Caveat: These functions cannot be used on constant strings.
885 Caveat: The identity of the delimiting character is lost.
887 See also mbssep(). */
888 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
889 _GL_ARG_NONNULL ((2, 3));
892 /* Map any int, typically from errno, into an error message. */
893 #if @GNULIB_STRERROR@
894 # if @REPLACE_STRERROR@
895 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
897 # define strerror rpl_strerror
899 _GL_FUNCDECL_RPL (strerror, char *, (int));
900 _GL_CXXALIAS_RPL (strerror, char *, (int));
902 _GL_CXXALIAS_SYS (strerror, char *, (int));
904 _GL_CXXALIASWARN (strerror);
905 #elif defined GNULIB_POSIXCHECK
907 /* Assume strerror is always declared. */
908 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
909 "use gnulib module strerror to guarantee non-NULL result");
912 /* Map any int, typically from errno, into an error message. Multithread-safe.
913 Uses the POSIX declaration, not the glibc declaration. */
914 #if @GNULIB_STRERROR_R@
915 # if @REPLACE_STRERROR_R@
916 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
918 # define strerror_r rpl_strerror_r
920 _GL_FUNCDECL_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen)
921 _GL_ARG_NONNULL ((2)));
922 _GL_CXXALIAS_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen));
924 # if !@HAVE_DECL_STRERROR_R@
925 _GL_FUNCDECL_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen)
926 _GL_ARG_NONNULL ((2)));
928 _GL_CXXALIAS_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen));
930 # if @HAVE_DECL_STRERROR_R@
931 _GL_CXXALIASWARN (strerror_r);
933 #elif defined GNULIB_POSIXCHECK
935 # if HAVE_RAW_DECL_STRERROR_R
936 _GL_WARN_ON_USE (strerror_r, "strerror_r is unportable - "
937 "use gnulib module strerror_r-posix for portability");
941 #if @GNULIB_STRSIGNAL@
942 # if @REPLACE_STRSIGNAL@
943 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
944 # define strsignal rpl_strsignal
946 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
947 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
949 # if ! @HAVE_DECL_STRSIGNAL@
950 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
952 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
954 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
956 _GL_CXXALIASWARN (strsignal);
957 #elif defined GNULIB_POSIXCHECK
959 # if HAVE_RAW_DECL_STRSIGNAL
960 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
961 "use gnulib module strsignal for portability");
965 #if @GNULIB_STRVERSCMP@
966 # if !@HAVE_STRVERSCMP@
967 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
968 _GL_ARG_NONNULL ((1, 2)));
970 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
971 _GL_CXXALIASWARN (strverscmp);
972 #elif defined GNULIB_POSIXCHECK
974 # if HAVE_RAW_DECL_STRVERSCMP
975 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
976 "use gnulib module strverscmp for portability");
981 #endif /* _GL_STRING_H */
982 #endif /* _GL_STRING_H */