blob: 631b3ddd6cff7c0c318779bf78f3c47a0015ef56 [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 Hughes3f66e742017-08-01 13:24:40 -070044void* memccpy(void*, const void*, int, size_t);
45void* memchr(const void*, int, size_t) __attribute_pure__ __overloadable __RENAME_CLANG(memchr);
46void* memrchr(const void*, int, size_t) __attribute_pure__ __overloadable __RENAME_CLANG(memrchr);
47int memcmp(const void*, const void*, size_t) __attribute_pure__;
48void* memcpy(void*, const void*, size_t)
George Burgess IV7cc779f2017-02-09 00:00:31 -080049 __overloadable __RENAME_CLANG(memcpy);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080050#if defined(__USE_GNU)
Elliott Hughes3f66e742017-08-01 13:24:40 -070051void* mempcpy(void*, const void*, size_t) __INTRODUCED_IN(23);
Elliott Hughes3cfb52a2015-02-18 21:29:13 -080052#endif
Elliott Hughes3f66e742017-08-01 13:24:40 -070053void* memmove(void*, const void*, size_t) __overloadable __RENAME_CLANG(memmove);
54void* memset(void*, int, size_t) __overloadable __RENAME_CLANG(memset);
55void* memmem(const void*, size_t, const void*, size_t) __attribute_pure__;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Elliott Hughes3f66e742017-08-01 13:24:40 -070057char* strchr(const char*, int) __attribute_pure__ __overloadable __RENAME_CLANG(strchr);
58char* __strchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070059#if defined(__USE_GNU)
60#if defined(__cplusplus)
Elliott Hughes3f66e742017-08-01 13:24:40 -070061extern "C++" char* strchrnul(char*, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
62extern "C++" const char* strchrnul(const char*, int) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070063#else
Elliott Hughes3f66e742017-08-01 13:24:40 -070064char* strchrnul(const char*, int) __attribute_pure__ __INTRODUCED_IN(24);
Elliott Hughes7ac3c122015-08-26 09:59:29 -070065#endif
66#endif
Pavel Chupin3c4b50f2013-07-26 16:50:11 +040067
Elliott Hughes3f66e742017-08-01 13:24:40 -070068char* strrchr(const char*, int) __attribute_pure__ __overloadable __RENAME_CLANG(strrchr);
69char* __strrchr_chk(const char*, int, size_t) __INTRODUCED_IN(18);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070
Elliott Hughes3f66e742017-08-01 13:24:40 -070071size_t strlen(const char*) __attribute_pure__ __overloadable
George Burgess IV7cc779f2017-02-09 00:00:31 -080072 __RENAME_CLANG(strlen);
Elliott Hughes3f66e742017-08-01 13:24:40 -070073size_t __strlen_chk(const char*, size_t) __INTRODUCED_IN(17);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074
Elliott Hughes3f66e742017-08-01 13:24:40 -070075int strcmp(const char*, const char*) __attribute_pure__;
76char* stpcpy(char*, const char*) __overloadable __RENAME_CLANG(stpcpy) __INTRODUCED_IN(21);
77char* strcpy(char*, const char*)
George Burgess IV7cc779f2017-02-09 00:00:31 -080078 __overloadable __RENAME_CLANG(strcpy);
Elliott Hughes3f66e742017-08-01 13:24:40 -070079char* strcat(char*, const char*) __overloadable __RENAME_CLANG(strcat);
80char* strdup(const char*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Elliott Hughes3f66e742017-08-01 13:24:40 -070082char* strstr(const char*, const char*) __attribute_pure__;
83char* strcasestr(const char*, const char*) __attribute_pure__;
84char* strtok(char*, const char*);
85char* strtok_r(char*, const char*, char**);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
Elliott Hughes5470c182016-07-22 11:36:17 -070087char* strerror(int);
88char* strerror_l(int, locale_t) __INTRODUCED_IN(23);
Dan Albert8b154b12017-02-14 16:33:06 -080089#if defined(__USE_GNU) && __ANDROID_API__ >= 23
Elliott Hughes5470c182016-07-22 11:36:17 -070090char* strerror_r(int, char*, size_t) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070091#else /* POSIX */
Elliott Hughes5470c182016-07-22 11:36:17 -070092int strerror_r(int, char*, size_t);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070093#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094
Elliott Hughes3f66e742017-08-01 13:24:40 -070095size_t strnlen(const char*, size_t) __attribute_pure__;
96char* strncat(char*, const char*, size_t) __overloadable __RENAME_CLANG(strncat);
97char* strndup(const char*, size_t);
98int strncmp(const char*, const char*, size_t) __attribute_pure__;
99char* stpncpy(char*, const char*, size_t) __overloadable __RENAME_CLANG(stpncpy) __INTRODUCED_IN(21);
100char* strncpy(char*, const char*, size_t) __overloadable __RENAME_CLANG(strncpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Elliott Hughes3f66e742017-08-01 13:24:40 -0700102size_t strlcat(char*, const char*, size_t) __overloadable __RENAME_CLANG(strlcat);
103size_t strlcpy(char*, const char*, size_t) __overloadable __RENAME_CLANG(strlcpy);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
Elliott Hughes3f66e742017-08-01 13:24:40 -0700105size_t strcspn(const char*, const char*) __attribute_pure__;
106char* strpbrk(const char*, const char*) __attribute_pure__;
107char* strsep(char**, const char*);
108size_t strspn(const char*, const char*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109
Elliott Hughes5470c182016-07-22 11:36:17 -0700110char* strsignal(int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Elliott Hughes3f66e742017-08-01 13:24:40 -0700112int strcoll(const char*, const char*) __attribute_pure__;
113size_t strxfrm(char*, const char*, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800115#if __ANDROID_API__ >= __ANDROID_API_L__
Elliott Hughes3f66e742017-08-01 13:24:40 -0700116int strcoll_l(const char*, const char*, locale_t) __attribute_pure__ __INTRODUCED_IN(21);
117size_t strxfrm_l(char*, const char*, size_t, locale_t) __INTRODUCED_IN(21);
Josh Gao6cd9fb02016-09-23 14:06:05 -0700118#else
119// Implemented as static inlines before 21.
120#endif
Dan Albertdfb5ce42014-07-09 22:51:34 +0000121
Josh Gaoeb9b9252015-11-03 18:46:02 -0800122#if defined(__USE_GNU) && !defined(basename)
Elliott Hughes09c39d62014-08-19 14:30:30 -0700123/*
124 * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
125 * It doesn't modify its argument, and in C++ it's const-correct.
126 */
127#if defined(__cplusplus)
Elliott Hughes3f66e742017-08-01 13:24:40 -0700128extern "C++" char* basename(char*) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
129extern "C++" const char* basename(const char*) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700130#else
Elliott Hughes3f66e742017-08-01 13:24:40 -0700131char* basename(const char*) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
Elliott Hughes09c39d62014-08-19 14:30:30 -0700132#endif
Elliott Hughes09c39d62014-08-19 14:30:30 -0700133#endif
134
George Burgess IVb97049c2017-07-24 15:05:05 -0700135#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
136#include <bits/fortify/string.h>
137#endif
Nick Kralevich0a230152012-06-04 15:20:25 -0700138
George Burgess IVbd3d2082017-04-04 17:34:02 -0700139/* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
140#if defined(__cplusplus) && defined(__clang__)
141/*
142 * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
143 * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
144 */
145#define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
146extern "C++" {
147inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700148void* __bionic_memchr(const void* const s __pass_object_size, int c, size_t n) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700149 return memchr(s, c, n);
150}
151
152inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700153const void* memchr(const void* const s __pass_object_size, int c, size_t n)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700154 __prefer_this_overload {
155 return __bionic_memchr(s, c, n);
156}
157
158inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700159void* memchr(void* const s __pass_object_size, int c, size_t n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700160 return __bionic_memchr(s, c, n);
161}
162
163inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700164char* __bionic_strchr(const char* const s __pass_object_size, int c) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700165 return strchr(s, c);
166}
167
168inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700169const char* strchr(const char* const s __pass_object_size, int c)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700170 __prefer_this_overload {
171 return __bionic_strchr(s, c);
172}
173
174inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700175char* strchr(char* const s __pass_object_size, int c)
George Burgess IVbd3d2082017-04-04 17:34:02 -0700176 __prefer_this_overload {
177 return __bionic_strchr(s, c);
178}
179
180inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700181char* __bionic_strrchr(const char* const s __pass_object_size, int c) {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700182 return strrchr(s, c);
183}
184
185inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700186const char* strrchr(const char* const s __pass_object_size, int c) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700187 return __bionic_strrchr(s, c);
188}
189
190inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700191char* strrchr(char* const s __pass_object_size, int c) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700192 return __bionic_strrchr(s, c);
193}
194
195/* Functions with no FORTIFY counterpart. */
196inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700197char* __bionic_strstr(const char* h, const char* n) { return strstr(h, n); }
George Burgess IVbd3d2082017-04-04 17:34:02 -0700198
199inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700200const char* strstr(const char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700201 return __bionic_strstr(h, n);
202}
203
204inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700205char* strstr(char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700206 return __bionic_strstr(h, n);
207}
208
209inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700210char* __bionic_strpbrk(const char* h, const char* n) { return strpbrk(h, n); }
George Burgess IVbd3d2082017-04-04 17:34:02 -0700211
212inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700213char* strpbrk(char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700214 return __bionic_strpbrk(h, n);
215}
216
217inline __always_inline
Elliott Hughes3f66e742017-08-01 13:24:40 -0700218const char* strpbrk(const char* h, const char* n) __prefer_this_overload {
George Burgess IVbd3d2082017-04-04 17:34:02 -0700219 return __bionic_strpbrk(h, n);
220}
221}
222#undef __prefer_this_overload
223#endif
224
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225__END_DECLS
226
Elliott Hughes09c39d62014-08-19 14:30:30 -0700227#endif /* _STRING_H */