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 */
44 /* The attribute __pure__ was added in gcc 2.96. */
45 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
46 # define __pure__ /* empty */
51 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
53 /* The definition of _GL_ARG_NONNULL is copied here. */
55 /* The definition of _GL_WARN_ON_USE is copied here. */
58 /* Return the first instance of C within N bytes of S, or NULL. */
61 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
62 # define memchr rpl_memchr
64 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
65 __attribute__ ((__pure__))
66 _GL_ARG_NONNULL ((1)));
67 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
69 /* On some systems, this function is defined as an overloaded function:
70 extern "C" { const void * std::memchr (const void *, int, size_t); }
71 extern "C++" { void * std::memchr (void *, int, size_t); } */
72 _GL_CXXALIAS_SYS_CAST2 (memchr,
73 void *, (void const *__s, int __c, size_t __n),
74 void const *, (void const *__s, int __c, size_t __n));
76 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
77 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
78 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
79 _GL_CXXALIASWARN1 (memchr, void const *,
80 (void const *__s, int __c, size_t __n));
82 _GL_CXXALIASWARN (memchr);
84 #elif defined GNULIB_POSIXCHECK
86 /* Assume memchr is always declared. */
87 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
88 "use gnulib module memchr for portability" );
91 /* Return the first occurrence of NEEDLE in HAYSTACK. */
94 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
95 # define memmem rpl_memmem
97 _GL_FUNCDECL_RPL (memmem, void *,
98 (void const *__haystack, size_t __haystack_len,
99 void const *__needle, size_t __needle_len)
100 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3)));
101 _GL_CXXALIAS_RPL (memmem, void *,
102 (void const *__haystack, size_t __haystack_len,
103 void const *__needle, size_t __needle_len));
105 # if ! @HAVE_DECL_MEMMEM@
106 _GL_FUNCDECL_SYS (memmem, void *,
107 (void const *__haystack, size_t __haystack_len,
108 void const *__needle, size_t __needle_len)
109 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3)));
111 _GL_CXXALIAS_SYS (memmem, void *,
112 (void const *__haystack, size_t __haystack_len,
113 void const *__needle, size_t __needle_len));
115 _GL_CXXALIASWARN (memmem);
116 #elif defined GNULIB_POSIXCHECK
118 # if HAVE_RAW_DECL_MEMMEM
119 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
120 "use gnulib module memmem-simple for portability, "
121 "and module memmem for speed" );
125 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
126 last written byte. */
128 # if ! @HAVE_MEMPCPY@
129 _GL_FUNCDECL_SYS (mempcpy, void *,
130 (void *restrict __dest, void const *restrict __src,
132 _GL_ARG_NONNULL ((1, 2)));
134 _GL_CXXALIAS_SYS (mempcpy, void *,
135 (void *restrict __dest, void const *restrict __src,
137 _GL_CXXALIASWARN (mempcpy);
138 #elif defined GNULIB_POSIXCHECK
140 # if HAVE_RAW_DECL_MEMPCPY
141 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
142 "use gnulib module mempcpy for portability");
146 /* Search backwards through a block for a byte (specified as an int). */
148 # if ! @HAVE_DECL_MEMRCHR@
149 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
150 __attribute__ ((__pure__))
151 _GL_ARG_NONNULL ((1)));
153 /* On some systems, this function is defined as an overloaded function:
154 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
155 extern "C++" { void * std::memrchr (void *, int, size_t); } */
156 _GL_CXXALIAS_SYS_CAST2 (memrchr,
157 void *, (void const *, int, size_t),
158 void const *, (void const *, int, size_t));
159 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
160 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
161 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
162 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
164 _GL_CXXALIASWARN (memrchr);
166 #elif defined GNULIB_POSIXCHECK
168 # if HAVE_RAW_DECL_MEMRCHR
169 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
170 "use gnulib module memrchr for portability");
174 /* Find the first occurrence of C in S. More efficient than
175 memchr(S,C,N), at the expense of undefined behavior if C does not
176 occur within N bytes. */
177 #if @GNULIB_RAWMEMCHR@
178 # if ! @HAVE_RAWMEMCHR@
179 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
180 __attribute__ ((__pure__))
181 _GL_ARG_NONNULL ((1)));
183 /* On some systems, this function is defined as an overloaded function:
184 extern "C++" { const void * std::rawmemchr (const void *, int); }
185 extern "C++" { void * std::rawmemchr (void *, int); } */
186 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
187 void *, (void const *__s, int __c_in),
188 void const *, (void const *__s, int __c_in));
189 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
190 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
191 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
192 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
194 _GL_CXXALIASWARN (rawmemchr);
196 #elif defined GNULIB_POSIXCHECK
198 # if HAVE_RAW_DECL_RAWMEMCHR
199 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
200 "use gnulib module rawmemchr for portability");
204 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
207 _GL_FUNCDECL_SYS (stpcpy, char *,
208 (char *restrict __dst, char const *restrict __src)
209 _GL_ARG_NONNULL ((1, 2)));
211 _GL_CXXALIAS_SYS (stpcpy, char *,
212 (char *restrict __dst, char const *restrict __src));
213 _GL_CXXALIASWARN (stpcpy);
214 #elif defined GNULIB_POSIXCHECK
216 # if HAVE_RAW_DECL_STPCPY
217 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
218 "use gnulib module stpcpy for portability");
222 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
223 last non-NUL byte written into DST. */
225 # if @REPLACE_STPNCPY@
226 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
227 # define stpncpy rpl_stpncpy
229 _GL_FUNCDECL_RPL (stpncpy, char *,
230 (char *restrict __dst, char const *restrict __src,
232 _GL_ARG_NONNULL ((1, 2)));
233 _GL_CXXALIAS_RPL (stpncpy, char *,
234 (char *restrict __dst, char const *restrict __src,
237 # if ! @HAVE_STPNCPY@
238 _GL_FUNCDECL_SYS (stpncpy, char *,
239 (char *restrict __dst, char const *restrict __src,
241 _GL_ARG_NONNULL ((1, 2)));
243 _GL_CXXALIAS_SYS (stpncpy, char *,
244 (char *restrict __dst, char const *restrict __src,
247 _GL_CXXALIASWARN (stpncpy);
248 #elif defined GNULIB_POSIXCHECK
250 # if HAVE_RAW_DECL_STPNCPY
251 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
252 "use gnulib module stpncpy for portability");
256 #if defined GNULIB_POSIXCHECK
257 /* strchr() does not work with multibyte strings if the locale encoding is
258 GB18030 and the character to be searched is a digit. */
260 /* Assume strchr is always declared. */
261 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
262 "in some multibyte locales - "
263 "use mbschr if you care about internationalization");
266 /* Find the first occurrence of C in S or the final NUL byte. */
267 #if @GNULIB_STRCHRNUL@
268 # if ! @HAVE_STRCHRNUL@
269 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
270 __attribute__ ((__pure__))
271 _GL_ARG_NONNULL ((1)));
273 /* On some systems, this function is defined as an overloaded function:
274 extern "C++" { const char * std::strchrnul (const char *, int); }
275 extern "C++" { char * std::strchrnul (char *, int); } */
276 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
277 char *, (char const *__s, int __c_in),
278 char const *, (char const *__s, int __c_in));
279 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
280 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
281 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
282 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
284 _GL_CXXALIASWARN (strchrnul);
286 #elif defined GNULIB_POSIXCHECK
288 # if HAVE_RAW_DECL_STRCHRNUL
289 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
290 "use gnulib module strchrnul for portability");
294 /* Duplicate S, returning an identical malloc'd string. */
296 # if @REPLACE_STRDUP@
297 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
299 # define strdup rpl_strdup
301 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
302 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
304 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
305 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
307 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
309 _GL_CXXALIASWARN (strdup);
310 #elif defined GNULIB_POSIXCHECK
312 # if HAVE_RAW_DECL_STRDUP
313 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
314 "use gnulib module strdup for portability");
318 /* Return a newly allocated copy of at most N bytes of STRING. */
320 # if @REPLACE_STRNDUP@
321 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
323 # define strndup rpl_strndup
325 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
326 _GL_ARG_NONNULL ((1)));
327 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
329 # if ! @HAVE_DECL_STRNDUP@
330 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
331 _GL_ARG_NONNULL ((1)));
333 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
335 _GL_CXXALIASWARN (strndup);
336 #elif defined GNULIB_POSIXCHECK
338 # if HAVE_RAW_DECL_STRNDUP
339 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
340 "use gnulib module strndup for portability");
344 /* Find the length (number of bytes) of STRING, but scan at most
345 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
348 # if ! @HAVE_DECL_STRNLEN@
349 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
350 __attribute__ ((__pure__))
351 _GL_ARG_NONNULL ((1)));
353 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
354 _GL_CXXALIASWARN (strnlen);
355 #elif defined GNULIB_POSIXCHECK
357 # if HAVE_RAW_DECL_STRNLEN
358 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
359 "use gnulib module strnlen for portability");
363 #if defined GNULIB_POSIXCHECK
364 /* strcspn() assumes the second argument is a list of single-byte characters.
365 Even in this simple case, it does not work with multibyte strings if the
366 locale encoding is GB18030 and one of the characters to be searched is a
369 /* Assume strcspn is always declared. */
370 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
371 "in multibyte locales - "
372 "use mbscspn if you care about internationalization");
375 /* Find the first occurrence in S of any character in ACCEPT. */
377 # if ! @HAVE_STRPBRK@
378 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
379 __attribute__ ((__pure__))
380 _GL_ARG_NONNULL ((1, 2)));
382 /* On some systems, this function is defined as an overloaded function:
383 extern "C" { const char * strpbrk (const char *, const char *); }
384 extern "C++" { char * strpbrk (char *, const char *); } */
385 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
386 char *, (char const *__s, char const *__accept),
387 const char *, (char const *__s, char const *__accept));
388 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
389 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
390 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
391 _GL_CXXALIASWARN1 (strpbrk, char const *,
392 (char const *__s, char const *__accept));
394 _GL_CXXALIASWARN (strpbrk);
396 # if defined GNULIB_POSIXCHECK
397 /* strpbrk() assumes the second argument is a list of single-byte characters.
398 Even in this simple case, it does not work with multibyte strings if the
399 locale encoding is GB18030 and one of the characters to be searched is a
402 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
403 "in multibyte locales - "
404 "use mbspbrk if you care about internationalization");
406 #elif defined GNULIB_POSIXCHECK
408 # if HAVE_RAW_DECL_STRPBRK
409 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
410 "use gnulib module strpbrk for portability");
414 #if defined GNULIB_POSIXCHECK
415 /* strspn() assumes the second argument is a list of single-byte characters.
416 Even in this simple case, it cannot work with multibyte strings. */
418 /* Assume strspn is always declared. */
419 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
420 "in multibyte locales - "
421 "use mbsspn if you care about internationalization");
424 #if defined GNULIB_POSIXCHECK
425 /* strrchr() does not work with multibyte strings if the locale encoding is
426 GB18030 and the character to be searched is a digit. */
428 /* Assume strrchr is always declared. */
429 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
430 "in some multibyte locales - "
431 "use mbsrchr if you care about internationalization");
434 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
435 If one is found, overwrite it with a NUL, and advance *STRINGP
436 to point to the next char after it. Otherwise, set *STRINGP to NULL.
437 If *STRINGP was already NULL, nothing happens.
438 Return the old value of *STRINGP.
440 This is a variant of strtok() that is multithread-safe and supports
443 Caveat: It modifies the original string.
444 Caveat: These functions cannot be used on constant strings.
445 Caveat: The identity of the delimiting character is lost.
446 Caveat: It doesn't work with multibyte strings unless all of the delimiter
447 characters are ASCII characters < 0x30.
449 See also strtok_r(). */
452 _GL_FUNCDECL_SYS (strsep, char *,
453 (char **restrict __stringp, char const *restrict __delim)
454 _GL_ARG_NONNULL ((1, 2)));
456 _GL_CXXALIAS_SYS (strsep, char *,
457 (char **restrict __stringp, char const *restrict __delim));
458 _GL_CXXALIASWARN (strsep);
459 # if defined GNULIB_POSIXCHECK
461 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
462 "in multibyte locales - "
463 "use mbssep if you care about internationalization");
465 #elif defined GNULIB_POSIXCHECK
467 # if HAVE_RAW_DECL_STRSEP
468 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
469 "use gnulib module strsep for portability");
474 # if @REPLACE_STRSTR@
475 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
476 # define strstr rpl_strstr
478 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
479 __attribute__ ((__pure__))
480 _GL_ARG_NONNULL ((1, 2)));
481 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
483 /* On some systems, this function is defined as an overloaded function:
484 extern "C++" { const char * strstr (const char *, const char *); }
485 extern "C++" { char * strstr (char *, const char *); } */
486 _GL_CXXALIAS_SYS_CAST2 (strstr,
487 char *, (const char *haystack, const char *needle),
488 const char *, (const char *haystack, const char *needle));
490 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
491 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
492 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
493 _GL_CXXALIASWARN1 (strstr, const char *,
494 (const char *haystack, const char *needle));
496 _GL_CXXALIASWARN (strstr);
498 #elif defined GNULIB_POSIXCHECK
499 /* strstr() does not work with multibyte strings if the locale encoding is
500 different from UTF-8:
501 POSIX says that it operates on "strings", and "string" in POSIX is defined
502 as a sequence of bytes, not of characters. */
504 /* Assume strstr is always declared. */
505 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
506 "work correctly on character strings in most "
507 "multibyte locales - "
508 "use mbsstr if you care about internationalization, "
509 "or use strstr if you care about speed");
512 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
514 #if @GNULIB_STRCASESTR@
515 # if @REPLACE_STRCASESTR@
516 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
517 # define strcasestr rpl_strcasestr
519 _GL_FUNCDECL_RPL (strcasestr, char *,
520 (const char *haystack, const char *needle)
521 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
522 _GL_CXXALIAS_RPL (strcasestr, char *,
523 (const char *haystack, const char *needle));
525 # if ! @HAVE_STRCASESTR@
526 _GL_FUNCDECL_SYS (strcasestr, char *,
527 (const char *haystack, const char *needle)
528 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
530 /* On some systems, this function is defined as an overloaded function:
531 extern "C++" { const char * strcasestr (const char *, const char *); }
532 extern "C++" { char * strcasestr (char *, const char *); } */
533 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
534 char *, (const char *haystack, const char *needle),
535 const char *, (const char *haystack, const char *needle));
537 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
538 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
539 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
540 _GL_CXXALIASWARN1 (strcasestr, const char *,
541 (const char *haystack, const char *needle));
543 _GL_CXXALIASWARN (strcasestr);
545 #elif defined GNULIB_POSIXCHECK
546 /* strcasestr() does not work with multibyte strings:
547 It is a glibc extension, and glibc implements it only for unibyte
550 # if HAVE_RAW_DECL_STRCASESTR
551 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
552 "strings in multibyte locales - "
553 "use mbscasestr if you care about "
554 "internationalization, or use c-strcasestr if you want "
555 "a locale independent function");
559 /* Parse S into tokens separated by characters in DELIM.
560 If S is NULL, the saved pointer in SAVE_PTR is used as
561 the next starting point. For example:
562 char s[] = "-abc-=-def";
564 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
565 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
566 x = strtok_r(NULL, "=", &sp); // x = NULL
569 This is a variant of strtok() that is multithread-safe.
571 For the POSIX documentation for this function, see:
572 http://www.opengroup.org/susv3xsh/strtok.html
574 Caveat: It modifies the original string.
575 Caveat: These functions cannot be used on constant strings.
576 Caveat: The identity of the delimiting character is lost.
577 Caveat: It doesn't work with multibyte strings unless all of the delimiter
578 characters are ASCII characters < 0x30.
580 See also strsep(). */
581 #if @GNULIB_STRTOK_R@
582 # if @REPLACE_STRTOK_R@
583 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
585 # define strtok_r rpl_strtok_r
587 _GL_FUNCDECL_RPL (strtok_r, char *,
588 (char *restrict s, char const *restrict delim,
589 char **restrict save_ptr)
590 _GL_ARG_NONNULL ((2, 3)));
591 _GL_CXXALIAS_RPL (strtok_r, char *,
592 (char *restrict s, char const *restrict delim,
593 char **restrict save_ptr));
595 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
598 # if ! @HAVE_DECL_STRTOK_R@
599 _GL_FUNCDECL_SYS (strtok_r, char *,
600 (char *restrict s, char const *restrict delim,
601 char **restrict save_ptr)
602 _GL_ARG_NONNULL ((2, 3)));
604 _GL_CXXALIAS_SYS (strtok_r, char *,
605 (char *restrict s, char const *restrict delim,
606 char **restrict save_ptr));
608 _GL_CXXALIASWARN (strtok_r);
609 # if defined GNULIB_POSIXCHECK
610 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
611 "strings in multibyte locales - "
612 "use mbstok_r if you care about internationalization");
614 #elif defined GNULIB_POSIXCHECK
616 # if HAVE_RAW_DECL_STRTOK_R
617 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
618 "use gnulib module strtok_r for portability");
623 /* The following functions are not specified by POSIX. They are gnulib
627 /* Return the number of multibyte characters in the character string STRING.
628 This considers multibyte characters, unlike strlen, which counts bytes. */
629 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
632 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
633 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
634 # define mbslen rpl_mbslen
636 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
637 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
639 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
640 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
642 _GL_CXXALIASWARN (mbslen);
646 /* Return the number of multibyte characters in the character string starting
647 at STRING and ending at STRING + LEN. */
648 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
649 _GL_ARG_NONNULL ((1));
653 /* Locate the first single-byte character C in the character string STRING,
654 and return a pointer to it. Return NULL if C is not found in STRING.
655 Unlike strchr(), this function works correctly in multibyte locales with
656 encodings such as GB18030. */
658 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
659 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
661 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
662 _GL_ARG_NONNULL ((1)));
663 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
665 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
666 _GL_ARG_NONNULL ((1)));
667 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
669 _GL_CXXALIASWARN (mbschr);
673 /* Locate the last single-byte character C in the character string STRING,
674 and return a pointer to it. Return NULL if C is not found in STRING.
675 Unlike strrchr(), this function works correctly in multibyte locales with
676 encodings such as GB18030. */
678 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
679 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
681 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
682 _GL_ARG_NONNULL ((1)));
683 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
685 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
686 _GL_ARG_NONNULL ((1)));
687 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
689 _GL_CXXALIASWARN (mbsrchr);
693 /* Find the first occurrence of the character string NEEDLE in the character
694 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
695 Unlike strstr(), this function works correctly in multibyte locales with
696 encodings different from UTF-8. */
697 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
698 _GL_ARG_NONNULL ((1, 2));
701 #if @GNULIB_MBSCASECMP@
702 /* Compare the character strings S1 and S2, ignoring case, returning less than,
703 equal to or greater than zero if S1 is lexicographically less than, equal to
705 Note: This function may, in multibyte locales, return 0 for strings of
707 Unlike strcasecmp(), this function works correctly in multibyte locales. */
708 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
709 _GL_ARG_NONNULL ((1, 2));
712 #if @GNULIB_MBSNCASECMP@
713 /* Compare the initial segment of the character string S1 consisting of at most
714 N characters with the initial segment of the character string S2 consisting
715 of at most N characters, ignoring case, returning less than, equal to or
716 greater than zero if the initial segment of S1 is lexicographically less
717 than, equal to or greater than the initial segment of S2.
718 Note: This function may, in multibyte locales, return 0 for initial segments
719 of different lengths!
720 Unlike strncasecmp(), this function works correctly in multibyte locales.
721 But beware that N is not a byte count but a character count! */
722 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
723 _GL_ARG_NONNULL ((1, 2));
726 #if @GNULIB_MBSPCASECMP@
727 /* Compare the initial segment of the character string STRING consisting of
728 at most mbslen (PREFIX) characters with the character string PREFIX,
729 ignoring case, returning less than, equal to or greater than zero if this
730 initial segment is lexicographically less than, equal to or greater than
732 Note: This function may, in multibyte locales, return 0 if STRING is of
733 smaller length than PREFIX!
734 Unlike strncasecmp(), this function works correctly in multibyte
736 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
737 _GL_ARG_NONNULL ((1, 2));
740 #if @GNULIB_MBSCASESTR@
741 /* Find the first occurrence of the character string NEEDLE in the character
742 string HAYSTACK, using case-insensitive comparison.
743 Note: This function may, in multibyte locales, return success even if
744 strlen (haystack) < strlen (needle) !
745 Unlike strcasestr(), this function works correctly in multibyte locales. */
746 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
747 _GL_ARG_NONNULL ((1, 2));
751 /* Find the first occurrence in the character string STRING of any character
752 in the character string ACCEPT. Return the number of bytes from the
753 beginning of the string to this occurrence, or to the end of the string
755 Unlike strcspn(), this function works correctly in multibyte locales. */
756 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
757 _GL_ARG_NONNULL ((1, 2));
761 /* Find the first occurrence in the character string STRING of any character
762 in the character string ACCEPT. Return the pointer to it, or NULL if none
764 Unlike strpbrk(), this function works correctly in multibyte locales. */
766 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
767 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
769 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
770 _GL_ARG_NONNULL ((1, 2)));
771 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
773 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
774 _GL_ARG_NONNULL ((1, 2)));
775 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
777 _GL_CXXALIASWARN (mbspbrk);
781 /* Find the first occurrence in the character string STRING of any character
782 not in the character string REJECT. Return the number of bytes from the
783 beginning of the string to this occurrence, or to the end of the string
785 Unlike strspn(), this function works correctly in multibyte locales. */
786 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
787 _GL_ARG_NONNULL ((1, 2));
791 /* Search the next delimiter (multibyte character listed in the character
792 string DELIM) starting at the character string *STRINGP.
793 If one is found, overwrite it with a NUL, and advance *STRINGP to point
794 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
795 If *STRINGP was already NULL, nothing happens.
796 Return the old value of *STRINGP.
798 This is a variant of mbstok_r() that supports empty fields.
800 Caveat: It modifies the original string.
801 Caveat: These functions cannot be used on constant strings.
802 Caveat: The identity of the delimiting character is lost.
804 See also mbstok_r(). */
805 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
806 _GL_ARG_NONNULL ((1, 2));
809 #if @GNULIB_MBSTOK_R@
810 /* Parse the character string STRING into tokens separated by characters in
811 the character string DELIM.
812 If STRING is NULL, the saved pointer in SAVE_PTR is used as
813 the next starting point. For example:
814 char s[] = "-abc-=-def";
816 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
817 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
818 x = mbstok_r(NULL, "=", &sp); // x = NULL
821 Caveat: It modifies the original string.
822 Caveat: These functions cannot be used on constant strings.
823 Caveat: The identity of the delimiting character is lost.
825 See also mbssep(). */
826 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
827 _GL_ARG_NONNULL ((2, 3));
830 /* Map any int, typically from errno, into an error message. */
831 #if @GNULIB_STRERROR@
832 # if @REPLACE_STRERROR@
833 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
835 # define strerror rpl_strerror
837 _GL_FUNCDECL_RPL (strerror, char *, (int));
838 _GL_CXXALIAS_RPL (strerror, char *, (int));
840 _GL_CXXALIAS_SYS (strerror, char *, (int));
842 _GL_CXXALIASWARN (strerror);
843 #elif defined GNULIB_POSIXCHECK
845 /* Assume strerror is always declared. */
846 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
847 "use gnulib module strerror to guarantee non-NULL result");
850 #if @GNULIB_STRSIGNAL@
851 # if @REPLACE_STRSIGNAL@
852 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
853 # define strsignal rpl_strsignal
855 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
856 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
858 # if ! @HAVE_DECL_STRSIGNAL@
859 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
861 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
863 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
865 _GL_CXXALIASWARN (strsignal);
866 #elif defined GNULIB_POSIXCHECK
868 # if HAVE_RAW_DECL_STRSIGNAL
869 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
870 "use gnulib module strsignal for portability");
874 #if @GNULIB_STRVERSCMP@
875 # if !@HAVE_STRVERSCMP@
876 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
877 _GL_ARG_NONNULL ((1, 2)));
879 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
880 _GL_CXXALIASWARN (strverscmp);
881 #elif defined GNULIB_POSIXCHECK
883 # if HAVE_RAW_DECL_STRVERSCMP
884 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
885 "use gnulib module strverscmp for portability");
890 #endif /* _GL_STRING_H */
891 #endif /* _GL_STRING_H */