blob: d409ba84837610baad6cfc7fa0f6c25f2fa1c166 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Elliott Hughes09c39d62014-08-19 14:30:30 -070028
29#ifndef _STRING_H
30#define _STRING_H
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031
32#include <sys/cdefs.h>
33#include <stddef.h>
Dan Albertdfb5ce42014-07-09 22:51:34 +000034#include <xlocale.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035
Josh Gaoc3cec272016-04-07 13:39:49 -070036#include <bits/strcasecmp.h>
37
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038__BEGIN_DECLS
39
Elliott Hughes76f89162015-01-26 13:34:58 -080040#if defined(__USE_BSD)
41#include <strings.h>
42#endif
43
Elliott Hughes684c31a2017-08-18 15:07:41 -070044void* memccpy(void* __dst, const void* __src, int __stop_char, size_t __n);
45void* memchr(const void* __s, int __ch, size_t __n) __attribute_pure__ __overloadable __RENAME_CLANG(memchr);
Elliott Hughesdf9a4892017-08-23 14:34:03 -070046#if defined(__cplusplus)
47extern "C++" void* memrchr(void* __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
48extern "C++" const void* memrchr(const void* __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
49#else
Elliott Hughes684c31a2017-08-18 15:07:41 -070050void* memrchr(const void* __s, int __ch, size_t __n) __attribute_pure__ __overloadable __RENAME_CLANG(memrchr);
Elliott Hughesdf9a4892017-08-23 14:34:03 -070051#endif
Elliott Hughes684c31a2017-08-18 15:07:41 -070052int memcmp(const void* __lhs, const void* __rhs, size_t __n) __attribute_pure__;
Elliott Hughes3f66e742017-08-01 13:24:40 -070053void* memcpy(void*, const void*, size_t)
George Burgess IV7cc779f2017-02-09 00:00:31 -080054 __overloadable __RENAME_CLANG(memcpy);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080055#if defined(__USE_GNU)
Elliott Hughes684c31a2017-08-18 15:07:41 -070056void* mempcpy(void* __dst, const void* __src, size_t __n) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080057#endif
Elliott Hughes684c31a2017-08-18 15:07:41 -070058void* memmove(void* __dst, const void* __src, size_t __n) __overloadable __RENAME_CLANG(memmove);
59void* memset(void* __dst, int __ch, size_t __n) __overloadable __RENAME_CLANG(memset);
60void* memmem(const void* __haystack, size_t __haystack_size, const void* __needle, size_t __needle_size) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
Elliott Hughes684c31a2017-08-18 15:07:41 -070062char* strchr(const char* __s, int __ch) __attribute_pure__ __overloadable __RENAME_CLANG(strchr);
63char* __strchr_chk(const char* __s, int __ch, size_t __n) __INTRODUCED_IN(18);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070064#if defined(__USE_GNU)
65#if defined(__cplusplus)
Dan Albert3f7e65e2017-08-15 14:42:31 -070066/* The versioner doesn't handle C++ blocks yet, so manually guarded. */
67#if __ANDROID_API__ >= 24
Elliott Hughes684c31a2017-08-18 15:07:41 -070068extern "C++" char* strchrnul(char* __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
69extern "C++" const char* strchrnul(const char* __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
Dan Albert3f7e65e2017-08-15 14:42:31 -070070#endif /* __ANDROID_API__ >= 24 */
Elliott Hughes7ac3c122015-08-26 09:59:29 -070071#else
Elliott Hughes684c31a2017-08-18 15:07:41 -070072char* strchrnul(const char* __s, int __ch) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070073#endif
74#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040075
Elliott Hughes684c31a2017-08-18 15:07:41 -070076char* strrchr(const char* __s, int __ch) __attribute_pure__ __overloadable __RENAME_CLANG(strrchr);
77char* __strrchr_chk(const char* __s, int __ch, size_t __n) __INTRODUCED_IN(18);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
Elliott Hughes684c31a2017-08-18 15:07:41 -070079size_t strlen(const char* __s) __attribute_pure__ __overloadable __RENAME_CLANG(strlen);
80size_t __strlen_chk(const char* __s, size_t __n) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Elliott Hughes684c31a2017-08-18 15:07:41 -070082int strcmp(const char* __lhs, const char* __rhs) __attribute_pure__;
83char* stpcpy(char* __dst, const char* __src) __overloadable __RENAME_CLANG(stpcpy) __INTRODUCED_IN(21);
84char* strcpy(char* __dst, const char* __src) __overloadable __RENAME_CLANG(strcpy);
85char* strcat(char* __dst, const char* __src) __overloadable __RENAME_CLANG(strcat);
86char* strdup(const char* __s);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
Elliott Hughes684c31a2017-08-18 15:07:41 -070088char* strstr(const char* __haystack, const char* __needle) __attribute_pure__;
Elliott Hughesdf9a4892017-08-23 14:34:03 -070089#if defined(__cplusplus)
90extern "C++" char* strcasestr(char*, const char*) __RENAME(strcasestr) __attribute_pure__;
91extern "C++" const char* strcasestr(const char*, const char*) __RENAME(strcasestr) __attribute_pure__;
92#else
Elliott Hughes684c31a2017-08-18 15:07:41 -070093char* strcasestr(const char* __haystack, const char* __needle) __attribute_pure__;
Elliott Hughesdf9a4892017-08-23 14:34:03 -070094#endif
Elliott Hughes684c31a2017-08-18 15:07:41 -070095char* strtok(char* __s, const char* __delimiter);
96char* strtok_r(char* __s, const char* __delimiter, char** __pos_ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097
Elliott Hughes684c31a2017-08-18 15:07:41 -070098char* strerror(int __errno_value);
99char* strerror_l(int __errno_value, locale_t __l) __INTRODUCED_IN(23);
Dan Albert8b154b12017-02-14 16:33:06 -0800100#if defined(__USE_GNU) && __ANDROID_API__ >= 23
Elliott Hughes684c31a2017-08-18 15:07:41 -0700101char* strerror_r(int __errno_value, char* __buf, size_t __n) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700102#else /* POSIX */
Elliott Hughes684c31a2017-08-18 15:07:41 -0700103int strerror_r(int __errno_value, char* __buf, size_t __n);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700104#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105
Elliott Hughes684c31a2017-08-18 15:07:41 -0700106size_t strnlen(const char* __s, size_t __n) __attribute_pure__;
107char* strncat(char* __dst, const char* __src, size_t __n) __overloadable __RENAME_CLANG(strncat);
108char* strndup(const char* __s, size_t __n);
109int strncmp(const char* __lhs, const char* __rhs, size_t __n) __attribute_pure__;
110char* stpncpy(char* __dst, const char* __src, size_t __n) __overloadable __RENAME_CLANG(stpncpy) __INTRODUCED_IN(21);
111char* strncpy(char* __dst, const char* __src, size_t __n) __overloadable __RENAME_CLANG(strncpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
Elliott Hughes684c31a2017-08-18 15:07:41 -0700113size_t strlcat(char* __dst, const char* __src, size_t __n) __overloadable __RENAME_CLANG(strlcat);
114size_t strlcpy(char* __dst, const char* __src, size_t __n) __overloadable __RENAME_CLANG(strlcpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115
Elliott Hughes684c31a2017-08-18 15:07:41 -0700116size_t strcspn(const char* __s, const char* __reject) __attribute_pure__;
117char* strpbrk(const char* __s, const char* __accept) __attribute_pure__;
118char* strsep(char** __s_ptr, const char* __delimiter);
119size_t strspn(const char* __s, const char* __accept);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120
Elliott Hughes684c31a2017-08-18 15:07:41 -0700121char* strsignal(int __signal);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Elliott Hughes684c31a2017-08-18 15:07:41 -0700123int strcoll(const char* __lhs, const char* __rhs) __attribute_pure__;
124size_t strxfrm(char* __dst, const char* __src, size_t __n);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800126#if __ANDROID_API__ >= __ANDROID_API_L__
Elliott Hughes684c31a2017-08-18 15:07:41 -0700127int strcoll_l(const char* __lhs, const char* __rhs, locale_t __l) __attribute_pure__ __INTRODUCED_IN(21);
128size_t strxfrm_l(char* __dst, const char* __src, size_t __n, locale_t __l) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700129#else
130// Implemented as static inlines before 21.
131#endif
Dan Albertdfb5ce42014-07-09 22:51:34 +0000132
Josh Gaoeb9b9252015-11-03 18:46:02 -0800133#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700134/*
135 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
136 * It doesn't modify its argument, and in C++ it's const-correct.
137 */
138#if defined(__cplusplus)
Dan Albert3f7e65e2017-08-15 14:42:31 -0700139/* The versioner doesn't handle C++ blocks yet, so manually guarded. */
140#if __ANDROID_API__ >= 23
Elliott Hughes684c31a2017-08-18 15:07:41 -0700141extern "C++" char* basename(char* __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
142extern "C++" const char* basename(const char* __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Dan Albert3f7e65e2017-08-15 14:42:31 -0700143#endif /* __ANDROID_API__ >= 23 */
Elliott Hughes09c39d62014-08-19 14:30:30 -0700144#else
Elliott Hughes684c31a2017-08-18 15:07:41 -0700145char* basename(const char* __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700146#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700147#endif
148
George Burgess IVb97049c2017-07-24 15:05:05 -0700149#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
150#include <bits/fortify/string.h>
151#endif
Nick Kralevich0a230152012-06-04 15:20:25 -0700152
George Burgess IVbd3d2082017-04-04 17:34:02 -0700153/* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
154#if defined(__cplusplus) && defined(__clang__)
155/*
156 * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
157 * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
158 */
159#define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
160extern "C++" {
161inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700162void* __bionic_memchr(const void* const s __pass_object_size, int c, size_t n) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700163 return memchr(s, c, n);
164}
165
166inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700167const void* memchr(const void* const s __pass_object_size, int c, size_t n)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700168 __prefer_this_overload {
169 return __bionic_memchr(s, c, n);
170}
171
172inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700173void* memchr(void* const s __pass_object_size, int c, size_t n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700174 return __bionic_memchr(s, c, n);
175}
176
177inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700178char* __bionic_strchr(const char* const s __pass_object_size, int c) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700179 return strchr(s, c);
180}
181
182inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700183const char* strchr(const char* const s __pass_object_size, int c)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700184 __prefer_this_overload {
185 return __bionic_strchr(s, c);
186}
187
188inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700189char* strchr(char* const s __pass_object_size, int c)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700190 __prefer_this_overload {
191 return __bionic_strchr(s, c);
192}
193
194inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700195char* __bionic_strrchr(const char* const s __pass_object_size, int c) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700196 return strrchr(s, c);
197}
198
199inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700200const char* strrchr(const char* const s __pass_object_size, int c) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700201 return __bionic_strrchr(s, c);
202}
203
204inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700205char* strrchr(char* const s __pass_object_size, int c) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700206 return __bionic_strrchr(s, c);
207}
208
209/* Functions with no FORTIFY counterpart. */
210inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700211char* __bionic_strstr(const char* h, const char* n) { return strstr(h, n); }
George Burgess IVbd3d2082017-04-04 17:34:02 -0700212
213inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700214const char* strstr(const char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700215 return __bionic_strstr(h, n);
216}
217
218inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700219char* strstr(char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700220 return __bionic_strstr(h, n);
221}
222
223inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700224char* __bionic_strpbrk(const char* h, const char* n) { return strpbrk(h, n); }
George Burgess IVbd3d2082017-04-04 17:34:02 -0700225
226inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700227char* strpbrk(char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700228 return __bionic_strpbrk(h, n);
229}
230
231inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700232const char* strpbrk(const char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700233 return __bionic_strpbrk(h, n);
234}
235}
236#undef __prefer_this_overload
237#endif
238
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239__END_DECLS
240
Elliott Hughes684c31a2017-08-18 15:07:41 -0700241#endif