1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
3 * string function definitions for NOLIBC
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
7 #ifndef _NOLIBC_STRING_H
8 #define _NOLIBC_STRING_H
12 static void *malloc(size_t len);
15 * As much as possible, please keep functions alphabetically sorted.
18 static __attribute__((unused))
19 int memcmp(const void *s1, const void *s2, size_t n)
24 while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
30 #ifndef NOLIBC_ARCH_HAS_MEMMOVE
31 /* might be ignored by the compiler without -ffreestanding, then found as
34 __attribute__((weak,unused,section(".text.nolibc_memmove")))
35 void *memmove(void *dst, const void *src, size_t len)
49 ((char *)dst)[pos] = ((const char *)src)[pos];
54 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMMOVE */
56 #ifndef NOLIBC_ARCH_HAS_MEMCPY
57 /* must be exported, as it's used by libgcc on ARM */
58 __attribute__((weak,unused,section(".text.nolibc_memcpy")))
59 void *memcpy(void *dst, const void *src, size_t len)
64 ((char *)dst)[pos] = ((const char *)src)[pos];
69 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMCPY */
71 #ifndef NOLIBC_ARCH_HAS_MEMSET
72 /* might be ignored by the compiler without -ffreestanding, then found as
75 __attribute__((weak,unused,section(".text.nolibc_memset")))
76 void *memset(void *dst, int b, size_t len)
81 /* prevent gcc from recognizing memset() here */
87 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
89 static __attribute__((unused))
90 char *strchr(const char *s, int c)
100 static __attribute__((unused))
101 int strcmp(const char *a, const char *b)
106 while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
111 static __attribute__((unused))
112 char *strcpy(char *dst, const char *src)
116 while ((*dst++ = *src++));
120 /* this function is only used with arguments that are not constants or when
121 * it's not known because optimizations are disabled. Note that gcc 12
122 * recognizes an strlen() pattern and replaces it with a jump to strlen(),
123 * thus itself, hence the asm() statement below that's meant to disable this
124 * confusing practice.
126 static __attribute__((unused))
127 size_t strlen(const char *str)
131 for (len = 0; str[len]; len++)
136 /* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
137 * the two branches, then will rely on an external definition of strlen().
139 #if defined(__OPTIMIZE__)
140 #define nolibc_strlen(x) strlen(x)
141 #define strlen(str) ({ \
142 __builtin_constant_p((str)) ? \
143 __builtin_strlen((str)) : \
144 nolibc_strlen((str)); \
148 static __attribute__((unused))
149 size_t strnlen(const char *str, size_t maxlen)
153 for (len = 0; (len < maxlen) && str[len]; len++);
157 static __attribute__((unused))
158 char *strdup(const char *str)
164 ret = malloc(len + 1);
165 if (__builtin_expect(ret != NULL, 1))
166 memcpy(ret, str, len + 1);
171 static __attribute__((unused))
172 char *strndup(const char *str, size_t maxlen)
177 len = strnlen(str, maxlen);
178 ret = malloc(len + 1);
179 if (__builtin_expect(ret != NULL, 1)) {
180 memcpy(ret, str, len);
187 static __attribute__((unused))
188 size_t strlcat(char *dst, const char *src, size_t size)
193 for (len = 0; dst[len]; len++)
209 static __attribute__((unused))
210 size_t strlcpy(char *dst, const char *src, size_t size)
226 static __attribute__((unused))
227 char *strncat(char *dst, const char *src, size_t size)
234 while (size && (*dst = *src)) {
244 static __attribute__((unused))
245 int strncmp(const char *a, const char *b, size_t size)
251 !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
257 static __attribute__((unused))
258 char *strncpy(char *dst, const char *src, size_t size)
262 for (len = 0; len < size; len++)
263 if ((dst[len] = *src))
268 static __attribute__((unused))
269 char *strrchr(const char *s, int c)
271 const char *ret = NULL;
281 /* make sure to include all global symbols */
284 #endif /* _NOLIBC_STRING_H */