Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / include / nolibc / string.h
blob9ec9c24f38c092dee93fab3762c579bffd35ce2f
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3 * string function definitions for NOLIBC
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5 */
7 #ifndef _NOLIBC_STRING_H
8 #define _NOLIBC_STRING_H
10 #include "arch.h"
11 #include "std.h"
13 static void *malloc(size_t len);
16 * As much as possible, please keep functions alphabetically sorted.
19 static __attribute__((unused))
20 int memcmp(const void *s1, const void *s2, size_t n)
22 size_t ofs = 0;
23 int c1 = 0;
25 while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
26 ofs++;
28 return c1;
31 #ifndef NOLIBC_ARCH_HAS_MEMMOVE
32 /* might be ignored by the compiler without -ffreestanding, then found as
33 * missing.
35 __attribute__((weak,unused,section(".text.nolibc_memmove")))
36 void *memmove(void *dst, const void *src, size_t len)
38 size_t dir, pos;
40 pos = len;
41 dir = -1;
43 if (dst < src) {
44 pos = -1;
45 dir = 1;
48 while (len) {
49 pos += dir;
50 ((char *)dst)[pos] = ((const char *)src)[pos];
51 len--;
53 return dst;
55 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMMOVE */
57 #ifndef NOLIBC_ARCH_HAS_MEMCPY
58 /* must be exported, as it's used by libgcc on ARM */
59 __attribute__((weak,unused,section(".text.nolibc_memcpy")))
60 void *memcpy(void *dst, const void *src, size_t len)
62 size_t pos = 0;
64 while (pos < len) {
65 ((char *)dst)[pos] = ((const char *)src)[pos];
66 pos++;
68 return dst;
70 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMCPY */
72 #ifndef NOLIBC_ARCH_HAS_MEMSET
73 /* might be ignored by the compiler without -ffreestanding, then found as
74 * missing.
76 __attribute__((weak,unused,section(".text.nolibc_memset")))
77 void *memset(void *dst, int b, size_t len)
79 char *p = dst;
81 while (len--) {
82 /* prevent gcc from recognizing memset() here */
83 __asm__ volatile("");
84 *(p++) = b;
86 return dst;
88 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
90 static __attribute__((unused))
91 char *strchr(const char *s, int c)
93 while (*s) {
94 if (*s == (char)c)
95 return (char *)s;
96 s++;
98 return NULL;
101 static __attribute__((unused))
102 int strcmp(const char *a, const char *b)
104 unsigned int c;
105 int diff;
107 while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
109 return diff;
112 static __attribute__((unused))
113 char *strcpy(char *dst, const char *src)
115 char *ret = dst;
117 while ((*dst++ = *src++));
118 return ret;
121 /* this function is only used with arguments that are not constants or when
122 * it's not known because optimizations are disabled. Note that gcc 12
123 * recognizes an strlen() pattern and replaces it with a jump to strlen(),
124 * thus itself, hence the asm() statement below that's meant to disable this
125 * confusing practice.
127 __attribute__((weak,unused,section(".text.nolibc_strlen")))
128 size_t strlen(const char *str)
130 size_t len;
132 for (len = 0; str[len]; len++)
133 __asm__("");
134 return len;
137 /* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
138 * the two branches, then will rely on an external definition of strlen().
140 #if defined(__OPTIMIZE__)
141 #define nolibc_strlen(x) strlen(x)
142 #define strlen(str) ({ \
143 __builtin_constant_p((str)) ? \
144 __builtin_strlen((str)) : \
145 nolibc_strlen((str)); \
147 #endif
149 static __attribute__((unused))
150 size_t strnlen(const char *str, size_t maxlen)
152 size_t len;
154 for (len = 0; (len < maxlen) && str[len]; len++);
155 return len;
158 static __attribute__((unused))
159 char *strdup(const char *str)
161 size_t len;
162 char *ret;
164 len = strlen(str);
165 ret = malloc(len + 1);
166 if (__builtin_expect(ret != NULL, 1))
167 memcpy(ret, str, len + 1);
169 return ret;
172 static __attribute__((unused))
173 char *strndup(const char *str, size_t maxlen)
175 size_t len;
176 char *ret;
178 len = strnlen(str, maxlen);
179 ret = malloc(len + 1);
180 if (__builtin_expect(ret != NULL, 1)) {
181 memcpy(ret, str, len);
182 ret[len] = '\0';
185 return ret;
188 static __attribute__((unused))
189 size_t strlcat(char *dst, const char *src, size_t size)
191 size_t len = strnlen(dst, size);
194 * We want len < size-1. But as size is unsigned and can wrap
195 * around, we use len + 1 instead.
197 while (len + 1 < size) {
198 dst[len] = *src;
199 if (*src == '\0')
200 break;
201 len++;
202 src++;
205 if (len < size)
206 dst[len] = '\0';
208 while (*src++)
209 len++;
211 return len;
214 static __attribute__((unused))
215 size_t strlcpy(char *dst, const char *src, size_t size)
217 size_t len;
219 for (len = 0; len < size; len++) {
220 dst[len] = src[len];
221 if (!dst[len])
222 return len;
224 if (size)
225 dst[size-1] = '\0';
227 while (src[len])
228 len++;
230 return len;
233 static __attribute__((unused))
234 char *strncat(char *dst, const char *src, size_t size)
236 char *orig = dst;
238 while (*dst)
239 dst++;
241 while (size && (*dst = *src)) {
242 src++;
243 dst++;
244 size--;
247 *dst = 0;
248 return orig;
251 static __attribute__((unused))
252 int strncmp(const char *a, const char *b, size_t size)
254 unsigned int c;
255 int diff = 0;
257 while (size-- &&
258 !(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
261 return diff;
264 static __attribute__((unused))
265 char *strncpy(char *dst, const char *src, size_t size)
267 size_t len;
269 for (len = 0; len < size; len++)
270 if ((dst[len] = *src))
271 src++;
272 return dst;
275 static __attribute__((unused))
276 char *strrchr(const char *s, int c)
278 const char *ret = NULL;
280 while (*s) {
281 if (*s == (char)c)
282 ret = s;
283 s++;
285 return (char *)ret;
288 /* make sure to include all global symbols */
289 #include "nolibc.h"
291 #endif /* _NOLIBC_STRING_H */