1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2010 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@
25 /* The include_next requires a split double-inclusion guard. */
26 #@INCLUDE_NEXT@ @NEXT_STRING_H@
31 /* NetBSD 5.0 mis-defines NULL. */
34 /* MirBSD defines mbslen as a macro. */
35 #if @GNULIB_MBSLEN@ && defined __MirBSD__
40 /* This feature is available in gcc versions 2.5 and later. */
41 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
42 # define __attribute__(Spec) /* empty */
45 /* The attribute __pure__ was added in gcc 2.96. */
46 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
47 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
49 # define _GL_ATTRIBUTE_PURE /* empty */
53 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
55 /* The definition of _GL_ARG_NONNULL is copied here. */
57 /* The definition of _GL_WARN_ON_USE is copied here. */
60 /* Return the first instance of C within N bytes of S, or NULL. */
63 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
64 # define memchr rpl_memchr
66 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
68 _GL_ARG_NONNULL ((1)));
69 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
72 _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
74 _GL_ARG_NONNULL ((1)));
76 /* On some systems, this function is defined as an overloaded function:
77 extern "C" { const void * std::memchr (const void *, int, size_t); }
78 extern "C++" { void * std::memchr (void *, int, size_t); } */
79 _GL_CXXALIAS_SYS_CAST2 (memchr,
80 void *, (void const *__s, int __c, size_t __n),
81 void const *, (void const *__s, int __c, size_t __n));
83 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
84 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
85 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
86 _GL_CXXALIASWARN1 (memchr, void const *,
87 (void const *__s, int __c, size_t __n));
89 _GL_CXXALIASWARN (memchr);
91 #elif defined GNULIB_POSIXCHECK
93 /* Assume memchr is always declared. */
94 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
95 "use gnulib module memchr for portability" );
98 /* Return the first occurrence of NEEDLE in HAYSTACK. */
100 # if @REPLACE_MEMMEM@
101 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
102 # define memmem rpl_memmem
104 _GL_FUNCDECL_RPL (memmem, void *,
105 (void const *__haystack, size_t __haystack_len,
106 void const *__needle, size_t __needle_len)
108 _GL_ARG_NONNULL ((1, 3)));
109 _GL_CXXALIAS_RPL (memmem, void *,
110 (void const *__haystack, size_t __haystack_len,
111 void const *__needle, size_t __needle_len));
113 # if ! @HAVE_DECL_MEMMEM@
114 _GL_FUNCDECL_SYS (memmem, void *,
115 (void const *__haystack, size_t __haystack_len,
116 void const *__needle, size_t __needle_len)
118 _GL_ARG_NONNULL ((1, 3)));
120 _GL_CXXALIAS_SYS (memmem, void *,
121 (void const *__haystack, size_t __haystack_len,
122 void const *__needle, size_t __needle_len));
124 _GL_CXXALIASWARN (memmem);
125 #elif defined GNULIB_POSIXCHECK
127 # if HAVE_RAW_DECL_MEMMEM
128 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
129 "use gnulib module memmem-simple for portability, "
130 "and module memmem for speed" );
134 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
135 last written byte. */
137 # if ! @HAVE_MEMPCPY@
138 _GL_FUNCDECL_SYS (mempcpy, void *,
139 (void *restrict __dest, void const *restrict __src,
141 _GL_ARG_NONNULL ((1, 2)));
143 _GL_CXXALIAS_SYS (mempcpy, void *,
144 (void *restrict __dest, void const *restrict __src,
146 _GL_CXXALIASWARN (mempcpy);
147 #elif defined GNULIB_POSIXCHECK
149 # if HAVE_RAW_DECL_MEMPCPY
150 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
151 "use gnulib module mempcpy for portability");
155 /* Search backwards through a block for a byte (specified as an int). */
157 # if ! @HAVE_DECL_MEMRCHR@
158 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
160 _GL_ARG_NONNULL ((1)));
162 /* On some systems, this function is defined as an overloaded function:
163 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
164 extern "C++" { void * std::memrchr (void *, int, size_t); } */
165 _GL_CXXALIAS_SYS_CAST2 (memrchr,
166 void *, (void const *, int, size_t),
167 void const *, (void const *, int, size_t));
168 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
169 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
170 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
171 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
173 _GL_CXXALIASWARN (memrchr);
175 #elif defined GNULIB_POSIXCHECK
177 # if HAVE_RAW_DECL_MEMRCHR
178 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
179 "use gnulib module memrchr for portability");
183 /* Find the first occurrence of C in S. More efficient than
184 memchr(S,C,N), at the expense of undefined behavior if C does not
185 occur within N bytes. */
186 #if @GNULIB_RAWMEMCHR@
187 # if ! @HAVE_RAWMEMCHR@
188 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
190 _GL_ARG_NONNULL ((1)));
192 /* On some systems, this function is defined as an overloaded function:
193 extern "C++" { const void * std::rawmemchr (const void *, int); }
194 extern "C++" { void * std::rawmemchr (void *, int); } */
195 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
196 void *, (void const *__s, int __c_in),
197 void const *, (void const *__s, int __c_in));
198 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
199 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
200 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
201 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
203 _GL_CXXALIASWARN (rawmemchr);
205 #elif defined GNULIB_POSIXCHECK
207 # if HAVE_RAW_DECL_RAWMEMCHR
208 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
209 "use gnulib module rawmemchr for portability");
213 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
216 _GL_FUNCDECL_SYS (stpcpy, char *,
217 (char *restrict __dst, char const *restrict __src)
218 _GL_ARG_NONNULL ((1, 2)));
220 _GL_CXXALIAS_SYS (stpcpy, char *,
221 (char *restrict __dst, char const *restrict __src));
222 _GL_CXXALIASWARN (stpcpy);
223 #elif defined GNULIB_POSIXCHECK
225 # if HAVE_RAW_DECL_STPCPY
226 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
227 "use gnulib module stpcpy for portability");
231 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
232 last non-NUL byte written into DST. */
234 # if @REPLACE_STPNCPY@
235 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
237 # define stpncpy rpl_stpncpy
239 _GL_FUNCDECL_RPL (stpncpy, char *,
240 (char *restrict __dst, char const *restrict __src,
242 _GL_ARG_NONNULL ((1, 2)));
243 _GL_CXXALIAS_RPL (stpncpy, char *,
244 (char *restrict __dst, char const *restrict __src,
247 # if ! @HAVE_STPNCPY@
248 _GL_FUNCDECL_SYS (stpncpy, char *,
249 (char *restrict __dst, char const *restrict __src,
251 _GL_ARG_NONNULL ((1, 2)));
253 _GL_CXXALIAS_SYS (stpncpy, char *,
254 (char *restrict __dst, char const *restrict __src,
257 _GL_CXXALIASWARN (stpncpy);
258 #elif defined GNULIB_POSIXCHECK
260 # if HAVE_RAW_DECL_STPNCPY
261 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
262 "use gnulib module stpncpy for portability");
266 #if defined GNULIB_POSIXCHECK
267 /* strchr() does not work with multibyte strings if the locale encoding is
268 GB18030 and the character to be searched is a digit. */
270 /* Assume strchr is always declared. */
271 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
272 "in some multibyte locales - "
273 "use mbschr if you care about internationalization");
276 /* Find the first occurrence of C in S or the final NUL byte. */
277 #if @GNULIB_STRCHRNUL@
278 # if ! @HAVE_STRCHRNUL@
279 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
281 _GL_ARG_NONNULL ((1)));
283 /* On some systems, this function is defined as an overloaded function:
284 extern "C++" { const char * std::strchrnul (const char *, int); }
285 extern "C++" { char * std::strchrnul (char *, int); } */
286 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
287 char *, (char const *__s, int __c_in),
288 char const *, (char const *__s, int __c_in));
289 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
290 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
291 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
292 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
294 _GL_CXXALIASWARN (strchrnul);
296 #elif defined GNULIB_POSIXCHECK
298 # if HAVE_RAW_DECL_STRCHRNUL
299 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
300 "use gnulib module strchrnul for portability");
304 /* Duplicate S, returning an identical malloc'd string. */
306 # if @REPLACE_STRDUP@
307 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
309 # define strdup rpl_strdup
311 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
312 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
314 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
315 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
317 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
319 _GL_CXXALIASWARN (strdup);
320 #elif defined GNULIB_POSIXCHECK
322 # if HAVE_RAW_DECL_STRDUP
323 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
324 "use gnulib module strdup for portability");
328 /* Append no more than N characters from SRC onto DEST. */
330 # if @REPLACE_STRNCAT@
331 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
333 # define strncat rpl_strncat
335 _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
336 _GL_ARG_NONNULL ((1, 2)));
337 _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
339 _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
341 _GL_CXXALIASWARN (strncat);
342 #elif defined GNULIB_POSIXCHECK
344 # if HAVE_RAW_DECL_STRNCAT
345 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
346 "use gnulib module strncat for portability");
350 /* Return a newly allocated copy of at most N bytes of STRING. */
352 # if @REPLACE_STRNDUP@
353 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
355 # define strndup rpl_strndup
357 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
358 _GL_ARG_NONNULL ((1)));
359 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
361 # if ! @HAVE_DECL_STRNDUP@
362 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
363 _GL_ARG_NONNULL ((1)));
365 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
367 _GL_CXXALIASWARN (strndup);
368 #elif defined GNULIB_POSIXCHECK
370 # if HAVE_RAW_DECL_STRNDUP
371 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
372 "use gnulib module strndup for portability");
376 /* Find the length (number of bytes) of STRING, but scan at most
377 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
380 # if @REPLACE_STRNLEN@
381 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
383 # define strnlen rpl_strnlen
385 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__string, size_t __maxlen)
387 _GL_ARG_NONNULL ((1)));
388 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__string, size_t __maxlen));
390 # if ! @HAVE_DECL_STRNLEN@
391 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
393 _GL_ARG_NONNULL ((1)));
395 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
397 _GL_CXXALIASWARN (strnlen);
398 #elif defined GNULIB_POSIXCHECK
400 # if HAVE_RAW_DECL_STRNLEN
401 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
402 "use gnulib module strnlen for portability");
406 #if defined GNULIB_POSIXCHECK
407 /* strcspn() assumes the second argument is a list of single-byte characters.
408 Even in this simple case, it does not work with multibyte strings if the
409 locale encoding is GB18030 and one of the characters to be searched is a
412 /* Assume strcspn is always declared. */
413 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
414 "in multibyte locales - "
415 "use mbscspn if you care about internationalization");
418 /* Find the first occurrence in S of any character in ACCEPT. */
420 # if ! @HAVE_STRPBRK@
421 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
423 _GL_ARG_NONNULL ((1, 2)));
425 /* On some systems, this function is defined as an overloaded function:
426 extern "C" { const char * strpbrk (const char *, const char *); }
427 extern "C++" { char * strpbrk (char *, const char *); } */
428 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
429 char *, (char const *__s, char const *__accept),
430 const char *, (char const *__s, char const *__accept));
431 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
432 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
433 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
434 _GL_CXXALIASWARN1 (strpbrk, char const *,
435 (char const *__s, char const *__accept));
437 _GL_CXXALIASWARN (strpbrk);
439 # if defined GNULIB_POSIXCHECK
440 /* strpbrk() assumes the second argument is a list of single-byte characters.
441 Even in this simple case, it does not work with multibyte strings if the
442 locale encoding is GB18030 and one of the characters to be searched is a
445 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
446 "in multibyte locales - "
447 "use mbspbrk if you care about internationalization");
449 #elif defined GNULIB_POSIXCHECK
451 # if HAVE_RAW_DECL_STRPBRK
452 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
453 "use gnulib module strpbrk for portability");
457 #if defined GNULIB_POSIXCHECK
458 /* strspn() assumes the second argument is a list of single-byte characters.
459 Even in this simple case, it cannot work with multibyte strings. */
461 /* Assume strspn is always declared. */
462 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
463 "in multibyte locales - "
464 "use mbsspn if you care about internationalization");
467 #if defined GNULIB_POSIXCHECK
468 /* strrchr() does not work with multibyte strings if the locale encoding is
469 GB18030 and the character to be searched is a digit. */
471 /* Assume strrchr is always declared. */
472 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
473 "in some multibyte locales - "
474 "use mbsrchr if you care about internationalization");
477 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
478 If one is found, overwrite it with a NUL, and advance *STRINGP
479 to point to the next char after it. Otherwise, set *STRINGP to NULL.
480 If *STRINGP was already NULL, nothing happens.
481 Return the old value of *STRINGP.
483 This is a variant of strtok() that is multithread-safe and supports
486 Caveat: It modifies the original string.
487 Caveat: These functions cannot be used on constant strings.
488 Caveat: The identity of the delimiting character is lost.
489 Caveat: It doesn't work with multibyte strings unless all of the delimiter
490 characters are ASCII characters < 0x30.
492 See also strtok_r(). */
495 _GL_FUNCDECL_SYS (strsep, char *,
496 (char **restrict __stringp, char const *restrict __delim)
497 _GL_ARG_NONNULL ((1, 2)));
499 _GL_CXXALIAS_SYS (strsep, char *,
500 (char **restrict __stringp, char const *restrict __delim));
501 _GL_CXXALIASWARN (strsep);
502 # if defined GNULIB_POSIXCHECK
504 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
505 "in multibyte locales - "
506 "use mbssep if you care about internationalization");
508 #elif defined GNULIB_POSIXCHECK
510 # if HAVE_RAW_DECL_STRSEP
511 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
512 "use gnulib module strsep for portability");
517 # if @REPLACE_STRSTR@
518 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
519 # define strstr rpl_strstr
521 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
523 _GL_ARG_NONNULL ((1, 2)));
524 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
526 /* On some systems, this function is defined as an overloaded function:
527 extern "C++" { const char * strstr (const char *, const char *); }
528 extern "C++" { char * strstr (char *, const char *); } */
529 _GL_CXXALIAS_SYS_CAST2 (strstr,
530 char *, (const char *haystack, const char *needle),
531 const char *, (const char *haystack, const char *needle));
533 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
534 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
535 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
536 _GL_CXXALIASWARN1 (strstr, const char *,
537 (const char *haystack, const char *needle));
539 _GL_CXXALIASWARN (strstr);
541 #elif defined GNULIB_POSIXCHECK
542 /* strstr() does not work with multibyte strings if the locale encoding is
543 different from UTF-8:
544 POSIX says that it operates on "strings", and "string" in POSIX is defined
545 as a sequence of bytes, not of characters. */
547 /* Assume strstr is always declared. */
548 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
549 "work correctly on character strings in most "
550 "multibyte locales - "
551 "use mbsstr if you care about internationalization, "
552 "or use strstr if you care about speed");
555 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
557 #if @GNULIB_STRCASESTR@
558 # if @REPLACE_STRCASESTR@
559 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
560 # define strcasestr rpl_strcasestr
562 _GL_FUNCDECL_RPL (strcasestr, char *,
563 (const char *haystack, const char *needle)
565 _GL_ARG_NONNULL ((1, 2)));
566 _GL_CXXALIAS_RPL (strcasestr, char *,
567 (const char *haystack, const char *needle));
569 # if ! @HAVE_STRCASESTR@
570 _GL_FUNCDECL_SYS (strcasestr, char *,
571 (const char *haystack, const char *needle)
573 _GL_ARG_NONNULL ((1, 2)));
575 /* On some systems, this function is defined as an overloaded function:
576 extern "C++" { const char * strcasestr (const char *, const char *); }
577 extern "C++" { char * strcasestr (char *, const char *); } */
578 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
579 char *, (const char *haystack, const char *needle),
580 const char *, (const char *haystack, const char *needle));
582 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
583 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
584 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
585 _GL_CXXALIASWARN1 (strcasestr, const char *,
586 (const char *haystack, const char *needle));
588 _GL_CXXALIASWARN (strcasestr);
590 #elif defined GNULIB_POSIXCHECK
591 /* strcasestr() does not work with multibyte strings:
592 It is a glibc extension, and glibc implements it only for unibyte
595 # if HAVE_RAW_DECL_STRCASESTR
596 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
597 "strings in multibyte locales - "
598 "use mbscasestr if you care about "
599 "internationalization, or use c-strcasestr if you want "
600 "a locale independent function");
604 /* Parse S into tokens separated by characters in DELIM.
605 If S is NULL, the saved pointer in SAVE_PTR is used as
606 the next starting point. For example:
607 char s[] = "-abc-=-def";
609 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
610 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
611 x = strtok_r(NULL, "=", &sp); // x = NULL
614 This is a variant of strtok() that is multithread-safe.
616 For the POSIX documentation for this function, see:
617 http://www.opengroup.org/susv3xsh/strtok.html
619 Caveat: It modifies the original string.
620 Caveat: These functions cannot be used on constant strings.
621 Caveat: The identity of the delimiting character is lost.
622 Caveat: It doesn't work with multibyte strings unless all of the delimiter
623 characters are ASCII characters < 0x30.
625 See also strsep(). */
626 #if @GNULIB_STRTOK_R@
627 # if @REPLACE_STRTOK_R@
628 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
630 # define strtok_r rpl_strtok_r
632 _GL_FUNCDECL_RPL (strtok_r, char *,
633 (char *restrict s, char const *restrict delim,
634 char **restrict save_ptr)
635 _GL_ARG_NONNULL ((2, 3)));
636 _GL_CXXALIAS_RPL (strtok_r, char *,
637 (char *restrict s, char const *restrict delim,
638 char **restrict save_ptr));
640 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
643 # if ! @HAVE_DECL_STRTOK_R@
644 _GL_FUNCDECL_SYS (strtok_r, char *,
645 (char *restrict s, char const *restrict delim,
646 char **restrict save_ptr)
647 _GL_ARG_NONNULL ((2, 3)));
649 _GL_CXXALIAS_SYS (strtok_r, char *,
650 (char *restrict s, char const *restrict delim,
651 char **restrict save_ptr));
653 _GL_CXXALIASWARN (strtok_r);
654 # if defined GNULIB_POSIXCHECK
655 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
656 "strings in multibyte locales - "
657 "use mbstok_r if you care about internationalization");
659 #elif defined GNULIB_POSIXCHECK
661 # if HAVE_RAW_DECL_STRTOK_R
662 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
663 "use gnulib module strtok_r for portability");
668 /* The following functions are not specified by POSIX. They are gnulib
672 /* Return the number of multibyte characters in the character string STRING.
673 This considers multibyte characters, unlike strlen, which counts bytes. */
674 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
677 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
678 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
679 # define mbslen rpl_mbslen
681 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
682 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
684 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
685 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
687 _GL_CXXALIASWARN (mbslen);
691 /* Return the number of multibyte characters in the character string starting
692 at STRING and ending at STRING + LEN. */
693 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
694 _GL_ARG_NONNULL ((1));
698 /* Locate the first single-byte character C in the character string STRING,
699 and return a pointer to it. Return NULL if C is not found in STRING.
700 Unlike strchr(), this function works correctly in multibyte locales with
701 encodings such as GB18030. */
703 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
704 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
706 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
707 _GL_ARG_NONNULL ((1)));
708 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
710 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
711 _GL_ARG_NONNULL ((1)));
712 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
714 _GL_CXXALIASWARN (mbschr);
718 /* Locate the last single-byte character C in the character string STRING,
719 and return a pointer to it. Return NULL if C is not found in STRING.
720 Unlike strrchr(), this function works correctly in multibyte locales with
721 encodings such as GB18030. */
723 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
724 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
726 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
727 _GL_ARG_NONNULL ((1)));
728 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
730 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
731 _GL_ARG_NONNULL ((1)));
732 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
734 _GL_CXXALIASWARN (mbsrchr);
738 /* Find the first occurrence of the character string NEEDLE in the character
739 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
740 Unlike strstr(), this function works correctly in multibyte locales with
741 encodings different from UTF-8. */
742 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
743 _GL_ARG_NONNULL ((1, 2));
746 #if @GNULIB_MBSCASECMP@
747 /* Compare the character strings S1 and S2, ignoring case, returning less than,
748 equal to or greater than zero if S1 is lexicographically less than, equal to
750 Note: This function may, in multibyte locales, return 0 for strings of
752 Unlike strcasecmp(), this function works correctly in multibyte locales. */
753 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
754 _GL_ARG_NONNULL ((1, 2));
757 #if @GNULIB_MBSNCASECMP@
758 /* Compare the initial segment of the character string S1 consisting of at most
759 N characters with the initial segment of the character string S2 consisting
760 of at most N characters, ignoring case, returning less than, equal to or
761 greater than zero if the initial segment of S1 is lexicographically less
762 than, equal to or greater than the initial segment of S2.
763 Note: This function may, in multibyte locales, return 0 for initial segments
764 of different lengths!
765 Unlike strncasecmp(), this function works correctly in multibyte locales.
766 But beware that N is not a byte count but a character count! */
767 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
768 _GL_ARG_NONNULL ((1, 2));
771 #if @GNULIB_MBSPCASECMP@
772 /* Compare the initial segment of the character string STRING consisting of
773 at most mbslen (PREFIX) characters with the character string PREFIX,
774 ignoring case. If the two match, return a pointer to the first byte
775 after this prefix in STRING. Otherwise, return NULL.
776 Note: This function may, in multibyte locales, return non-NULL if STRING
777 is of smaller length than PREFIX!
778 Unlike strncasecmp(), this function works correctly in multibyte
780 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
781 _GL_ARG_NONNULL ((1, 2));
784 #if @GNULIB_MBSCASESTR@
785 /* Find the first occurrence of the character string NEEDLE in the character
786 string HAYSTACK, using case-insensitive comparison.
787 Note: This function may, in multibyte locales, return success even if
788 strlen (haystack) < strlen (needle) !
789 Unlike strcasestr(), this function works correctly in multibyte locales. */
790 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
791 _GL_ARG_NONNULL ((1, 2));
795 /* Find the first occurrence in the character string STRING of any character
796 in the character string ACCEPT. Return the number of bytes from the
797 beginning of the string to this occurrence, or to the end of the string
799 Unlike strcspn(), this function works correctly in multibyte locales. */
800 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
801 _GL_ARG_NONNULL ((1, 2));
805 /* Find the first occurrence in the character string STRING of any character
806 in the character string ACCEPT. Return the pointer to it, or NULL if none
808 Unlike strpbrk(), this function works correctly in multibyte locales. */
810 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
811 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
813 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
814 _GL_ARG_NONNULL ((1, 2)));
815 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
817 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
818 _GL_ARG_NONNULL ((1, 2)));
819 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
821 _GL_CXXALIASWARN (mbspbrk);
825 /* Find the first occurrence in the character string STRING of any character
826 not in the character string REJECT. Return the number of bytes from the
827 beginning of the string to this occurrence, or to the end of the string
829 Unlike strspn(), this function works correctly in multibyte locales. */
830 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
831 _GL_ARG_NONNULL ((1, 2));
835 /* Search the next delimiter (multibyte character listed in the character
836 string DELIM) starting at the character string *STRINGP.
837 If one is found, overwrite it with a NUL, and advance *STRINGP to point
838 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
839 If *STRINGP was already NULL, nothing happens.
840 Return the old value of *STRINGP.
842 This is a variant of mbstok_r() that supports empty fields.
844 Caveat: It modifies the original string.
845 Caveat: These functions cannot be used on constant strings.
846 Caveat: The identity of the delimiting character is lost.
848 See also mbstok_r(). */
849 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
850 _GL_ARG_NONNULL ((1, 2));
853 #if @GNULIB_MBSTOK_R@
854 /* Parse the character string STRING into tokens separated by characters in
855 the character string DELIM.
856 If STRING is NULL, the saved pointer in SAVE_PTR is used as
857 the next starting point. For example:
858 char s[] = "-abc-=-def";
860 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
861 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
862 x = mbstok_r(NULL, "=", &sp); // x = NULL
865 Caveat: It modifies the original string.
866 Caveat: These functions cannot be used on constant strings.
867 Caveat: The identity of the delimiting character is lost.
869 See also mbssep(). */
870 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
871 _GL_ARG_NONNULL ((2, 3));
874 /* Map any int, typically from errno, into an error message. */
875 #if @GNULIB_STRERROR@
876 # if @REPLACE_STRERROR@
877 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
879 # define strerror rpl_strerror
881 _GL_FUNCDECL_RPL (strerror, char *, (int));
882 _GL_CXXALIAS_RPL (strerror, char *, (int));
884 _GL_CXXALIAS_SYS (strerror, char *, (int));
886 _GL_CXXALIASWARN (strerror);
887 #elif defined GNULIB_POSIXCHECK
889 /* Assume strerror is always declared. */
890 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
891 "use gnulib module strerror to guarantee non-NULL result");
894 #if @GNULIB_STRSIGNAL@
895 # if @REPLACE_STRSIGNAL@
896 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
897 # define strsignal rpl_strsignal
899 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
900 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
902 # if ! @HAVE_DECL_STRSIGNAL@
903 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
905 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
907 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
909 _GL_CXXALIASWARN (strsignal);
910 #elif defined GNULIB_POSIXCHECK
912 # if HAVE_RAW_DECL_STRSIGNAL
913 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
914 "use gnulib module strsignal for portability");
918 #if @GNULIB_STRVERSCMP@
919 # if !@HAVE_STRVERSCMP@
920 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
921 _GL_ARG_NONNULL ((1, 2)));
923 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
924 _GL_CXXALIASWARN (strverscmp);
925 #elif defined GNULIB_POSIXCHECK
927 # if HAVE_RAW_DECL_STRVERSCMP
928 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
929 "use gnulib module strverscmp for portability");
934 #endif /* _GL_STRING_H */
935 #endif /* _GL_STRING_H */