1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2007 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 #include @ABSOLUTE_STRING_H@
25 /* GL_LINK_WARNING("literal string") arranges to emit the literal string as
26 a linker warning on most glibc systems.
27 We use a linker warning rather than a preprocessor warning, because
28 #warning cannot be used inside macros. */
29 #ifndef GL_LINK_WARNING
30 /* This works on platforms with GNU ld and ELF object format.
31 Testing __GLIBC__ is sufficient for asserting that GNU ld is in use.
32 Testing __ELF__ guarantees the ELF object format.
33 Testing __GNUC__ is necessary for the compound expression syntax. */
34 # if defined __GLIBC__ && defined __ELF__ && defined __GNUC__
35 # define GL_LINK_WARNING(message) \
36 GL_LINK_WARNING1 (__FILE__, __LINE__, message)
37 # define GL_LINK_WARNING1(file, line, message) \
38 GL_LINK_WARNING2 (file, line, message) /* macroexpand file and line */
39 # define GL_LINK_WARNING2(file, line, message) \
40 GL_LINK_WARNING3 (file ":" #line ": " message)
41 # define GL_LINK_WARNING3(message) \
42 ({ static const char warning[sizeof (message)] \
43 __attribute__ ((__unused__, \
44 __section__ (".gnu.warning"), \
50 # define GL_LINK_WARNING(message) ((void) 0)
59 /* Return the first occurrence of NEEDLE in HAYSTACK. */
61 # if ! @HAVE_DECL_MEMMEM@
62 extern void *memmem (void const *__haystack, size_t __haystack_len,
63 void const *__needle, size_t __needle_len);
65 #elif defined GNULIB_POSIXCHECK
67 # define memmem memmem_is_unportable__use_gnulib_module_memmem_for_portability
70 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
74 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
77 #elif defined GNULIB_POSIXCHECK
79 # define mempcpy mempcpy_is_unportable__use_gnulib_module_mempcpy_for_portability
82 /* Search backwards through a block for a byte (specified as an int). */
84 # if ! @HAVE_DECL_MEMRCHR@
85 extern void *memrchr (void const *, int, size_t);
87 #elif defined GNULIB_POSIXCHECK
89 # define memrchr memrchr_is_unportable__use_gnulib_module_memrchr_for_portability
92 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
95 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
97 #elif defined GNULIB_POSIXCHECK
99 # define stpcpy stpcpy_is_unportable__use_gnulib_module_stpcpy_for_portability
102 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
103 last non-NUL byte written into DST. */
105 # if ! @HAVE_STPNCPY@
106 # define stpncpy gnu_stpncpy
107 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
110 #elif defined GNULIB_POSIXCHECK
112 # define stpncpy stpncpy_is_unportable__use_gnulib_module_stpncpy_for_portability
115 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
116 greater than zero if S1 is lexicographically less than, equal to or greater
118 Note: This function may, in multibyte locales, return 0 for strings of
120 No known system has a strcasecmp() function that works correctly in
121 multibyte locales. Therefore use our version always, if the
122 strcase module is available. */
124 # if @REPLACE_STRCASECMP@
125 # define strcasecmp rpl_strcasecmp
126 extern int strcasecmp (char const *__s1, char const *__s2);
128 #elif defined GNULIB_POSIXCHECK
130 # define strcasecmp(a,b) \
131 (GL_LINK_WARNING ("strcasecmp is often incorrectly implemented for multibyte locales - use gnulib module 'strcase' for correct and portable internationalization"), \
135 /* Compare no more than N bytes of strings S1 and S2, ignoring case,
136 returning less than, equal to or greater than zero if S1 is
137 lexicographically less than, equal to or greater than S2.
138 Note: This function cannot work correctly in multibyte locales. */
140 # if ! @HAVE_DECL_STRNCASECMP@
141 extern int strncasecmp (char const *__s1, char const *__s2, size_t __n);
144 #if defined GNULIB_POSIXCHECK
146 # define strncasecmp(a,b) \
147 (GL_LINK_WARNING ("strncasecmp cannot work correctly in multibyte locales - don't use it if you care about internationalization"), \
151 #if defined GNULIB_POSIXCHECK
152 /* strchr() does not work with multibyte strings if the locale encoding is
153 GB18030 and the character to be searched is a digit. */
155 # define strchr(s,c) \
156 (GL_LINK_WARNING ("strchr cannot work correctly on character strings in some multibyte locales - use mbschr if you care about internationalization"), \
160 /* Find the first occurrence of C in S or the final NUL byte. */
161 #if @GNULIB_STRCHRNUL@
162 # if ! @HAVE_STRCHRNUL@
163 extern char *strchrnul (char const *__s, int __c_in);
165 #elif defined GNULIB_POSIXCHECK
167 # define strchrnul strchrnul_is_unportable__use_gnulib_module_strchrnul_for_portability
170 /* Duplicate S, returning an identical malloc'd string. */
172 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
173 extern char *strdup (char const *__s);
175 #elif defined GNULIB_POSIXCHECK
177 # define strdup strdup_is_unportable__use_gnulib_module_strdup_for_portability
180 /* Return a newly allocated copy of at most N bytes of STRING. */
182 # if ! @HAVE_STRNDUP@
184 # define strndup rpl_strndup
185 # if ! @HAVE_DECL_STRNDUP@
186 extern char *strndup (char const *__string, size_t __n);
189 #elif defined GNULIB_POSIXCHECK
191 # define strndup strndup_is_unportable__use_gnulib_module_strndup_for_portability
194 /* Find the length (number of bytes) of STRING, but scan at most
195 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
198 # if ! @HAVE_DECL_STRNLEN@
199 extern size_t strnlen (char const *__string, size_t __maxlen);
201 #elif defined GNULIB_POSIXCHECK
203 # define strnlen strnlen_is_unportable__use_gnulib_module_strnlen_for_portability
206 /* Find the first occurrence in S of any character in ACCEPT. */
208 # if ! @HAVE_STRPBRK@
209 extern char *strpbrk (char const *__s, char const *__accept);
211 #elif defined GNULIB_POSIXCHECK
213 # define strpbrk strpbrk_is_unportable__use_gnulib_module_strpbrk_for_portability
216 #if defined GNULIB_POSIXCHECK
217 /* strrchr() does not work with multibyte strings if the locale encoding is
218 GB18030 and the character to be searched is a digit. */
220 # define strrchr(s,c) \
221 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings in some multibyte locales - use mbsrchr if you care about internationalization"), \
225 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
226 If one is found, overwrite it with a NUL, and advance *STRINGP
227 to point to the next char after it. Otherwise, set *STRINGP to NULL.
228 If *STRINGP was already NULL, nothing happens.
229 Return the old value of *STRINGP.
231 This is a variant of strtok() that is multithread-safe and supports
234 Caveat: It modifies the original string.
235 Caveat: These functions cannot be used on constant strings.
236 Caveat: The identity of the delimiting character is lost.
237 Caveat: It doesn't work with multibyte strings unless all of the delimiter
238 characters are ASCII characters < 0x30.
240 See also strtok_r(). */
243 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
245 #elif defined GNULIB_POSIXCHECK
247 # define strsep strsep_is_unportable__use_gnulib_module_strsep_for_portability
250 #if defined GNULIB_POSIXCHECK
251 /* strstr() does not work with multibyte strings if the locale encoding is
252 different from UTF-8:
253 POSIX says that it operates on "strings", and "string" in POSIX is defined
254 as a sequence of bytes, not of characters. */
256 # define strstr(a,b) \
257 (GL_LINK_WARNING ("strstr cannot work correctly on character strings in most multibyte locales - use mbsstr if you care about internationalization"), \
261 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
263 Note: This function may, in multibyte locales, return success even if
264 strlen (haystack) < strlen (needle) ! */
265 #if @GNULIB_STRCASESTR@
266 # if @REPLACE_STRCASESTR@
268 # define strcasestr rpl_strcasestr
269 extern char *strcasestr (const char *haystack, const char *needle);
271 #elif defined GNULIB_POSIXCHECK
273 # define strcasestr(a,b) \
274 (GL_LINK_WARNING ("strcasestr is often incorrectly implemented for multibyte locales - use gnulib module 'strcasestr' for correct and portable internationalization"), \
278 /* Parse S into tokens separated by characters in DELIM.
279 If S is NULL, the saved pointer in SAVE_PTR is used as
280 the next starting point. For example:
281 char s[] = "-abc-=-def";
283 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
284 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
285 x = strtok_r(NULL, "=", &sp); // x = NULL
288 This is a variant of strtok() that is multithread-safe.
290 For the POSIX documentation for this function, see:
291 http://www.opengroup.org/susv3xsh/strtok.html
293 Caveat: It modifies the original string.
294 Caveat: These functions cannot be used on constant strings.
295 Caveat: The identity of the delimiting character is lost.
296 Caveat: It doesn't work with multibyte strings unless all of the delimiter
297 characters are ASCII characters < 0x30.
299 See also strsep(). */
300 #if @GNULIB_STRTOK_R@
301 # if ! @HAVE_DECL_STRTOK_R@
302 extern char *strtok_r (char *restrict __s, char const *restrict __sep,
303 char **restrict __lasts);
305 #elif defined GNULIB_POSIXCHECK
307 # define strtok_r strtok_r_is_unportable__use_gnulib_module_strtok_r_for_portability
311 /* The following functions are not specified by POSIX. They are gnulib
315 /* Locate the first single-byte character C in the character string STRING,
316 and return a pointer to it. Return NULL if C is not found in STRING.
317 Unlike strchr(), this function works correctly in multibyte locales with
318 encodings such as GB18030. */
319 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
320 extern char * mbschr (const char *string, int c);
324 /* Locate the last single-byte character C in the character string STRING,
325 and return a pointer to it. Return NULL if C is not found in STRING.
326 Unlike strrchr(), this function works correctly in multibyte locales with
327 encodings such as GB18030. */
328 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
329 extern char * mbsrchr (const char *string, int c);
333 /* Find the first occurrence of the character string NEEDLE in the character
334 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
335 Unlike strstr(), this function works correctly in multibyte locales with
336 encodings different from UTF-8. */
337 extern char * mbsstr (const char *haystack, const char *needle);