Home | History | Annotate | Download | only in include
      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)
     41 #include <strings.h>
     42 #endif
     43 
     44 void* memccpy(void* __dst, const void* __src, int __stop_char, size_t __n);
     45 void* memchr(const void* __s, int __ch, size_t __n) __attribute_pure__;
     46 #if defined(__cplusplus)
     47 extern "C++" void* memrchr(void* __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
     48 extern "C++" const void* memrchr(const void* __s, int __ch, size_t __n) __RENAME(memrchr) __attribute_pure__;
     49 #else
     50 void* memrchr(const void* __s, int __ch, size_t __n) __attribute_pure__;
     51 #endif
     52 int memcmp(const void* __lhs, const void* __rhs, size_t __n) __attribute_pure__;
     53 void* memcpy(void*, const void*, size_t);
     54 #if defined(__USE_GNU)
     55 void* mempcpy(void* __dst, const void* __src, size_t __n) __INTRODUCED_IN(23);
     56 #endif
     57 void* memmove(void* __dst, const void* __src, size_t __n);
     58 void* memset(void* __dst, int __ch, size_t __n);
     59 void* memmem(const void* __haystack, size_t __haystack_size, const void* __needle, size_t __needle_size) __attribute_pure__;
     60 
     61 char* strchr(const char* __s, int __ch) __attribute_pure__;
     62 char* __strchr_chk(const char* __s, int __ch, size_t __n) __INTRODUCED_IN(18);
     63 #if defined(__USE_GNU)
     64 #if defined(__cplusplus)
     65 extern "C++" char* strchrnul(char* __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
     66 extern "C++" const char* strchrnul(const char* __s, int __ch) __RENAME(strchrnul) __attribute_pure__ __INTRODUCED_IN(24);
     67 #else
     68 char* strchrnul(const char* __s, int __ch) __attribute_pure__ __INTRODUCED_IN(24);
     69 #endif
     70 #endif
     71 
     72 char* strrchr(const char* __s, int __ch) __attribute_pure__;
     73 char* __strrchr_chk(const char* __s, int __ch, size_t __n) __INTRODUCED_IN(18);
     74 
     75 size_t strlen(const char* __s) __attribute_pure__;
     76 size_t __strlen_chk(const char* __s, size_t __n) __INTRODUCED_IN(17);
     77 
     78 int strcmp(const char* __lhs, const char* __rhs) __attribute_pure__;
     79 char* stpcpy(char* __dst, const char* __src) __INTRODUCED_IN(21);
     80 char* strcpy(char* __dst, const char* __src);
     81 char* strcat(char* __dst, const char* __src);
     82 char* strdup(const char* __s);
     83 
     84 char* strstr(const char* __haystack, const char* __needle) __attribute_pure__;
     85 #if defined(__cplusplus)
     86 extern "C++" char* strcasestr(char*, const char*) __RENAME(strcasestr) __attribute_pure__;
     87 extern "C++" const char* strcasestr(const char*, const char*) __RENAME(strcasestr) __attribute_pure__;
     88 #else
     89 char* strcasestr(const char* __haystack, const char* __needle) __attribute_pure__;
     90 #endif
     91 char* strtok(char* __s, const char* __delimiter);
     92 char* strtok_r(char* __s, const char* __delimiter, char** __pos_ptr);
     93 
     94 char* strerror(int __errno_value);
     95 char* strerror_l(int __errno_value, locale_t __l) __INTRODUCED_IN(23);
     96 #if defined(__USE_GNU) && __ANDROID_API__ >= 23
     97 char* strerror_r(int __errno_value, char* __buf, size_t __n) __RENAME(__gnu_strerror_r) __INTRODUCED_IN(23);
     98 #else /* POSIX */
     99 int strerror_r(int __errno_value, char* __buf, size_t __n);
    100 #endif
    101 
    102 size_t strnlen(const char* __s, size_t __n) __attribute_pure__;
    103 char* strncat(char* __dst, const char* __src, size_t __n);
    104 char* strndup(const char* __s, size_t __n);
    105 int strncmp(const char* __lhs, const char* __rhs, size_t __n) __attribute_pure__;
    106 char* stpncpy(char* __dst, const char* __src, size_t __n) __INTRODUCED_IN(21);
    107 char* strncpy(char* __dst, const char* __src, size_t __n);
    108 
    109 size_t strlcat(char* __dst, const char* __src, size_t __n);
    110 size_t strlcpy(char* __dst, const char* __src, size_t __n);
    111 
    112 size_t strcspn(const char* __s, const char* __reject) __attribute_pure__;
    113 char* strpbrk(const char* __s, const char* __accept) __attribute_pure__;
    114 char* strsep(char** __s_ptr, const char* __delimiter);
    115 size_t strspn(const char* __s, const char* __accept);
    116 
    117 char* strsignal(int __signal);
    118 
    119 int strcoll(const char* __lhs, const char* __rhs) __attribute_pure__;
    120 size_t strxfrm(char* __dst, const char* __src, size_t __n);
    121 
    122 #if __ANDROID_API__ >= __ANDROID_API_L__
    123 int strcoll_l(const char* __lhs, const char* __rhs, locale_t __l) __attribute_pure__ __INTRODUCED_IN(21);
    124 size_t strxfrm_l(char* __dst, const char* __src, size_t __n, locale_t __l) __INTRODUCED_IN(21);
    125 #else
    126 // Implemented as static inlines before 21.
    127 #endif
    128 
    129 #if defined(__USE_GNU) && !defined(basename)
    130 /*
    131  * glibc has a basename in <string.h> that's different to the POSIX one in <libgen.h>.
    132  * It doesn't modify its argument, and in C++ it's const-correct.
    133  */
    134 #if defined(__cplusplus)
    135 extern "C++" char* basename(char* __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
    136 extern "C++" const char* basename(const char* __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
    137 #else
    138 char* basename(const char* __path) __RENAME(__gnu_basename) __INTRODUCED_IN(23);
    139 #endif
    140 #endif
    141 
    142 #if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
    143 #include <bits/fortify/string.h>
    144 #endif
    145 
    146 /* Const-correct overloads. Placed after FORTIFY so we call those functions, if possible. */
    147 #if defined(__cplusplus)
    148 /*
    149  * Use two enable_ifs so these overloads don't conflict with + are preferred over libcxx's. This can
    150  * be reduced to 1 after libcxx recognizes that we have const-correct overloads.
    151  */
    152 #define __prefer_this_overload __enable_if(true, "preferred overload") __enable_if(true, "")
    153 extern "C++" {
    154 inline __always_inline
    155 void* __bionic_memchr(const void* const s __pass_object_size, int c, size_t n) {
    156     return memchr(s, c, n);
    157 }
    158 
    159 inline __always_inline
    160 const void* memchr(const void* const s __pass_object_size, int c, size_t n)
    161         __prefer_this_overload {
    162     return __bionic_memchr(s, c, n);
    163 }
    164 
    165 inline __always_inline
    166 void* memchr(void* const s __pass_object_size, int c, size_t n) __prefer_this_overload {
    167     return __bionic_memchr(s, c, n);
    168 }
    169 
    170 inline __always_inline
    171 char* __bionic_strchr(const char* const s __pass_object_size, int c) {
    172     return strchr(s, c);
    173 }
    174 
    175 inline __always_inline
    176 const char* strchr(const char* const s __pass_object_size, int c)
    177         __prefer_this_overload {
    178     return __bionic_strchr(s, c);
    179 }
    180 
    181 inline __always_inline
    182 char* strchr(char* const s __pass_object_size, int c)
    183         __prefer_this_overload {
    184     return __bionic_strchr(s, c);
    185 }
    186 
    187 inline __always_inline
    188 char* __bionic_strrchr(const char* const s __pass_object_size, int c) {
    189     return strrchr(s, c);
    190 }
    191 
    192 inline __always_inline
    193 const char* strrchr(const char* const s __pass_object_size, int c) __prefer_this_overload {
    194     return __bionic_strrchr(s, c);
    195 }
    196 
    197 inline __always_inline
    198 char* strrchr(char* const s __pass_object_size, int c) __prefer_this_overload {
    199     return __bionic_strrchr(s, c);
    200 }
    201 
    202 /* Functions with no FORTIFY counterpart. */
    203 inline __always_inline
    204 char* __bionic_strstr(const char* h, const char* n) { return strstr(h, n); }
    205 
    206 inline __always_inline
    207 const char* strstr(const char* h, const char* n) __prefer_this_overload {
    208     return __bionic_strstr(h, n);
    209 }
    210 
    211 inline __always_inline
    212 char* strstr(char* h, const char* n) __prefer_this_overload {
    213     return __bionic_strstr(h, n);
    214 }
    215 
    216 inline __always_inline
    217 char* __bionic_strpbrk(const char* h, const char* n) { return strpbrk(h, n); }
    218 
    219 inline __always_inline
    220 char* strpbrk(char* h, const char* n) __prefer_this_overload {
    221     return __bionic_strpbrk(h, n);
    222 }
    223 
    224 inline __always_inline
    225 const char* strpbrk(const char* h, const char* n) __prefer_this_overload {
    226     return __bionic_strpbrk(h, n);
    227 }
    228 }
    229 #undef __prefer_this_overload
    230 #endif
    231 
    232 __END_DECLS
    233 
    234 #endif
    235