Turn the errors for use of strstr and similar into conditional warnings.
[pspp] / lib / string_.h
1 /* A GNU-like <string.h>.
2
3    Copyright (C) 1995-1996, 2001-2007 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 #define _GL_STRING_H
21
22 #include @ABSOLUTE_STRING_H@
23
24
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"),         \
45                           __aligned__ (1)))                     \
46           = message "\n";                                       \
47         (void)0;                                                \
48      })
49 # else
50 #  define GL_LINK_WARNING(message) ((void) 0)
51 # endif
52 #endif
53
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 /* Return the first occurrence of NEEDLE in HAYSTACK.  */
60 #if @GNULIB_MEMMEM@
61 # if ! @HAVE_DECL_MEMMEM@
62 extern void *memmem (void const *__haystack, size_t __haystack_len,
63                      void const *__needle, size_t __needle_len);
64 # endif
65 #else
66 # undef memmem
67 # define memmem memmem_is_unportable__use_gnulib_module_memmem_for_portability
68 #endif
69
70 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
71    last written byte.  */
72 #if @GNULIB_MEMPCPY@
73 # if ! @HAVE_MEMPCPY@
74 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
75                       size_t __n);
76 # endif
77 #else
78 # undef mempcpy
79 # define mempcpy mempcpy_is_unportable__use_gnulib_module_mempcpy_for_portability
80 #endif
81
82 /* Search backwards through a block for a byte (specified as an int).  */
83 #if @GNULIB_MEMRCHR@
84 # if ! @HAVE_DECL_MEMRCHR@
85 extern void *memrchr (void const *, int, size_t);
86 # endif
87 #else
88 # undef memrchr
89 # define memrchr memrchr_is_unportable__use_gnulib_module_memrchr_for_portability
90 #endif
91
92 /* Copy SRC to DST, returning the address of the terminating '\0' in DST.  */
93 #if @GNULIB_STPCPY@
94 # if ! @HAVE_STPCPY@
95 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
96 # endif
97 #else
98 # undef stpcpy
99 # define stpcpy stpcpy_is_unportable__use_gnulib_module_stpcpy_for_portability
100 #endif
101
102 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
103    last non-NUL byte written into DST.  */
104 #if @GNULIB_STPNCPY@
105 # if ! @HAVE_STPNCPY@
106 #  define stpncpy gnu_stpncpy
107 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
108                       size_t __n);
109 # endif
110 #else
111 # undef stpncpy
112 # define stpncpy stpncpy_is_unportable__use_gnulib_module_stpncpy_for_portability
113 #endif
114
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
117    than S2.
118    Note: This function may, in multibyte locales, return 0 for strings of
119    different lengths!
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.  */
123 #if @GNULIB_STRCASE@
124 # if @REPLACE_STRCASECMP@
125 #  define strcasecmp rpl_strcasecmp
126 extern int strcasecmp (char const *__s1, char const *__s2);
127 # endif
128 #elif defined GNULIB_POSIXCHECK
129 # undef strcasecmp
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"), \
132      strcasecmp (a, b))
133 #endif
134
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.  */
139 #if @GNULIB_STRCASE@
140 # if ! @HAVE_DECL_STRNCASECMP@
141 extern int strncasecmp (char const *__s1, char const *__s2, size_t __n);
142 # endif
143 #endif
144 #if defined GNULIB_POSIXCHECK
145 # undef strncasecmp
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"), \
148      strncasecmp (a, b))
149 #endif
150
151 /* Find the first occurrence of C in S or the final NUL byte.  */
152 #if @GNULIB_STRCHRNUL@
153 # if ! @HAVE_STRCHRNUL@
154 extern char *strchrnul (char const *__s, int __c_in);
155 # endif
156 #else
157 # undef strchrnul
158 # define strchrnul strchrnul_is_unportable__use_gnulib_module_strchrnul_for_portability
159 #endif
160
161 /* Duplicate S, returning an identical malloc'd string.  */
162 #if @GNULIB_STRDUP@
163 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
164 extern char *strdup (char const *__s);
165 # endif
166 #else
167 # undef strdup
168 # define strdup strdup_is_unportable__use_gnulib_module_strdup_for_portability
169 #endif
170
171 /* Return a newly allocated copy of at most N bytes of STRING.  */
172 #if @GNULIB_STRNDUP@
173 # if ! @HAVE_STRNDUP@
174 #  undef strndup
175 #  define strndup rpl_strndup
176 #  if ! @HAVE_DECL_STRNDUP@
177 extern char *strndup (char const *__string, size_t __n);
178 #  endif
179 # endif
180 #else
181 # undef strndup
182 # define strndup strndup_is_unportable__use_gnulib_module_strndup_for_portability
183 #endif
184
185 /* Find the length (number of bytes) of STRING, but scan at most
186    MAXLEN bytes.  If no '\0' terminator is found in that many bytes,
187    return MAXLEN.  */
188 #if @GNULIB_STRNLEN@
189 # if ! @HAVE_DECL_STRNLEN@
190 extern size_t strnlen (char const *__string, size_t __maxlen);
191 # endif
192 #else
193 # undef strnlen
194 # define strnlen strnlen_is_unportable__use_gnulib_module_strnlen_for_portability
195 #endif
196
197 /* Find the first occurrence in S of any character in ACCEPT.  */
198 #if @GNULIB_STRPBRK@
199 # if ! @HAVE_STRPBRK@
200 extern char *strpbrk (char const *__s, char const *__accept);
201 # endif
202 #else
203 # undef strpbrk
204 # define strpbrk strpbrk_is_unportable__use_gnulib_module_strpbrk_for_portability
205 #endif
206
207 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
208    If one is found, overwrite it with a NUL, and advance *STRINGP
209    to point to the next char after it.  Otherwise, set *STRINGP to NULL.
210    If *STRINGP was already NULL, nothing happens.
211    Return the old value of *STRINGP.
212
213    This is a variant of strtok() that is multithread-safe and supports
214    empty fields.
215
216    Caveat: It modifies the original string.
217    Caveat: These functions cannot be used on constant strings.
218    Caveat: The identity of the delimiting character is lost.
219    Caveat: It doesn't work with multibyte strings unless all of the delimiter
220            characters are ASCII characters < 0x30.
221
222    See also strtok_r().  */
223 #if @GNULIB_STRSEP@
224 # if ! @HAVE_STRSEP@
225 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
226 # endif
227 #else
228 # undef strsep
229 # define strsep strsep_is_unportable__use_gnulib_module_strsep_for_portability
230 #endif
231
232 /* Find the first occurrence of NEEDLE in HAYSTACK.
233    No known system has a strstr() function that works correctly in
234    multibyte locales.  Therefore use our version always, if the strstr
235    module is available.  */
236 #if @GNULIB_STRSTR@
237 # if @REPLACE_STRSTR@
238 #  undef strstr
239 #  define strstr rpl_strstr
240 extern char *strstr (char const *__haystack, char const *__needle);
241 # endif
242 #elif defined GNULIB_POSIXCHECK
243 # undef strstr
244 # define strstr(a,b) \
245     (GL_LINK_WARNING ("strstr is often incorrectly implemented for multibyte locales - use gnulib module 'strstr' for correct and portable internationalization"), \
246      strstr (a, b))
247 #endif
248
249 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
250    comparison.
251    Note: This function may, in multibyte locales, return success even if
252    strlen (haystack) < strlen (needle) !  */
253 #if @GNULIB_STRCASESTR@
254 # if @REPLACE_STRCASESTR@
255 #  undef strcasestr
256 #  define strcasestr rpl_strcasestr
257 extern char *strcasestr (const char *haystack, const char *needle);
258 # endif
259 #elif defined GNULIB_POSIXCHECK
260 # undef strcasestr
261 # define strcasestr(a,b) \
262     (GL_LINK_WARNING ("strcasestr is often incorrectly implemented for multibyte locales - use gnulib module 'strcasestr' for correct and portable internationalization"), \
263      strcasestr (a, b))
264 #endif
265
266 /* Parse S into tokens separated by characters in DELIM.
267    If S is NULL, the saved pointer in SAVE_PTR is used as
268    the next starting point.  For example:
269         char s[] = "-abc-=-def";
270         char *sp;
271         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
272         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
273         x = strtok_r(NULL, "=", &sp);   // x = NULL
274                 // s = "abc\0-def\0"
275
276    This is a variant of strtok() that is multithread-safe.
277
278    For the POSIX documentation for this function, see:
279    http://www.opengroup.org/susv3xsh/strtok.html
280
281    Caveat: It modifies the original string.
282    Caveat: These functions cannot be used on constant strings.
283    Caveat: The identity of the delimiting character is lost.
284    Caveat: It doesn't work with multibyte strings unless all of the delimiter
285            characters are ASCII characters < 0x30.
286
287    See also strsep().  */
288 #if @GNULIB_STRTOK_R@
289 # if ! @HAVE_DECL_STRTOK_R@
290 extern char *strtok_r (char *restrict __s, char const *restrict __sep,
291                        char **restrict __lasts);
292 # endif
293 #else
294 # undef strtok_r
295 # define strtok_r strtok_r_is_unportable__use_gnulib_module_strtok_r_for_portability
296 #endif
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302 #endif