| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | */ |
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 28 | |
| 29 | #ifndef _STRING_H |
| 30 | #define _STRING_H |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <sys/cdefs.h> |
| 33 | #include <stddef.h> |
| Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 34 | #include <xlocale.h> |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
| Josh Gao | c3cec27 | 2016-04-07 13:39:49 -0700 | [diff] [blame] | 36 | #include <bits/strcasecmp.h> |
| 37 | |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | __BEGIN_DECLS |
| 39 | |
| Elliott Hughes | c5d9795 | 2023-11-14 00:40:30 +0000 | [diff] [blame] | 40 | #if defined(__USE_BSD) || defined(__USE_GNU) |
| Elliott Hughes | 76f8916 | 2015-01-26 13:34:58 -0800 | [diff] [blame] | 41 | #include <strings.h> |
| 42 | #endif |
| 43 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 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__; |
| Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 46 | #if defined(__cplusplus) |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 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__; |
| Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 49 | #else |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 50 | void* _Nullable memrchr(const void* _Nonnull __s, int __ch, size_t __n) __attribute_pure__; |
| Elliott Hughes | df9a489 | 2017-08-23 14:34:03 -0700 | [diff] [blame] | 51 | #endif |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 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); |
| Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 54 | |
| Elliott Hughes | 7d1b9cb | 2025-04-10 15:37:10 -0400 | [diff] [blame] | 55 | #if defined(__USE_GNU) && __BIONIC_AVAILABILITY_GUARD(23) |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 56 | void* _Nonnull mempcpy(void* _Nonnull __dst, const void* _Nonnull __src, size_t __n) __INTRODUCED_IN(23); |
| Dan Albert | 7b7abe9 | 2024-10-22 20:33:44 +0000 | [diff] [blame] | 57 | #endif |
| Elliott Hughes | 216f3ad | 2025-03-31 13:36:43 -0700 | [diff] [blame] | 58 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 59 | void* _Nonnull memmove(void* _Nonnull __dst, const void* _Nonnull __src, size_t __n); |
| Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 62 | * [memset(3)](https://man7.org/linux/man-pages/man3/memset.3.html) writes the |
| Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 63 | * bottom 8 bits of the given int to the next `n` bytes of `dst`. |
| 64 | * |
| 65 | * Returns `dst`. |
| 66 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 67 | void* _Nonnull memset(void* _Nonnull __dst, int __ch, size_t __n); |
| Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 70 | * [memset_explicit(3)](https://man7.org/linux/man-pages/man3/memset_explicit.3.html) |
| Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 71 | * writes the bottom 8 bits of the given int to the next `n` bytes of `dst`, |
| 72 | * but won't be optimized out by the compiler. |
| 73 | * |
| 74 | * Returns `dst`. |
| Elliott Hughes | 249cad0 | 2025-06-04 05:14:10 -0700 | [diff] [blame] | 75 | * |
| 76 | * Available from API level 34, or with __ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__. |
| Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 77 | */ |
| Elliott Hughes | 80ac670 | 2025-06-05 11:47:14 -0700 | [diff] [blame] | 78 | #if __ANDROID_API__ >= 34 |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 79 | void* _Nonnull memset_explicit(void* _Nonnull __dst, int __ch, size_t __n) __INTRODUCED_IN(34); |
| Elliott Hughes | 249cad0 | 2025-06-04 05:14:10 -0700 | [diff] [blame] | 80 | #elif defined(__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__) |
| Elliott Hughes | d4888f3 | 2025-06-04 10:26:01 -0700 | [diff] [blame] | 81 | #define __BIONIC_MEMSET_EXPLICIT_INLINE static __inline |
| 82 | #include <bits/memset_explicit_impl.h> |
| 83 | #undef __BIONIC_MEMSET_EXPLICIT_INLINE |
| Elliott Hughes | 249cad0 | 2025-06-04 05:14:10 -0700 | [diff] [blame] | 84 | #endif |
| Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 85 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 86 | void* _Nullable memmem(const void* _Nonnull __haystack, size_t __haystack_size, const void* _Nonnull __needle, size_t __needle_size) __attribute_pure__; |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 87 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 88 | char* _Nullable strchr(const char* _Nonnull __s, int __ch) __attribute_pure__; |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 89 | char* _Nullable __strchr_chk(const char* _Nonnull __s, int __ch, size_t __n); |
| Elliott Hughes | 1749d21 | 2025-04-24 11:59:35 -0700 | [diff] [blame] | 90 | |
| 91 | #if defined(__USE_GNU) && __BIONIC_AVAILABILITY_GUARD(24) |
| Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 92 | #if defined(__cplusplus) |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 93 | extern "C++" char* _Nonnull strchrnul(char* _Nonnull __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24); |
| 94 | extern "C++" const char* _Nonnull strchrnul(const char* _Nonnull __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24); |
| Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 95 | #else |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 96 | char* _Nonnull strchrnul(const char* _Nonnull __s, int __ch) __attribute_pure__ __INTRODUCED_IN(24); |
| Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 97 | #endif |
| 98 | #endif |
| Pavel Chupin | 3c4b50f | 2013-07-26 16:50:11 +0400 | [diff] [blame] | 99 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 100 | char* _Nullable strrchr(const char* _Nonnull __s, int __ch) __attribute_pure__; |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 101 | char* _Nullable __strrchr_chk(const char* _Nonnull __s, int __ch, size_t __n); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 102 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 103 | size_t strlen(const char* _Nonnull __s) __attribute_pure__; |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 104 | size_t __strlen_chk(const char* _Nonnull __s, size_t __n); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 106 | int strcmp(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__; |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 107 | char* _Nonnull stpcpy(char* _Nonnull __dst, const char* _Nonnull __src); |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 108 | char* _Nonnull strcpy(char* _Nonnull __dst, const char* _Nonnull __src); |
| 109 | char* _Nonnull strcat(char* _Nonnull __dst, const char* _Nonnull __src); |
| 110 | char* _Nullable strdup(const char* _Nonnull __s); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 112 | char* _Nullable strstr(const char* _Nonnull __haystack, const char* _Nonnull __needle) __attribute_pure__; |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 113 | char* _Nullable strcasestr(const char* _Nonnull __haystack, const char* _Nonnull __needle) __attribute_pure__; |
| Elliott Hughes | 55eba99 | 2025-07-29 10:36:13 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * [strtok(3)](https://man7.org/linux/man-pages/man3/strtok.3.html) |
| 117 | * extracts non-empty tokens from strings. |
| 118 | * |
| 119 | * Code on Android should use strtok_r() instead, |
| 120 | * since strtok() isn't thread-safe. |
| 121 | * |
| 122 | * See strsep() if you want empty tokens returned too. |
| 123 | */ |
| Elliott Hughes | 266f2b2 | 2025-07-30 05:36:04 -0700 | [diff] [blame] | 124 | char* _Nullable strtok(char* _Nullable __s, const char* _Nonnull __delimiter) |
| 125 | __attribute__((__deprecated__("strtok() is not thread-safe; use strtok_r() instead"))); |
| Elliott Hughes | 55eba99 | 2025-07-29 10:36:13 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * [strtok_r(3)](https://man7.org/linux/man-pages/man3/strtok_r.3.html) |
| 129 | * extracts non-empty tokens from strings. |
| 130 | * |
| 131 | * See strsep() if you want empty tokens returned too. |
| 132 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 133 | char* _Nullable strtok_r(char* _Nullable __s, const char* _Nonnull __delimiter, char* _Nonnull * _Nonnull __pos_ptr); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 134 | |
| Elliott Hughes | ee04e3d | 2024-08-15 17:14:18 +0000 | [diff] [blame] | 135 | /** |
| 136 | * [strerror(3)](https://man7.org/linux/man-pages/man3/strerror.3.html) |
| 137 | * returns a string describing the given errno value. |
| 138 | * `strerror(EINVAL)` would return "Invalid argument", for example. |
| 139 | * |
| 140 | * On Android, unknown errno values return a string such as "Unknown error 666". |
| 141 | * These unknown errno value strings live in thread-local storage, and are valid |
| 142 | * until the next call of strerror() on the same thread. |
| 143 | * |
| 144 | * Returns a pointer to a string. |
| 145 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 146 | char* _Nonnull strerror(int __errno_value); |
| Elliott Hughes | ee04e3d | 2024-08-15 17:14:18 +0000 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Equivalent to strerror() on Android where only C/POSIX locales are available. |
| 150 | */ |
| 151 | char* _Nonnull strerror_l(int __errno_value, locale_t _Nonnull __l) __RENAME(strerror); |
| 152 | |
| 153 | /** |
| 154 | * [strerror_r(3)](https://man7.org/linux/man-pages/man3/strerror_r.3.html) |
| 155 | * writes a string describing the given errno value into the given buffer. |
| 156 | * |
| 157 | * There are two variants of this function, POSIX and GNU. |
| 158 | * The GNU variant returns a pointer to the buffer. |
| 159 | * The POSIX variant returns 0 on success or an errno value on failure. |
| 160 | * |
| 161 | * The GNU variant is available since API level 23 if `_GNU_SOURCE` is defined. |
| 162 | * The POSIX variant is available otherwise. |
| 163 | */ |
| Dan Albert | 8b154b1 | 2017-02-14 16:33:06 -0800 | [diff] [blame] | 164 | #if defined(__USE_GNU) && __ANDROID_API__ >= 23 |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 165 | char* _Nonnull strerror_r(int __errno_value, char* _Nullable __buf, size_t __n) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23); |
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 166 | #else /* POSIX */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 167 | int strerror_r(int __errno_value, char* _Nonnull __buf, size_t __n); |
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 168 | #endif |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 169 | |
| Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 170 | /** |
| Elliott Hughes | 7fb8d58 | 2025-01-06 15:57:38 +0000 | [diff] [blame] | 171 | * [strerrorname_np(3)](https://man7.org/linux/man-pages/man3/strerrorname_np.3.html) |
| Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 172 | * returns the name of the errno constant corresponding to its argument. |
| 173 | * `strerrorname_np(38)` would return "ENOSYS", because `ENOSYS` is errno 38. This |
| 174 | * is mostly useful for error reporting in cases where a string like "ENOSYS" is |
| 175 | * more readable than a string like "Function not implemented", which would be |
| 176 | * returned by strerror(). |
| 177 | * |
| 178 | * Returns a pointer to a string, or null for unknown errno values. |
| 179 | * |
| Elliott Hughes | ab47e9a | 2025-04-11 09:56:13 -0700 | [diff] [blame] | 180 | * Available since API level 35 when compiling with `_GNU_SOURCE`. |
| Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 181 | */ |
| Elliott Hughes | 7d1b9cb | 2025-04-10 15:37:10 -0400 | [diff] [blame] | 182 | #if defined(__USE_GNU) && __BIONIC_AVAILABILITY_GUARD(35) |
| Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 183 | const char* _Nullable strerrorname_np(int __errno_value) __INTRODUCED_IN(35); |
| 184 | #endif |
| 185 | |
| 186 | /** |
| Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 187 | * [strerrordesc_np(3)](https://man7.org/linux/man-pages/man3/strerrordesc_np.3.html) |
| Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 188 | * is like strerror() but without localization. Since Android's strerror() |
| 189 | * does not localize, this is the same as strerror() on Android. |
| 190 | * |
| 191 | * Returns a pointer to a string. |
| Elliott Hughes | ab47e9a | 2025-04-11 09:56:13 -0700 | [diff] [blame] | 192 | * |
| 193 | * Available when compiling with `_GNU_SOURCE`. |
| Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 194 | */ |
| 195 | #if defined(__USE_GNU) |
| 196 | const char* _Nonnull strerrordesc_np(int __errno_value) __RENAME(strerror); |
| 197 | #endif |
| 198 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 199 | size_t strnlen(const char* _Nonnull __s, size_t __n) __attribute_pure__; |
| 200 | char* _Nonnull strncat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n); |
| 201 | char* _Nullable strndup(const char* _Nonnull __s, size_t __n); |
| 202 | int strncmp(const char* _Nonnull __lhs, const char* _Nonnull __rhs, size_t __n) __attribute_pure__; |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 203 | char* _Nonnull stpncpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n); |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 204 | char* _Nonnull strncpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 205 | |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 206 | size_t strlcat(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n); |
| 207 | size_t strlcpy(char* _Nonnull __dst, const char* _Nonnull __src, size_t __n); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 208 | |
| Elliott Hughes | 55eba99 | 2025-07-29 10:36:13 -0700 | [diff] [blame] | 209 | /** |
| 210 | * [strcspn(3)](https://man7.org/linux/man-pages/man3/strcspn.3.html) |
| 211 | * returns the length of the prefix containing only characters _not_ in |
| 212 | * the reject set. |
| 213 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 214 | size_t strcspn(const char* _Nonnull __s, const char* _Nonnull __reject) __attribute_pure__; |
| Elliott Hughes | 55eba99 | 2025-07-29 10:36:13 -0700 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * [strpbrk(3)](https://man7.org/linux/man-pages/man3/strpbrk.3.html) |
| 218 | * returns a pointer to the first character in the string that's |
| 219 | * in the accept set, or null. |
| 220 | * |
| 221 | * See strspn() if you want an index instead. |
| 222 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 223 | char* _Nullable strpbrk(const char* _Nonnull __s, const char* _Nonnull __accept) __attribute_pure__; |
| Elliott Hughes | 55eba99 | 2025-07-29 10:36:13 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * [strsep(3)](https://man7.org/linux/man-pages/man3/strsep.3.html) |
| 227 | * extracts tokens (including empty ones) from strings. |
| 228 | * |
| 229 | * See strtok_r() if you don't want empty tokens. |
| 230 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 231 | char* _Nullable strsep(char* _Nullable * _Nonnull __s_ptr, const char* _Nonnull __delimiter); |
| Elliott Hughes | 55eba99 | 2025-07-29 10:36:13 -0700 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * [strspn(3)](https://man7.org/linux/man-pages/man3/strspn.3.html) |
| 235 | * returns the length of the prefix containing only characters in |
| 236 | * the accept set. |
| 237 | * |
| 238 | * See strpbrk() if you want a pointer instead. |
| 239 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 240 | size_t strspn(const char* _Nonnull __s, const char* _Nonnull __accept); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 241 | |
| Elliott Hughes | c107a36 | 2025-06-19 06:41:48 -0700 | [diff] [blame] | 242 | /** |
| 243 | * [strsignal(3)](https://man7.org/linux/man-pages/man3/strsignal.3.html) |
| 244 | * converts the integer corresponding to SIGSEGV (say) into a string |
| 245 | * like "Segmentation violation". |
| 246 | * |
| 247 | * Use sig2str() instead to convert the integer corresponding to SIGSEGV (say) |
| 248 | * into a string like "SEGV". |
| 249 | * |
| 250 | * Returns a pointer to a string. For invalid signals, the string is in TLS. |
| 251 | */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 252 | char* _Nonnull strsignal(int __signal); |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 253 | |
| Elliott Hughes | 50574f5 | 2025-07-29 06:50:15 -0700 | [diff] [blame] | 254 | /** Equivalent to strcmp() on Android. */ |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 255 | int strcoll(const char* _Nonnull __lhs, const char* _Nonnull __rhs) __attribute_pure__; |
| Elliott Hughes | 50574f5 | 2025-07-29 06:50:15 -0700 | [diff] [blame] | 256 | /** Equivalent to strcmp() on Android. */ |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 257 | int strcoll_l(const char* _Nonnull __lhs, const char* _Nonnull __rhs, locale_t _Nonnull __l) __attribute_pure__; |
| Elliott Hughes | 50574f5 | 2025-07-29 06:50:15 -0700 | [diff] [blame] | 258 | |
| 259 | /** Equivalent to strlcpy() on Android. */ |
| 260 | size_t strxfrm(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n); |
| 261 | /** Equivalent to strlcpy() on Android. */ |
| Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 262 | size_t strxfrm_l(char* __BIONIC_COMPLICATED_NULLNESS __dst, const char* _Nonnull __src, size_t __n, locale_t _Nonnull __l); |
| Dan Albert | dfb5ce4 | 2014-07-09 22:51:34 +0000 | [diff] [blame] | 263 | |
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 264 | /* |
| 265 | * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>. |
| 266 | * It doesn't modify its argument, and in C++ it's const-correct. |
| 267 | */ |
| Elliott Hughes | 1749d21 | 2025-04-24 11:59:35 -0700 | [diff] [blame] | 268 | #if defined(__USE_GNU) && __BIONIC_AVAILABILITY_GUARD(23) && !defined(basename) |
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 269 | #if defined(__cplusplus) |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 270 | extern "C++" char* _Nonnull basename(char* _Nullable __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
| 271 | extern "C++" const char* _Nonnull basename(const char* _Nonnull __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 272 | #else |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 273 | char* _Nonnull basename(const char* _Nonnull __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23); |
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 274 | #endif |
| Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 275 | #endif |
| 276 | |
| George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 277 | #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS) |
| 278 | #include <bits/fortify/string.h> |
| 279 | #endif |
| Nick Kralevich | 0a23015 | 2012-06-04 15:20:25 -0700 | [diff] [blame] | 280 | |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 281 | /* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */ |
| Elliott Hughes | 0d1a8a5 | 2018-07-24 19:36:51 +0000 | [diff] [blame] | 282 | #if defined(__cplusplus) |
| George Burgess IV | 61b629d | 2024-08-07 10:51:06 -0600 | [diff] [blame] | 283 | /* libcxx tries to provide these. Suppress that, since libcxx's impl doesn't respect FORTIFY. */ |
| 284 | #define __CORRECT_ISO_CPP_STRING_H_PROTO |
| 285 | /* Used to make these preferable over regular <string.h> signatures for overload resolution. */ |
| 286 | #define __prefer_this_overload __enable_if(true, "") |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 287 | extern "C++" { |
| 288 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 289 | void* _Nullable __bionic_memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n) { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 290 | return memchr(s, c, n); |
| 291 | } |
| 292 | |
| 293 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 294 | const void* _Nullable memchr(const void* _Nonnull const s __pass_object_size, int c, size_t n) |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 295 | __prefer_this_overload { |
| 296 | return __bionic_memchr(s, c, n); |
| 297 | } |
| 298 | |
| 299 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 300 | void* _Nullable memchr(void* _Nonnull const s __pass_object_size, int c, size_t n) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 301 | return __bionic_memchr(s, c, n); |
| 302 | } |
| 303 | |
| 304 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 305 | char* _Nullable __bionic_strchr(const char* _Nonnull const s __pass_object_size, int c) { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 306 | return strchr(s, c); |
| 307 | } |
| 308 | |
| 309 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 310 | const char* _Nullable strchr(const char* _Nonnull const s __pass_object_size, int c) |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 311 | __prefer_this_overload { |
| 312 | return __bionic_strchr(s, c); |
| 313 | } |
| 314 | |
| 315 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 316 | char* _Nullable strchr(char* _Nonnull const s __pass_object_size, int c) |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 317 | __prefer_this_overload { |
| 318 | return __bionic_strchr(s, c); |
| 319 | } |
| 320 | |
| 321 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 322 | char* _Nullable __bionic_strrchr(const char* _Nonnull const s __pass_object_size, int c) { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 323 | return strrchr(s, c); |
| 324 | } |
| 325 | |
| 326 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 327 | const char* _Nullable strrchr(const char* _Nonnull const s __pass_object_size, int c) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 328 | return __bionic_strrchr(s, c); |
| 329 | } |
| 330 | |
| 331 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 332 | char* _Nullable strrchr(char* _Nonnull const s __pass_object_size, int c) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 333 | return __bionic_strrchr(s, c); |
| 334 | } |
| 335 | |
| 336 | /* Functions with no FORTIFY counterpart. */ |
| Elliott Hughes | 28d333b | 2025-04-25 08:19:37 -0700 | [diff] [blame] | 337 | |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 338 | inline __always_inline |
| Elliott Hughes | 28d333b | 2025-04-25 08:19:37 -0700 | [diff] [blame] | 339 | char* _Nullable __bionic_strcasestr(const char* _Nonnull h, const char* _Nonnull n) { |
| 340 | return strcasestr(h, n); |
| 341 | } |
| 342 | |
| 343 | inline __always_inline |
| 344 | const char* _Nullable strcasestr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| 345 | return __bionic_strcasestr(h, n); |
| 346 | } |
| 347 | |
| 348 | inline __always_inline |
| 349 | char* _Nullable strcasestr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| 350 | return __bionic_strcasestr(h, n); |
| 351 | } |
| 352 | |
| 353 | inline __always_inline |
| 354 | char* _Nullable __bionic_strstr(const char* _Nonnull h, const char* _Nonnull n) { |
| 355 | return strstr(h, n); |
| 356 | } |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 357 | |
| 358 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 359 | const char* _Nullable strstr(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 360 | return __bionic_strstr(h, n); |
| 361 | } |
| 362 | |
| 363 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 364 | char* _Nullable strstr(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 365 | return __bionic_strstr(h, n); |
| 366 | } |
| 367 | |
| 368 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 369 | char* _Nullable __bionic_strpbrk(const char* _Nonnull h, const char* _Nonnull n) { return strpbrk(h, n); } |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 370 | |
| 371 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 372 | char* _Nullable strpbrk(char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 373 | return __bionic_strpbrk(h, n); |
| 374 | } |
| 375 | |
| 376 | inline __always_inline |
| zijunzhao | 02c4ef4 | 2023-02-11 00:17:24 +0000 | [diff] [blame] | 377 | const char* _Nullable strpbrk(const char* _Nonnull h, const char* _Nonnull n) __prefer_this_overload { |
| George Burgess IV | bd3d208 | 2017-04-04 17:34:02 -0700 | [diff] [blame] | 378 | return __bionic_strpbrk(h, n); |
| 379 | } |
| 380 | } |
| 381 | #undef __prefer_this_overload |
| 382 | #endif |
| 383 | |
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 384 | __END_DECLS |
| 385 | |
| Elliott Hughes | 684c31a | 2017-08-18 15:07:41 -0700 | [diff] [blame] | 386 | #endif |