xref: /aosp_15_r20/bionic/libc/include/string.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _STRING_H
30 #define _STRING_H
31 
32 #include <sys/cdefs.h>
33 #include <stddef.h>
34 #include <xlocale.h>
35 
36 #include <bits/strcasecmp.h>
37 
38 __BEGIN_DECLS
39 
40 #if defined(__USE_BSD) || defined(__USE_GNU)
41 #include <strings.h>
42 #endif
43 
44 void* _Nullable memccpy(void* _Nonnull __dst, const void* _Nonnull __src, int __stop_char, size_t __n);
45 void* _Nullable memchr(const void* _Nonnull __s, int __ch, size_t __n) __attribute_pure__;
46 #if defined(__cplusplus)
47 extern "C++" void* _Nullable memrchr(void* _Nonnull __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
48 extern "C++" const void* _Nullable memrchr(const void* _Nonnull __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
49 #else
50 void* _Nullable memrchr(const void* _Nonnull __s, int __ch, size_t __n) __attribute_pure__;
51 #endif
52 int memcmp(const void* _Nonnull __lhs, const void* _Nonnull __rhs, size_t __n) __attribute_pure__;
53 void* _Nonnull memcpy(void* _Nonnull, const void* _Nonnull, size_t);
54 #if defined(__USE_GNU)
55 
56 #if __BIONIC_AVAILABILITY_GUARD(23)
57 void* _Nonnull mempcpy(void* _Nonnull __dst, const void* _Nonnull __src, size_t __n) __INTRODUCED_IN(23);
58 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
59 
60 #endif
61 void* _Nonnull memmove(void* _Nonnull __dst, const void* _Nonnull __src, size_t __n);
62 
63 /**
64  * [memset(3)](https://man7.org/linux/man-pages/man3/memset.3.html) writes the
65  * bottom 8 bits of the given int to the next `n` bytes of `dst`.
66  *
67  * Returns `dst`.
68  */
69 void* _Nonnull memset(void* _Nonnull __dst, int __ch, size_t __n);
70 
71 /**
72  * [memset_explicit(3)](https://man7.org/linux/man-pages/man3/memset_explicit.3.html)
73  * writes the bottom 8 bits of the given int to the next `n` bytes of `dst`,
74  * but won't be optimized out by the compiler.
75  *
76  * Returns `dst`.
77  */
78 
79 #if __BIONIC_AVAILABILITY_GUARD(34)
80 void* _Nonnull memset_explicit(void* _Nonnull __dst, int __ch, size_t __n) __INTRODUCED_IN(34);
81 #endif /* __BIONIC_AVAILABILITY_GUARD(34) */
82 
83 
84 void* _Nullable memmem(const void* _Nonnull __haystack, size_t __haystack_size, const void* _Nonnull __needle, size_t __needle_size) __attribute_pure__;
85 
86 char* _Nullable strchr(const char* _Nonnull __s, int __ch) __attribute_pure__;
87 char* _Nullable __strchr_chk(const char* _Nonnull __s, int __ch, size_t __n);
88 #if defined(__USE_GNU)
89 #if defined(__cplusplus)
90 
91 #if __BIONIC_AVAILABILITY_GUARD(24)
92 extern "C++" char* _Nonnull strchrnul(char* _Nonnull __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
93 extern "C++" const char* _Nonnull strchrnul(const char* _Nonnull __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
94 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
95 
96 #else
97 
98 #if __BIONIC_AVAILABILITY_GUARD(24)
99 char* _Nonnull strchrnul(const char* _Nonnull __s, int __ch) __attribute_pure__ __INTRODUCED_IN(24);
100 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
101 
102 #endif
103 #endif
104 
105 char* _Nullable strrchr(const char* _Nonnull __s, int __ch) __attribute_pure__;
106 char* _Nullable __strrchr_chk(const char* _Nonnull __s, int __ch, size_t __n);
107 
108 size_t strlen(const char* _Nonnull __s) __attribute_pure__;
109 size_t __strlen_chk(const char* _Nonnull __s, size_t __n);
110 
111 int strcmp(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__;
112 char* _Nonnull stpcpy(char* _Nonnull __dst, const char* _Nonnull __src);
113 char* _Nonnull strcpy(char* _Nonnull __dst, const char* _Nonnull __src);
114 char* _Nonnull strcat(char* _Nonnull __dst, const char* _Nonnull __src);
115 char* _Nullable strdup(const char* _Nonnull __s);
116 
117 char* _Nullable strstr(const char* _Nonnull __haystack, const char* _Nonnull __needle) __attribute_pure__;
118 #if defined(__cplusplus)
119 extern "C++" char* _Nullable strcasestr(char* _Nonnull, const char* _Nonnull) __RENAME(strcasestr) __attribute_pure__;
120 extern "C++" const char* _Nullable strcasestr(const char* _Nonnull, const char* _Nonnull) __RENAME(strcasestr) __attribute_pure__;
121 #else
122 char* _Nullable strcasestr(const char* _Nonnull __haystack, const char* _Nonnull __needle) __attribute_pure__;
123 #endif
124 char* _Nullable strtok(char* _Nullable __s, const char* _Nonnull __delimiter);
125 char* _Nullable strtok_r(char* _Nullable __s, const char* _Nonnull __delimiter, char* _Nonnull * _Nonnull __pos_ptr);
126 
127 /**
128  * [strerror(3)](https://man7.org/linux/man-pages/man3/strerror.3.html)
129  * returns a string describing the given errno value.
130  * `strerror(EINVAL)` would return "Invalid argument", for example.
131  *
132  * On Android, unknown errno values return a string such as "Unknown error 666".
133  * These unknown errno value strings live in thread-local storage, and are valid
134  * until the next call of strerror() on the same thread.
135  *
136  * Returns a pointer to a string.
137  */
138 char* _Nonnull strerror(int __errno_value);
139 
140 /**
141  * Equivalent to strerror() on Android where only C/POSIX locales are available.
142  */
143 char* _Nonnull strerror_l(int __errno_value, locale_t _Nonnull __l) __RENAME(strerror);
144 
145 /**
146  * [strerror_r(3)](https://man7.org/linux/man-pages/man3/strerror_r.3.html)
147  * writes a string describing the given errno value into the given buffer.
148  *
149  * There are two variants of this function, POSIX and GNU.
150  * The GNU variant returns a pointer to the buffer.
151  * The POSIX variant returns 0 on success or an errno value on failure.
152  *
153  * The GNU variant is available since API level 23 if `_GNU_SOURCE` is defined.
154  * The POSIX variant is available otherwise.
155  */
156 #if defined(__USE_GNU) && __ANDROID_API__ >= 23
157 char* _Nonnull strerror_r(int __errno_value, char* _Nullable __buf, size_t __n) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
158 #else /* POSIX */
159 int strerror_r(int __errno_value, char* _Nonnull __buf, size_t __n);
160 #endif
161 
162 /**
163  * [strerrorname_np(3)](https://man7.org/linux/man-pages/man3/strerrordesc_np.3.html)
164  * returns the name of the errno constant corresponding to its argument.
165  * `strerrorname_np(38)` would return "ENOSYS", because `ENOSYS` is errno 38. This
166  * is mostly useful for error reporting in cases where a string like "ENOSYS" is
167  * more readable than a string like "Function not implemented", which would be
168  * returned by strerror().
169  *
170  * Returns a pointer to a string, or null for unknown errno values.
171  *
172  * Available since API level 35.
173  */
174 #if defined(__USE_GNU)
175 
176 #if __BIONIC_AVAILABILITY_GUARD(35)
177 const char* _Nullable strerrorname_np(int __errno_value) __INTRODUCED_IN(35);
178 #endif /* __BIONIC_AVAILABILITY_GUARD(35) */
179 
180 #endif
181 
182 /**
183  * [strerrordesc_np(3)](https://man7.org/linux/man-pages/man3/strerrordesc_np.3.html)
184  * is like strerror() but without localization. Since Android's strerror()
185  * does not localize, this is the same as strerror() on Android.
186  *
187  * Returns a pointer to a string.
188  */
189 #if defined(__USE_GNU)
190 const char* _Nonnull strerrordesc_np(int __errno_value) __RENAME(strerror);
191 #endif
192 
193 size_t strnlen(const char* _Nonnull __s, size_t __n) __attribute_pure__;
194 char* _Nonnull strncat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
195 char* _Nullable strndup(const char* _Nonnull __s, size_t __n);
196 int strncmp(const char* _Nonnull __lhs, const char* _Nonnull __rhs, size_t __n) __attribute_pure__;
197 char* _Nonnull stpncpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
198 char* _Nonnull strncpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
199 
200 size_t strlcat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
201 size_t strlcpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n);
202 
203 size_t strcspn(const char* _Nonnull __s, const char* _Nonnull __reject) __attribute_pure__;
204 char* _Nullable strpbrk(const char* _Nonnull __s, const char* _Nonnull __accept) __attribute_pure__;
205 char* _Nullable strsep(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __delimiter);
206 size_t strspn(const char* _Nonnull __s, const char* _Nonnull __accept);
207 
208 char* _Nonnull strsignal(int __signal);
209 
210 int strcoll(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__;
211 size_t strxfrm(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n);
212 
213 int strcoll_l(const char* _Nonnull __lhs, const char* _Nonnull __rhs, locale_t _Nonnull __l) __attribute_pure__;
214 size_t strxfrm_l(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n, locale_t _Nonnull __l);
215 
216 #if defined(__USE_GNU) && !defined(basename)
217 /*
218  * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
219  * It doesn't modify its argument, and in C++ it's const-correct.
220  */
221 #if defined(__cplusplus)
222 
223 #if __BIONIC_AVAILABILITY_GUARD(23)
224 extern "C++" char* _Nonnull basename(char* _Nullable __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
225 extern "C++" const char* _Nonnull basename(const char* _Nonnull __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
226 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
227 
228 #else
229 
230 #if __BIONIC_AVAILABILITY_GUARD(23)
231 char* _Nonnull basename(const char* _Nonnull __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
232 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
233 
234 #endif
235 #endif
236 
237 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
238 #include <bits/fortify/string.h>
239 #endif
240 
241 /* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
242 #if defined(__cplusplus)
243 /* libcxx tries to provide these. Suppress that, since libcxx's impl doesn't respect FORTIFY. */
244 #define __CORRECT_ISO_CPP_STRING_H_PROTO
245 /* Used to make these preferable over regular <string.h> signatures for overload resolution. */
246 #define __prefer_this_overload __enable_if(true, "")
247 extern "C++" {
248 inline __always_inline
__bionic_memchr(const void * _Nonnull const s __pass_object_size,int c,size_t n)249 void* _Nullable __bionic_memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n) {
250     return memchr(s, c, n);
251 }
252 
253 inline __always_inline
memchr(const void * _Nonnull const s __pass_object_size,int c,size_t n)254 const void* _Nullable memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n)
255         __prefer_this_overload {
256     return __bionic_memchr(s, c, n);
257 }
258 
259 inline __always_inline
memchr(void * _Nonnull const s __pass_object_size,int c,size_t n)260 void* _Nullable memchr(void* _Nonnull const s __pass_object_size, int c, size_t n) __prefer_this_overload {
261     return __bionic_memchr(s, c, n);
262 }
263 
264 inline __always_inline
__bionic_strchr(const char * _Nonnull const s __pass_object_size,int c)265 char* _Nullable __bionic_strchr(const char* _Nonnull const s __pass_object_size, int c) {
266     return strchr(s, c);
267 }
268 
269 inline __always_inline
strchr(const char * _Nonnull const s __pass_object_size,int c)270 const char* _Nullable strchr(const char* _Nonnull const s __pass_object_size, int c)
271         __prefer_this_overload {
272     return __bionic_strchr(s, c);
273 }
274 
275 inline __always_inline
strchr(char * _Nonnull const s __pass_object_size,int c)276 char* _Nullable strchr(char* _Nonnull const s __pass_object_size, int c)
277         __prefer_this_overload {
278     return __bionic_strchr(s, c);
279 }
280 
281 inline __always_inline
__bionic_strrchr(const char * _Nonnull const s __pass_object_size,int c)282 char* _Nullable __bionic_strrchr(const char* _Nonnull const s __pass_object_size, int c) {
283     return strrchr(s, c);
284 }
285 
286 inline __always_inline
strrchr(const char * _Nonnull const s __pass_object_size,int c)287 const char* _Nullable strrchr(const char* _Nonnull const s __pass_object_size, int c) __prefer_this_overload {
288     return __bionic_strrchr(s, c);
289 }
290 
291 inline __always_inline
strrchr(char * _Nonnull const s __pass_object_size,int c)292 char* _Nullable strrchr(char* _Nonnull const s __pass_object_size, int c) __prefer_this_overload {
293     return __bionic_strrchr(s, c);
294 }
295 
296 /* Functions with no FORTIFY counterpart. */
297 inline __always_inline
__bionic_strstr(const char * _Nonnull h,const char * _Nonnull n)298 char* _Nullable __bionic_strstr(const char* _Nonnull h, const char* _Nonnull n) { return strstr(h, n); }
299 
300 inline __always_inline
strstr(const char * _Nonnull h,const char * _Nonnull n)301 const char* _Nullable strstr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
302     return __bionic_strstr(h, n);
303 }
304 
305 inline __always_inline
strstr(char * _Nonnull h,const char * _Nonnull n)306 char* _Nullable strstr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
307     return __bionic_strstr(h, n);
308 }
309 
310 inline __always_inline
__bionic_strpbrk(const char * _Nonnull h,const char * _Nonnull n)311 char* _Nullable __bionic_strpbrk(const char* _Nonnull h, const char* _Nonnull n) { return strpbrk(h, n); }
312 
313 inline __always_inline
strpbrk(char * _Nonnull h,const char * _Nonnull n)314 char* _Nullable strpbrk(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
315     return __bionic_strpbrk(h, n);
316 }
317 
318 inline __always_inline
strpbrk(const char * _Nonnull h,const char * _Nonnull n)319 const char* _Nullable strpbrk(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload {
320     return __bionic_strpbrk(h, n);
321 }
322 }
323 #undef __prefer_this_overload
324 #endif
325 
326 __END_DECLS
327 
328 #endif
329