string.c (14e77332e74603efab8347c89d3cda447c3b97c9) string.c (03699f271de1f4df6369cd379506539cd7d590d3)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/lib/string.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/*

--- 62 unchanged lines hidden (view full) ---

71 c2 = tolower(*s2++);
72 } while (c1 == c2 && c1 != 0);
73 return c1 - c2;
74}
75EXPORT_SYMBOL(strcasecmp);
76#endif
77
78#ifndef __HAVE_ARCH_STRCPY
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * linux/lib/string.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/*

--- 62 unchanged lines hidden (view full) ---

71 c2 = tolower(*s2++);
72 } while (c1 == c2 && c1 != 0);
73 return c1 - c2;
74}
75EXPORT_SYMBOL(strcasecmp);
76#endif
77
78#ifndef __HAVE_ARCH_STRCPY
79/**
80 * strcpy - Copy a %NUL terminated string
81 * @dest: Where to copy the string to
82 * @src: Where to copy the string from
83 */
84char *strcpy(char *dest, const char *src)
85{
86 char *tmp = dest;
87
88 while ((*dest++ = *src++) != '\0')
89 /* nothing */;
90 return tmp;
91}
92EXPORT_SYMBOL(strcpy);
93#endif
94
95#ifndef __HAVE_ARCH_STRNCPY
79char *strcpy(char *dest, const char *src)
80{
81 char *tmp = dest;
82
83 while ((*dest++ = *src++) != '\0')
84 /* nothing */;
85 return tmp;
86}
87EXPORT_SYMBOL(strcpy);
88#endif
89
90#ifndef __HAVE_ARCH_STRNCPY
96/**
97 * strncpy - Copy a length-limited, C-string
98 * @dest: Where to copy the string to
99 * @src: Where to copy the string from
100 * @count: The maximum number of bytes to copy
101 *
102 * The result is not %NUL-terminated if the source exceeds
103 * @count bytes.
104 *
105 * In the case where the length of @src is less than that of
106 * count, the remainder of @dest will be padded with %NUL.
107 *
108 */
109char *strncpy(char *dest, const char *src, size_t count)
110{
111 char *tmp = dest;
112
113 while (count) {
114 if ((*tmp = *src) != 0)
115 src++;
116 tmp++;
117 count--;
118 }
119 return dest;
120}
121EXPORT_SYMBOL(strncpy);
122#endif
123
124#ifndef __HAVE_ARCH_STRLCPY
91char *strncpy(char *dest, const char *src, size_t count)
92{
93 char *tmp = dest;
94
95 while (count) {
96 if ((*tmp = *src) != 0)
97 src++;
98 tmp++;
99 count--;
100 }
101 return dest;
102}
103EXPORT_SYMBOL(strncpy);
104#endif
105
106#ifndef __HAVE_ARCH_STRLCPY
125/**
126 * strlcpy - Copy a C-string into a sized buffer
127 * @dest: Where to copy the string to
128 * @src: Where to copy the string from
129 * @size: size of destination buffer
130 *
131 * Compatible with ``*BSD``: the result is always a valid
132 * NUL-terminated string that fits in the buffer (unless,
133 * of course, the buffer size is zero). It does not pad
134 * out the result like strncpy() does.
135 */
136size_t strlcpy(char *dest, const char *src, size_t size)
137{
138 size_t ret = strlen(src);
139
140 if (size) {
141 size_t len = (ret >= size) ? size - 1 : ret;
142 memcpy(dest, src, len);
143 dest[len] = '\0';
144 }
145 return ret;
146}
147EXPORT_SYMBOL(strlcpy);
148#endif
149
150#ifndef __HAVE_ARCH_STRSCPY
107size_t strlcpy(char *dest, const char *src, size_t size)
108{
109 size_t ret = strlen(src);
110
111 if (size) {
112 size_t len = (ret >= size) ? size - 1 : ret;
113 memcpy(dest, src, len);
114 dest[len] = '\0';
115 }
116 return ret;
117}
118EXPORT_SYMBOL(strlcpy);
119#endif
120
121#ifndef __HAVE_ARCH_STRSCPY
151/**
152 * strscpy - Copy a C-string into a sized buffer
153 * @dest: Where to copy the string to
154 * @src: Where to copy the string from
155 * @count: Size of destination buffer
156 *
157 * Copy the string, or as much of it as fits, into the dest buffer. The
158 * behavior is undefined if the string buffers overlap. The destination
159 * buffer is always NUL terminated, unless it's zero-sized.
160 *
161 * Preferred to strlcpy() since the API doesn't require reading memory
162 * from the src string beyond the specified "count" bytes, and since
163 * the return value is easier to error-check than strlcpy()'s.
164 * In addition, the implementation is robust to the string changing out
165 * from underneath it, unlike the current strlcpy() implementation.
166 *
167 * Preferred to strncpy() since it always returns a valid string, and
168 * doesn't unnecessarily force the tail of the destination buffer to be
169 * zeroed. If zeroing is desired please use strscpy_pad().
170 *
171 * Returns:
172 * * The number of characters copied (not including the trailing %NUL)
173 * * -E2BIG if count is 0 or @src was truncated.
174 */
175ssize_t strscpy(char *dest, const char *src, size_t count)
176{
177 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
178 size_t max = count;
179 long res = 0;
180
181 if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
182 return -E2BIG;

--- 78 unchanged lines hidden (view full) ---

261{
262 while ((*dest++ = *src++) != '\0')
263 /* nothing */;
264 return --dest;
265}
266EXPORT_SYMBOL(stpcpy);
267
268#ifndef __HAVE_ARCH_STRCAT
122ssize_t strscpy(char *dest, const char *src, size_t count)
123{
124 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
125 size_t max = count;
126 long res = 0;
127
128 if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
129 return -E2BIG;

--- 78 unchanged lines hidden (view full) ---

208{
209 while ((*dest++ = *src++) != '\0')
210 /* nothing */;
211 return --dest;
212}
213EXPORT_SYMBOL(stpcpy);
214
215#ifndef __HAVE_ARCH_STRCAT
269/**
270 * strcat - Append one %NUL-terminated string to another
271 * @dest: The string to be appended to
272 * @src: The string to append to it
273 */
274char *strcat(char *dest, const char *src)
275{
276 char *tmp = dest;
277
278 while (*dest)
279 dest++;
280 while ((*dest++ = *src++) != '\0')
281 ;
282 return tmp;
283}
284EXPORT_SYMBOL(strcat);
285#endif
286
287#ifndef __HAVE_ARCH_STRNCAT
216char *strcat(char *dest, const char *src)
217{
218 char *tmp = dest;
219
220 while (*dest)
221 dest++;
222 while ((*dest++ = *src++) != '\0')
223 ;
224 return tmp;
225}
226EXPORT_SYMBOL(strcat);
227#endif
228
229#ifndef __HAVE_ARCH_STRNCAT
288/**
289 * strncat - Append a length-limited, C-string to another
290 * @dest: The string to be appended to
291 * @src: The string to append to it
292 * @count: The maximum numbers of bytes to copy
293 *
294 * Note that in contrast to strncpy(), strncat() ensures the result is
295 * terminated.
296 */
297char *strncat(char *dest, const char *src, size_t count)
298{
299 char *tmp = dest;
300
301 if (count) {
302 while (*dest)
303 dest++;
304 while ((*dest++ = *src++) != 0) {

--- 4 unchanged lines hidden (view full) ---

309 }
310 }
311 return tmp;
312}
313EXPORT_SYMBOL(strncat);
314#endif
315
316#ifndef __HAVE_ARCH_STRLCAT
230char *strncat(char *dest, const char *src, size_t count)
231{
232 char *tmp = dest;
233
234 if (count) {
235 while (*dest)
236 dest++;
237 while ((*dest++ = *src++) != 0) {

--- 4 unchanged lines hidden (view full) ---

242 }
243 }
244 return tmp;
245}
246EXPORT_SYMBOL(strncat);
247#endif
248
249#ifndef __HAVE_ARCH_STRLCAT
317/**
318 * strlcat - Append a length-limited, C-string to another
319 * @dest: The string to be appended to
320 * @src: The string to append to it
321 * @count: The size of the destination buffer.
322 */
323size_t strlcat(char *dest, const char *src, size_t count)
324{
325 size_t dsize = strlen(dest);
326 size_t len = strlen(src);
327 size_t res = dsize + len;
328
329 /* This would be a bug */
330 BUG_ON(dsize >= count);

--- 148 unchanged lines hidden (view full) ---

479 break;
480 }
481 return NULL;
482}
483EXPORT_SYMBOL(strnchr);
484#endif
485
486#ifndef __HAVE_ARCH_STRLEN
250size_t strlcat(char *dest, const char *src, size_t count)
251{
252 size_t dsize = strlen(dest);
253 size_t len = strlen(src);
254 size_t res = dsize + len;
255
256 /* This would be a bug */
257 BUG_ON(dsize >= count);

--- 148 unchanged lines hidden (view full) ---

406 break;
407 }
408 return NULL;
409}
410EXPORT_SYMBOL(strnchr);
411#endif
412
413#ifndef __HAVE_ARCH_STRLEN
487/**
488 * strlen - Find the length of a string
489 * @s: The string to be sized
490 */
491size_t strlen(const char *s)
492{
493 const char *sc;
494
495 for (sc = s; *sc != '\0'; ++sc)
496 /* nothing */;
497 return sc - s;
498}
499EXPORT_SYMBOL(strlen);
500#endif
501
502#ifndef __HAVE_ARCH_STRNLEN
414size_t strlen(const char *s)
415{
416 const char *sc;
417
418 for (sc = s; *sc != '\0'; ++sc)
419 /* nothing */;
420 return sc - s;
421}
422EXPORT_SYMBOL(strlen);
423#endif
424
425#ifndef __HAVE_ARCH_STRNLEN
503/**
504 * strnlen - Find the length of a length-limited string
505 * @s: The string to be sized
506 * @count: The maximum number of bytes to search
507 */
508size_t strnlen(const char *s, size_t count)
509{
510 const char *sc;
511
512 for (sc = s; count-- && *sc != '\0'; ++sc)
513 /* nothing */;
514 return sc - s;
515}

--- 449 unchanged lines hidden ---
426size_t strnlen(const char *s, size_t count)
427{
428 const char *sc;
429
430 for (sc = s; count-- && *sc != '\0'; ++sc)
431 /* nothing */;
432 return sc - s;
433}

--- 449 unchanged lines hidden ---