1 // SPDX-License-Identifier: GPL-2.0
3 * linux/tools/lib/string.c
5 * Copied from linux/lib/string.c, where it is:
7 * Copyright (C) 1991, 1992 Linus Torvalds
9 * More specifically, the first copied function was strtobool, which
12 * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
13 * Author: Jonathan Cameron <jic23@cam.ac.uk>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/compiler.h>
24 * memdup - duplicate region of memory
26 * @src: memory region to duplicate
27 * @len: memory region length
29 void *memdup(const void *src
, size_t len
)
31 void *p
= malloc(len
);
40 * strtobool - convert common user inputs into boolean values
44 * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
45 * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
46 * pointed to by res is updated upon finding a match.
48 int strtobool(const char *s
, bool *res
)
86 * strlcpy - Copy a C-string into a sized buffer
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
89 * @size: size of destination buffer
91 * Compatible with *BSD: the result is always a valid
92 * NUL-terminated string that fits in the buffer (unless,
93 * of course, the buffer size is zero). It does not pad
94 * out the result like strncpy() does.
96 * If libc has strlcpy() then that version will override this
100 #pragma clang diagnostic push
101 #pragma clang diagnostic ignored "-Wignored-attributes"
103 size_t __weak
strlcpy(char *dest
, const char *src
, size_t size
)
105 size_t ret
= strlen(src
);
108 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
109 memcpy(dest
, src
, len
);
115 #pragma clang diagnostic pop
119 * skip_spaces - Removes leading whitespace from @str.
120 * @str: The string to be stripped.
122 * Returns a pointer to the first non-whitespace character in @str.
124 char *skip_spaces(const char *str
)
126 while (isspace(*str
))
132 * strim - Removes leading and trailing whitespace from @s.
133 * @s: The string to be stripped.
135 * Note that the first trailing whitespace is replaced with a %NUL-terminator
136 * in the given string @s. Returns a pointer to the first non-whitespace
149 while (end
>= s
&& isspace(*end
))
153 return skip_spaces(s
);
157 * strreplace - Replace all occurrences of character in string.
158 * @s: The string to operate on.
159 * @old: The character being replaced.
160 * @new: The character @old is replaced with.
162 * Returns pointer to the nul byte at the end of @s.
164 char *strreplace(char *s
, char old
, char new)
172 static void *check_bytes8(const u8
*start
, u8 value
, unsigned int bytes
)
176 return (void *)start
;
184 * memchr_inv - Find an unmatching character in an area of memory.
185 * @start: The memory area
186 * @c: Find a character other than c
187 * @bytes: The size of the area.
189 * returns the address of the first character other than @c, or %NULL
190 * if the whole buffer contains just @c.
192 void *memchr_inv(const void *start
, int c
, size_t bytes
)
196 unsigned int words
, prefix
;
199 return check_bytes8(start
, value
, bytes
);
202 value64
|= value64
<< 8;
203 value64
|= value64
<< 16;
204 value64
|= value64
<< 32;
206 prefix
= (unsigned long)start
% 8;
211 r
= check_bytes8(start
, value
, prefix
);
221 if (*(u64
*)start
!= value64
)
222 return check_bytes8(start
, value
, 8);
227 return check_bytes8(start
, value
, bytes
% 8);