strsignal: work around NetBSD bug
[gnulib.git] / lib / string.in.h
1 /* A GNU-like <string.h>.
2
3    Copyright (C) 1995-1996, 2001-2010 Free Software Foundation, Inc.
4
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)
8    any later version.
9
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.
14
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.  */
18
19 #ifndef _GL_STRING_H
20
21 #if __GNUC__ >= 3
22 @PRAGMA_SYSTEM_HEADER@
23 #endif
24
25 /* The include_next requires a split double-inclusion guard.  */
26 #@INCLUDE_NEXT@ @NEXT_STRING_H@
27
28 #ifndef _GL_STRING_H
29 #define _GL_STRING_H
30
31 /* NetBSD 5.0 mis-defines NULL.  */
32 #include <stddef.h>
33
34 /* MirBSD defines mbslen as a macro.  */
35 #if @GNULIB_MBSLEN@ && defined __MirBSD__
36 # include <wchar.h>
37 #endif
38
39 #ifndef __attribute__
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 */
43 # endif
44 #endif
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__))
48 #else
49 # define _GL_ATTRIBUTE_PURE /* empty */
50 #endif
51
52 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>.  */
53 /* But avoid namespace pollution on glibc systems.  */
54 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK)  \
55     && ! defined __GLIBC__
56 # include <unistd.h>
57 #endif
58
59 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
60
61 /* The definition of _GL_ARG_NONNULL is copied here.  */
62
63 /* The definition of _GL_WARN_ON_USE is copied here.  */
64
65
66 /* Return the first instance of C within N bytes of S, or NULL.  */
67 #if @GNULIB_MEMCHR@
68 # if @REPLACE_MEMCHR@
69 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
70 #   define memchr rpl_memchr
71 #  endif
72 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
73                                   _GL_ATTRIBUTE_PURE
74                                   _GL_ARG_NONNULL ((1)));
75 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
76 # else
77 #  if ! @HAVE_MEMCHR@
78 _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
79                                   _GL_ATTRIBUTE_PURE
80                                   _GL_ARG_NONNULL ((1)));
81 #  endif
82   /* On some systems, this function is defined as an overloaded function:
83        extern "C" { const void * std::memchr (const void *, int, size_t); }
84        extern "C++" { void * std::memchr (void *, int, size_t); }  */
85 _GL_CXXALIAS_SYS_CAST2 (memchr,
86                         void *, (void const *__s, int __c, size_t __n),
87                         void const *, (void const *__s, int __c, size_t __n));
88 # endif
89 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
90      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
91 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
92 _GL_CXXALIASWARN1 (memchr, void const *,
93                    (void const *__s, int __c, size_t __n));
94 # else
95 _GL_CXXALIASWARN (memchr);
96 # endif
97 #elif defined GNULIB_POSIXCHECK
98 # undef memchr
99 /* Assume memchr is always declared.  */
100 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
101                  "use gnulib module memchr for portability" );
102 #endif
103
104 /* Return the first occurrence of NEEDLE in HAYSTACK.  */
105 #if @GNULIB_MEMMEM@
106 # if @REPLACE_MEMMEM@
107 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
108 #   define memmem rpl_memmem
109 #  endif
110 _GL_FUNCDECL_RPL (memmem, void *,
111                   (void const *__haystack, size_t __haystack_len,
112                    void const *__needle, size_t __needle_len)
113                   _GL_ATTRIBUTE_PURE
114                   _GL_ARG_NONNULL ((1, 3)));
115 _GL_CXXALIAS_RPL (memmem, void *,
116                   (void const *__haystack, size_t __haystack_len,
117                    void const *__needle, size_t __needle_len));
118 # else
119 #  if ! @HAVE_DECL_MEMMEM@
120 _GL_FUNCDECL_SYS (memmem, void *,
121                   (void const *__haystack, size_t __haystack_len,
122                    void const *__needle, size_t __needle_len)
123                   _GL_ATTRIBUTE_PURE
124                   _GL_ARG_NONNULL ((1, 3)));
125 #  endif
126 _GL_CXXALIAS_SYS (memmem, void *,
127                   (void const *__haystack, size_t __haystack_len,
128                    void const *__needle, size_t __needle_len));
129 # endif
130 _GL_CXXALIASWARN (memmem);
131 #elif defined GNULIB_POSIXCHECK
132 # undef memmem
133 # if HAVE_RAW_DECL_MEMMEM
134 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
135                  "use gnulib module memmem-simple for portability, "
136                  "and module memmem for speed" );
137 # endif
138 #endif
139
140 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
141    last written byte.  */
142 #if @GNULIB_MEMPCPY@
143 # if ! @HAVE_MEMPCPY@
144 _GL_FUNCDECL_SYS (mempcpy, void *,
145                   (void *restrict __dest, void const *restrict __src,
146                    size_t __n)
147                   _GL_ARG_NONNULL ((1, 2)));
148 # endif
149 _GL_CXXALIAS_SYS (mempcpy, void *,
150                   (void *restrict __dest, void const *restrict __src,
151                    size_t __n));
152 _GL_CXXALIASWARN (mempcpy);
153 #elif defined GNULIB_POSIXCHECK
154 # undef mempcpy
155 # if HAVE_RAW_DECL_MEMPCPY
156 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
157                  "use gnulib module mempcpy for portability");
158 # endif
159 #endif
160
161 /* Search backwards through a block for a byte (specified as an int).  */
162 #if @GNULIB_MEMRCHR@
163 # if ! @HAVE_DECL_MEMRCHR@
164 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
165                                    _GL_ATTRIBUTE_PURE
166                                    _GL_ARG_NONNULL ((1)));
167 # endif
168   /* On some systems, this function is defined as an overloaded function:
169        extern "C++" { const void * std::memrchr (const void *, int, size_t); }
170        extern "C++" { void * std::memrchr (void *, int, size_t); }  */
171 _GL_CXXALIAS_SYS_CAST2 (memrchr,
172                         void *, (void const *, int, size_t),
173                         void const *, (void const *, int, size_t));
174 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
175      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
176 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
177 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
178 # else
179 _GL_CXXALIASWARN (memrchr);
180 # endif
181 #elif defined GNULIB_POSIXCHECK
182 # undef memrchr
183 # if HAVE_RAW_DECL_MEMRCHR
184 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
185                  "use gnulib module memrchr for portability");
186 # endif
187 #endif
188
189 /* Find the first occurrence of C in S.  More efficient than
190    memchr(S,C,N), at the expense of undefined behavior if C does not
191    occur within N bytes.  */
192 #if @GNULIB_RAWMEMCHR@
193 # if ! @HAVE_RAWMEMCHR@
194 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
195                                      _GL_ATTRIBUTE_PURE
196                                      _GL_ARG_NONNULL ((1)));
197 # endif
198   /* On some systems, this function is defined as an overloaded function:
199        extern "C++" { const void * std::rawmemchr (const void *, int); }
200        extern "C++" { void * std::rawmemchr (void *, int); }  */
201 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
202                         void *, (void const *__s, int __c_in),
203                         void const *, (void const *__s, int __c_in));
204 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
205      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
206 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
207 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
208 # else
209 _GL_CXXALIASWARN (rawmemchr);
210 # endif
211 #elif defined GNULIB_POSIXCHECK
212 # undef rawmemchr
213 # if HAVE_RAW_DECL_RAWMEMCHR
214 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
215                  "use gnulib module rawmemchr for portability");
216 # endif
217 #endif
218
219 /* Copy SRC to DST, returning the address of the terminating '\0' in DST.  */
220 #if @GNULIB_STPCPY@
221 # if ! @HAVE_STPCPY@
222 _GL_FUNCDECL_SYS (stpcpy, char *,
223                   (char *restrict __dst, char const *restrict __src)
224                   _GL_ARG_NONNULL ((1, 2)));
225 # endif
226 _GL_CXXALIAS_SYS (stpcpy, char *,
227                   (char *restrict __dst, char const *restrict __src));
228 _GL_CXXALIASWARN (stpcpy);
229 #elif defined GNULIB_POSIXCHECK
230 # undef stpcpy
231 # if HAVE_RAW_DECL_STPCPY
232 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
233                  "use gnulib module stpcpy for portability");
234 # endif
235 #endif
236
237 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
238    last non-NUL byte written into DST.  */
239 #if @GNULIB_STPNCPY@
240 # if @REPLACE_STPNCPY@
241 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
242 #   undef stpncpy
243 #   define stpncpy rpl_stpncpy
244 #  endif
245 _GL_FUNCDECL_RPL (stpncpy, char *,
246                   (char *restrict __dst, char const *restrict __src,
247                    size_t __n)
248                   _GL_ARG_NONNULL ((1, 2)));
249 _GL_CXXALIAS_RPL (stpncpy, char *,
250                   (char *restrict __dst, char const *restrict __src,
251                    size_t __n));
252 # else
253 #  if ! @HAVE_STPNCPY@
254 _GL_FUNCDECL_SYS (stpncpy, char *,
255                   (char *restrict __dst, char const *restrict __src,
256                    size_t __n)
257                   _GL_ARG_NONNULL ((1, 2)));
258 #  endif
259 _GL_CXXALIAS_SYS (stpncpy, char *,
260                   (char *restrict __dst, char const *restrict __src,
261                    size_t __n));
262 # endif
263 _GL_CXXALIASWARN (stpncpy);
264 #elif defined GNULIB_POSIXCHECK
265 # undef stpncpy
266 # if HAVE_RAW_DECL_STPNCPY
267 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
268                  "use gnulib module stpncpy for portability");
269 # endif
270 #endif
271
272 #if defined GNULIB_POSIXCHECK
273 /* strchr() does not work with multibyte strings if the locale encoding is
274    GB18030 and the character to be searched is a digit.  */
275 # undef strchr
276 /* Assume strchr is always declared.  */
277 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
278                  "in some multibyte locales - "
279                  "use mbschr if you care about internationalization");
280 #endif
281
282 /* Find the first occurrence of C in S or the final NUL byte.  */
283 #if @GNULIB_STRCHRNUL@
284 # if ! @HAVE_STRCHRNUL@
285 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
286                                      _GL_ATTRIBUTE_PURE
287                                      _GL_ARG_NONNULL ((1)));
288 # endif
289   /* On some systems, this function is defined as an overloaded function:
290        extern "C++" { const char * std::strchrnul (const char *, int); }
291        extern "C++" { char * std::strchrnul (char *, int); }  */
292 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
293                         char *, (char const *__s, int __c_in),
294                         char const *, (char const *__s, int __c_in));
295 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
296      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
297 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
298 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
299 # else
300 _GL_CXXALIASWARN (strchrnul);
301 # endif
302 #elif defined GNULIB_POSIXCHECK
303 # undef strchrnul
304 # if HAVE_RAW_DECL_STRCHRNUL
305 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
306                  "use gnulib module strchrnul for portability");
307 # endif
308 #endif
309
310 /* Duplicate S, returning an identical malloc'd string.  */
311 #if @GNULIB_STRDUP@
312 # if @REPLACE_STRDUP@
313 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
314 #   undef strdup
315 #   define strdup rpl_strdup
316 #  endif
317 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
318 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
319 # else
320 #  if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
321     /* strdup exists as a function and as a macro.  Get rid of the macro.  */
322 #   undef strdup
323 #  endif
324 #  if !(@HAVE_DECL_STRDUP@ || defined strdup)
325 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
326 #  endif
327 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
328 # endif
329 _GL_CXXALIASWARN (strdup);
330 #elif defined GNULIB_POSIXCHECK
331 # undef strdup
332 # if HAVE_RAW_DECL_STRDUP
333 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
334                  "use gnulib module strdup for portability");
335 # endif
336 #endif
337
338 /* Append no more than N characters from SRC onto DEST.  */
339 #if @GNULIB_STRNCAT@
340 # if @REPLACE_STRNCAT@
341 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
342 #   undef strncat
343 #   define strncat rpl_strncat
344 #  endif
345 _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
346                                    _GL_ARG_NONNULL ((1, 2)));
347 _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
348 # else
349 _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
350 # endif
351 _GL_CXXALIASWARN (strncat);
352 #elif defined GNULIB_POSIXCHECK
353 # undef strncat
354 # if HAVE_RAW_DECL_STRNCAT
355 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
356                  "use gnulib module strncat for portability");
357 # endif
358 #endif
359
360 /* Return a newly allocated copy of at most N bytes of STRING.  */
361 #if @GNULIB_STRNDUP@
362 # if @REPLACE_STRNDUP@
363 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
364 #   undef strndup
365 #   define strndup rpl_strndup
366 #  endif
367 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
368                                    _GL_ARG_NONNULL ((1)));
369 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
370 # else
371 #  if ! @HAVE_DECL_STRNDUP@
372 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
373                                    _GL_ARG_NONNULL ((1)));
374 #  endif
375 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
376 # endif
377 _GL_CXXALIASWARN (strndup);
378 #elif defined GNULIB_POSIXCHECK
379 # undef strndup
380 # if HAVE_RAW_DECL_STRNDUP
381 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
382                  "use gnulib module strndup for portability");
383 # endif
384 #endif
385
386 /* Find the length (number of bytes) of STRING, but scan at most
387    MAXLEN bytes.  If no '\0' terminator is found in that many bytes,
388    return MAXLEN.  */
389 #if @GNULIB_STRNLEN@
390 # if @REPLACE_STRNLEN@
391 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
392 #   undef strnlen
393 #   define strnlen rpl_strnlen
394 #  endif
395 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__string, size_t __maxlen)
396                                    _GL_ATTRIBUTE_PURE
397                                    _GL_ARG_NONNULL ((1)));
398 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__string, size_t __maxlen));
399 # else
400 #  if ! @HAVE_DECL_STRNLEN@
401 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
402                                    _GL_ATTRIBUTE_PURE
403                                    _GL_ARG_NONNULL ((1)));
404 #  endif
405 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
406 # endif
407 _GL_CXXALIASWARN (strnlen);
408 #elif defined GNULIB_POSIXCHECK
409 # undef strnlen
410 # if HAVE_RAW_DECL_STRNLEN
411 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
412                  "use gnulib module strnlen for portability");
413 # endif
414 #endif
415
416 #if defined GNULIB_POSIXCHECK
417 /* strcspn() assumes the second argument is a list of single-byte characters.
418    Even in this simple case, it does not work with multibyte strings if the
419    locale encoding is GB18030 and one of the characters to be searched is a
420    digit.  */
421 # undef strcspn
422 /* Assume strcspn is always declared.  */
423 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
424                  "in multibyte locales - "
425                  "use mbscspn if you care about internationalization");
426 #endif
427
428 /* Find the first occurrence in S of any character in ACCEPT.  */
429 #if @GNULIB_STRPBRK@
430 # if ! @HAVE_STRPBRK@
431 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
432                                    _GL_ATTRIBUTE_PURE
433                                    _GL_ARG_NONNULL ((1, 2)));
434 # endif
435   /* On some systems, this function is defined as an overloaded function:
436        extern "C" { const char * strpbrk (const char *, const char *); }
437        extern "C++" { char * strpbrk (char *, const char *); }  */
438 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
439                         char *, (char const *__s, char const *__accept),
440                         const char *, (char const *__s, char const *__accept));
441 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
442      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
443 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
444 _GL_CXXALIASWARN1 (strpbrk, char const *,
445                    (char const *__s, char const *__accept));
446 # else
447 _GL_CXXALIASWARN (strpbrk);
448 # endif
449 # if defined GNULIB_POSIXCHECK
450 /* strpbrk() assumes the second argument is a list of single-byte characters.
451    Even in this simple case, it does not work with multibyte strings if the
452    locale encoding is GB18030 and one of the characters to be searched is a
453    digit.  */
454 #  undef strpbrk
455 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
456                  "in multibyte locales - "
457                  "use mbspbrk if you care about internationalization");
458 # endif
459 #elif defined GNULIB_POSIXCHECK
460 # undef strpbrk
461 # if HAVE_RAW_DECL_STRPBRK
462 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
463                  "use gnulib module strpbrk for portability");
464 # endif
465 #endif
466
467 #if defined GNULIB_POSIXCHECK
468 /* strspn() assumes the second argument is a list of single-byte characters.
469    Even in this simple case, it cannot work with multibyte strings.  */
470 # undef strspn
471 /* Assume strspn is always declared.  */
472 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
473                  "in multibyte locales - "
474                  "use mbsspn if you care about internationalization");
475 #endif
476
477 #if defined GNULIB_POSIXCHECK
478 /* strrchr() does not work with multibyte strings if the locale encoding is
479    GB18030 and the character to be searched is a digit.  */
480 # undef strrchr
481 /* Assume strrchr is always declared.  */
482 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
483                  "in some multibyte locales - "
484                  "use mbsrchr if you care about internationalization");
485 #endif
486
487 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
488    If one is found, overwrite it with a NUL, and advance *STRINGP
489    to point to the next char after it.  Otherwise, set *STRINGP to NULL.
490    If *STRINGP was already NULL, nothing happens.
491    Return the old value of *STRINGP.
492
493    This is a variant of strtok() that is multithread-safe and supports
494    empty fields.
495
496    Caveat: It modifies the original string.
497    Caveat: These functions cannot be used on constant strings.
498    Caveat: The identity of the delimiting character is lost.
499    Caveat: It doesn't work with multibyte strings unless all of the delimiter
500            characters are ASCII characters < 0x30.
501
502    See also strtok_r().  */
503 #if @GNULIB_STRSEP@
504 # if ! @HAVE_STRSEP@
505 _GL_FUNCDECL_SYS (strsep, char *,
506                   (char **restrict __stringp, char const *restrict __delim)
507                   _GL_ARG_NONNULL ((1, 2)));
508 # endif
509 _GL_CXXALIAS_SYS (strsep, char *,
510                   (char **restrict __stringp, char const *restrict __delim));
511 _GL_CXXALIASWARN (strsep);
512 # if defined GNULIB_POSIXCHECK
513 #  undef strsep
514 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
515                  "in multibyte locales - "
516                  "use mbssep if you care about internationalization");
517 # endif
518 #elif defined GNULIB_POSIXCHECK
519 # undef strsep
520 # if HAVE_RAW_DECL_STRSEP
521 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
522                  "use gnulib module strsep for portability");
523 # endif
524 #endif
525
526 #if @GNULIB_STRSTR@
527 # if @REPLACE_STRSTR@
528 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
529 #   define strstr rpl_strstr
530 #  endif
531 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
532                                   _GL_ATTRIBUTE_PURE
533                                   _GL_ARG_NONNULL ((1, 2)));
534 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
535 # else
536   /* On some systems, this function is defined as an overloaded function:
537        extern "C++" { const char * strstr (const char *, const char *); }
538        extern "C++" { char * strstr (char *, const char *); }  */
539 _GL_CXXALIAS_SYS_CAST2 (strstr,
540                         char *, (const char *haystack, const char *needle),
541                         const char *, (const char *haystack, const char *needle));
542 # endif
543 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
544      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
545 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
546 _GL_CXXALIASWARN1 (strstr, const char *,
547                    (const char *haystack, const char *needle));
548 # else
549 _GL_CXXALIASWARN (strstr);
550 # endif
551 #elif defined GNULIB_POSIXCHECK
552 /* strstr() does not work with multibyte strings if the locale encoding is
553    different from UTF-8:
554    POSIX says that it operates on "strings", and "string" in POSIX is defined
555    as a sequence of bytes, not of characters.  */
556 # undef strstr
557 /* Assume strstr is always declared.  */
558 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
559                  "work correctly on character strings in most "
560                  "multibyte locales - "
561                  "use mbsstr if you care about internationalization, "
562                  "or use strstr if you care about speed");
563 #endif
564
565 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
566    comparison.  */
567 #if @GNULIB_STRCASESTR@
568 # if @REPLACE_STRCASESTR@
569 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
570 #   define strcasestr rpl_strcasestr
571 #  endif
572 _GL_FUNCDECL_RPL (strcasestr, char *,
573                   (const char *haystack, const char *needle)
574                   _GL_ATTRIBUTE_PURE
575                   _GL_ARG_NONNULL ((1, 2)));
576 _GL_CXXALIAS_RPL (strcasestr, char *,
577                   (const char *haystack, const char *needle));
578 # else
579 #  if ! @HAVE_STRCASESTR@
580 _GL_FUNCDECL_SYS (strcasestr, char *,
581                   (const char *haystack, const char *needle)
582                   _GL_ATTRIBUTE_PURE
583                   _GL_ARG_NONNULL ((1, 2)));
584 #  endif
585   /* On some systems, this function is defined as an overloaded function:
586        extern "C++" { const char * strcasestr (const char *, const char *); }
587        extern "C++" { char * strcasestr (char *, const char *); }  */
588 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
589                         char *, (const char *haystack, const char *needle),
590                         const char *, (const char *haystack, const char *needle));
591 # endif
592 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
593      && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
594 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
595 _GL_CXXALIASWARN1 (strcasestr, const char *,
596                    (const char *haystack, const char *needle));
597 # else
598 _GL_CXXALIASWARN (strcasestr);
599 # endif
600 #elif defined GNULIB_POSIXCHECK
601 /* strcasestr() does not work with multibyte strings:
602    It is a glibc extension, and glibc implements it only for unibyte
603    locales.  */
604 # undef strcasestr
605 # if HAVE_RAW_DECL_STRCASESTR
606 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
607                  "strings in multibyte locales - "
608                  "use mbscasestr if you care about "
609                  "internationalization, or use c-strcasestr if you want "
610                  "a locale independent function");
611 # endif
612 #endif
613
614 /* Parse S into tokens separated by characters in DELIM.
615    If S is NULL, the saved pointer in SAVE_PTR is used as
616    the next starting point.  For example:
617         char s[] = "-abc-=-def";
618         char *sp;
619         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
620         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
621         x = strtok_r(NULL, "=", &sp);   // x = NULL
622                 // s = "abc\0-def\0"
623
624    This is a variant of strtok() that is multithread-safe.
625
626    For the POSIX documentation for this function, see:
627    http://www.opengroup.org/susv3xsh/strtok.html
628
629    Caveat: It modifies the original string.
630    Caveat: These functions cannot be used on constant strings.
631    Caveat: The identity of the delimiting character is lost.
632    Caveat: It doesn't work with multibyte strings unless all of the delimiter
633            characters are ASCII characters < 0x30.
634
635    See also strsep().  */
636 #if @GNULIB_STRTOK_R@
637 # if @REPLACE_STRTOK_R@
638 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
639 #   undef strtok_r
640 #   define strtok_r rpl_strtok_r
641 #  endif
642 _GL_FUNCDECL_RPL (strtok_r, char *,
643                   (char *restrict s, char const *restrict delim,
644                    char **restrict save_ptr)
645                   _GL_ARG_NONNULL ((2, 3)));
646 _GL_CXXALIAS_RPL (strtok_r, char *,
647                   (char *restrict s, char const *restrict delim,
648                    char **restrict save_ptr));
649 # else
650 #  if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
651 #   undef strtok_r
652 #  endif
653 #  if ! @HAVE_DECL_STRTOK_R@
654 _GL_FUNCDECL_SYS (strtok_r, char *,
655                   (char *restrict s, char const *restrict delim,
656                    char **restrict save_ptr)
657                   _GL_ARG_NONNULL ((2, 3)));
658 #  endif
659 _GL_CXXALIAS_SYS (strtok_r, char *,
660                   (char *restrict s, char const *restrict delim,
661                    char **restrict save_ptr));
662 # endif
663 _GL_CXXALIASWARN (strtok_r);
664 # if defined GNULIB_POSIXCHECK
665 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
666                  "strings in multibyte locales - "
667                  "use mbstok_r if you care about internationalization");
668 # endif
669 #elif defined GNULIB_POSIXCHECK
670 # undef strtok_r
671 # if HAVE_RAW_DECL_STRTOK_R
672 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
673                  "use gnulib module strtok_r for portability");
674 # endif
675 #endif
676
677
678 /* The following functions are not specified by POSIX.  They are gnulib
679    extensions.  */
680
681 #if @GNULIB_MBSLEN@
682 /* Return the number of multibyte characters in the character string STRING.
683    This considers multibyte characters, unlike strlen, which counts bytes.  */
684 # ifdef __MirBSD__  /* MirBSD defines mbslen as a macro.  Override it.  */
685 #  undef mbslen
686 # endif
687 # if @HAVE_MBSLEN@  /* AIX, OSF/1, MirBSD define mbslen already in libc.  */
688 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
689 #   define mbslen rpl_mbslen
690 #  endif
691 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
692 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
693 # else
694 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
695 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
696 # endif
697 _GL_CXXALIASWARN (mbslen);
698 #endif
699
700 #if @GNULIB_MBSNLEN@
701 /* Return the number of multibyte characters in the character string starting
702    at STRING and ending at STRING + LEN.  */
703 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
704      _GL_ARG_NONNULL ((1));
705 #endif
706
707 #if @GNULIB_MBSCHR@
708 /* Locate the first single-byte character C in the character string STRING,
709    and return a pointer to it.  Return NULL if C is not found in STRING.
710    Unlike strchr(), this function works correctly in multibyte locales with
711    encodings such as GB18030.  */
712 # if defined __hpux
713 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
714 #   define mbschr rpl_mbschr /* avoid collision with HP-UX function */
715 #  endif
716 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
717                                   _GL_ARG_NONNULL ((1)));
718 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
719 # else
720 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
721                                   _GL_ARG_NONNULL ((1)));
722 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
723 # endif
724 _GL_CXXALIASWARN (mbschr);
725 #endif
726
727 #if @GNULIB_MBSRCHR@
728 /* Locate the last single-byte character C in the character string STRING,
729    and return a pointer to it.  Return NULL if C is not found in STRING.
730    Unlike strrchr(), this function works correctly in multibyte locales with
731    encodings such as GB18030.  */
732 # if defined __hpux
733 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
734 #   define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
735 #  endif
736 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
737                                    _GL_ARG_NONNULL ((1)));
738 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
739 # else
740 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
741                                    _GL_ARG_NONNULL ((1)));
742 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
743 # endif
744 _GL_CXXALIASWARN (mbsrchr);
745 #endif
746
747 #if @GNULIB_MBSSTR@
748 /* Find the first occurrence of the character string NEEDLE in the character
749    string HAYSTACK.  Return NULL if NEEDLE is not found in HAYSTACK.
750    Unlike strstr(), this function works correctly in multibyte locales with
751    encodings different from UTF-8.  */
752 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
753      _GL_ARG_NONNULL ((1, 2));
754 #endif
755
756 #if @GNULIB_MBSCASECMP@
757 /* Compare the character strings S1 and S2, ignoring case, returning less than,
758    equal to or greater than zero if S1 is lexicographically less than, equal to
759    or greater than S2.
760    Note: This function may, in multibyte locales, return 0 for strings of
761    different lengths!
762    Unlike strcasecmp(), this function works correctly in multibyte locales.  */
763 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
764      _GL_ARG_NONNULL ((1, 2));
765 #endif
766
767 #if @GNULIB_MBSNCASECMP@
768 /* Compare the initial segment of the character string S1 consisting of at most
769    N characters with the initial segment of the character string S2 consisting
770    of at most N characters, ignoring case, returning less than, equal to or
771    greater than zero if the initial segment of S1 is lexicographically less
772    than, equal to or greater than the initial segment of S2.
773    Note: This function may, in multibyte locales, return 0 for initial segments
774    of different lengths!
775    Unlike strncasecmp(), this function works correctly in multibyte locales.
776    But beware that N is not a byte count but a character count!  */
777 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
778      _GL_ARG_NONNULL ((1, 2));
779 #endif
780
781 #if @GNULIB_MBSPCASECMP@
782 /* Compare the initial segment of the character string STRING consisting of
783    at most mbslen (PREFIX) characters with the character string PREFIX,
784    ignoring case.  If the two match, return a pointer to the first byte
785    after this prefix in STRING.  Otherwise, return NULL.
786    Note: This function may, in multibyte locales, return non-NULL if STRING
787    is of smaller length than PREFIX!
788    Unlike strncasecmp(), this function works correctly in multibyte
789    locales.  */
790 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
791      _GL_ARG_NONNULL ((1, 2));
792 #endif
793
794 #if @GNULIB_MBSCASESTR@
795 /* Find the first occurrence of the character string NEEDLE in the character
796    string HAYSTACK, using case-insensitive comparison.
797    Note: This function may, in multibyte locales, return success even if
798    strlen (haystack) < strlen (needle) !
799    Unlike strcasestr(), this function works correctly in multibyte locales.  */
800 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
801      _GL_ARG_NONNULL ((1, 2));
802 #endif
803
804 #if @GNULIB_MBSCSPN@
805 /* Find the first occurrence in the character string STRING of any character
806    in the character string ACCEPT.  Return the number of bytes from the
807    beginning of the string to this occurrence, or to the end of the string
808    if none exists.
809    Unlike strcspn(), this function works correctly in multibyte locales.  */
810 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
811      _GL_ARG_NONNULL ((1, 2));
812 #endif
813
814 #if @GNULIB_MBSPBRK@
815 /* Find the first occurrence in the character string STRING of any character
816    in the character string ACCEPT.  Return the pointer to it, or NULL if none
817    exists.
818    Unlike strpbrk(), this function works correctly in multibyte locales.  */
819 # if defined __hpux
820 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
821 #   define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
822 #  endif
823 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
824                                    _GL_ARG_NONNULL ((1, 2)));
825 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
826 # else
827 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
828                                    _GL_ARG_NONNULL ((1, 2)));
829 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
830 # endif
831 _GL_CXXALIASWARN (mbspbrk);
832 #endif
833
834 #if @GNULIB_MBSSPN@
835 /* Find the first occurrence in the character string STRING of any character
836    not in the character string REJECT.  Return the number of bytes from the
837    beginning of the string to this occurrence, or to the end of the string
838    if none exists.
839    Unlike strspn(), this function works correctly in multibyte locales.  */
840 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
841      _GL_ARG_NONNULL ((1, 2));
842 #endif
843
844 #if @GNULIB_MBSSEP@
845 /* Search the next delimiter (multibyte character listed in the character
846    string DELIM) starting at the character string *STRINGP.
847    If one is found, overwrite it with a NUL, and advance *STRINGP to point
848    to the next multibyte character after it.  Otherwise, set *STRINGP to NULL.
849    If *STRINGP was already NULL, nothing happens.
850    Return the old value of *STRINGP.
851
852    This is a variant of mbstok_r() that supports empty fields.
853
854    Caveat: It modifies the original string.
855    Caveat: These functions cannot be used on constant strings.
856    Caveat: The identity of the delimiting character is lost.
857
858    See also mbstok_r().  */
859 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
860      _GL_ARG_NONNULL ((1, 2));
861 #endif
862
863 #if @GNULIB_MBSTOK_R@
864 /* Parse the character string STRING into tokens separated by characters in
865    the character string DELIM.
866    If STRING is NULL, the saved pointer in SAVE_PTR is used as
867    the next starting point.  For example:
868         char s[] = "-abc-=-def";
869         char *sp;
870         x = mbstok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
871         x = mbstok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
872         x = mbstok_r(NULL, "=", &sp);   // x = NULL
873                 // s = "abc\0-def\0"
874
875    Caveat: It modifies the original string.
876    Caveat: These functions cannot be used on constant strings.
877    Caveat: The identity of the delimiting character is lost.
878
879    See also mbssep().  */
880 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
881      _GL_ARG_NONNULL ((2, 3));
882 #endif
883
884 /* Map any int, typically from errno, into an error message.  */
885 #if @GNULIB_STRERROR@
886 # if @REPLACE_STRERROR@
887 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
888 #   undef strerror
889 #   define strerror rpl_strerror
890 #  endif
891 _GL_FUNCDECL_RPL (strerror, char *, (int));
892 _GL_CXXALIAS_RPL (strerror, char *, (int));
893 # else
894 _GL_CXXALIAS_SYS (strerror, char *, (int));
895 # endif
896 _GL_CXXALIASWARN (strerror);
897 #elif defined GNULIB_POSIXCHECK
898 # undef strerror
899 /* Assume strerror is always declared.  */
900 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
901                  "use gnulib module strerror to guarantee non-NULL result");
902 #endif
903
904 #if @GNULIB_STRSIGNAL@
905 # if @REPLACE_STRSIGNAL@
906 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
907 #   define strsignal rpl_strsignal
908 #  endif
909 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
910 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
911 # else
912 #  if ! @HAVE_DECL_STRSIGNAL@
913 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
914 #  endif
915 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
916    'const char *'.  */
917 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
918 # endif
919 _GL_CXXALIASWARN (strsignal);
920 #elif defined GNULIB_POSIXCHECK
921 # undef strsignal
922 # if HAVE_RAW_DECL_STRSIGNAL
923 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
924                  "use gnulib module strsignal for portability");
925 # endif
926 #endif
927
928 #if @GNULIB_STRVERSCMP@
929 # if !@HAVE_STRVERSCMP@
930 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
931                                    _GL_ARG_NONNULL ((1, 2)));
932 # endif
933 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
934 _GL_CXXALIASWARN (strverscmp);
935 #elif defined GNULIB_POSIXCHECK
936 # undef strverscmp
937 # if HAVE_RAW_DECL_STRVERSCMP
938 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
939                  "use gnulib module strverscmp for portability");
940 # endif
941 #endif
942
943
944 #endif /* _GL_STRING_H */
945 #endif /* _GL_STRING_H */