^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/tools/lib/string.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copied from linux/lib/string.c, where it is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 1991, 1992 Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * More specifically, the first copied function was strtobool, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * was introduced by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Author: Jonathan Cameron <jic23@cam.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * memdup - duplicate region of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @src: memory region to duplicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @len: memory region length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void *memdup(const void *src, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void *p = malloc(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) memcpy(p, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * strtobool - convert common user inputs into boolean values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @s: input string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @res: result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * pointed to by res is updated upon finding a match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int strtobool(const char *s, bool *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) switch (s[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) case 'y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) case 'Y':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) case '1':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) case 'N':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case '0':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) case 'o':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case 'O':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) switch (s[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) case 'n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case 'N':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *res = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) case 'F':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) *res = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * strlcpy - Copy a C-string into a sized buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * @dest: Where to copy the string to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * @src: Where to copy the string from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @size: size of destination buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Compatible with *BSD: the result is always a valid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * NUL-terminated string that fits in the buffer (unless,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * of course, the buffer size is zero). It does not pad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * out the result like strncpy() does.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * If libc has strlcpy() then that version will override this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * implementation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #ifdef __clang__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #pragma clang diagnostic push
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #pragma clang diagnostic ignored "-Wignored-attributes"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) size_t __weak strlcpy(char *dest, const char *src, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) size_t ret = strlen(src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) size_t len = (ret >= size) ? size - 1 : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) memcpy(dest, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) dest[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #ifdef __clang__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #pragma clang diagnostic pop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * skip_spaces - Removes leading whitespace from @str.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @str: The string to be stripped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Returns a pointer to the first non-whitespace character in @str.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char *skip_spaces(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) while (isspace(*str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ++str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return (char *)str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * strim - Removes leading and trailing whitespace from @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @s: The string to be stripped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * Note that the first trailing whitespace is replaced with a %NUL-terminator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * in the given string @s. Returns a pointer to the first non-whitespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * character in @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) char *strim(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) char *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) size = strlen(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) end = s + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) while (end >= s && isspace(*end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) end--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) *(end + 1) = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return skip_spaces(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * strreplace - Replace all occurrences of character in string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @s: The string to operate on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @old: The character being replaced.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @new: The character @old is replaced with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * Returns pointer to the nul byte at the end of @s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) char *strreplace(char *s, char old, char new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) for (; *s; ++s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (*s == old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *s = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) while (bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (*start != value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return (void *)start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) start++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) bytes--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * memchr_inv - Find an unmatching character in an area of memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @start: The memory area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @c: Find a character other than c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * @bytes: The size of the area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * returns the address of the first character other than @c, or %NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * if the whole buffer contains just @c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) void *memchr_inv(const void *start, int c, size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) u8 value = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u64 value64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) unsigned int words, prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (bytes <= 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return check_bytes8(start, value, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) value64 = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) value64 |= value64 << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) value64 |= value64 << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) value64 |= value64 << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) prefix = (unsigned long)start % 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (prefix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) u8 *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) prefix = 8 - prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) r = check_bytes8(start, value, prefix);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) start += prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) bytes -= prefix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) words = bytes / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) while (words) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (*(u64 *)start != value64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return check_bytes8(start, value, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) start += 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) words--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return check_bytes8(start, value, bytes % 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }