Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/tools/lib/string.c
0004  *
0005  *  Copied from linux/lib/string.c, where it is:
0006  *
0007  *  Copyright (C) 1991, 1992  Linus Torvalds
0008  *
0009  *  More specifically, the first copied function was strtobool, which
0010  *  was introduced by:
0011  *
0012  *  d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents")
0013  *  Author: Jonathan Cameron <jic23@cam.ac.uk>
0014  */
0015 
0016 #include <stdlib.h>
0017 #include <string.h>
0018 #include <errno.h>
0019 #include <linux/string.h>
0020 #include <linux/ctype.h>
0021 #include <linux/compiler.h>
0022 
0023 /**
0024  * memdup - duplicate region of memory
0025  *
0026  * @src: memory region to duplicate
0027  * @len: memory region length
0028  */
0029 void *memdup(const void *src, size_t len)
0030 {
0031     void *p = malloc(len);
0032 
0033     if (p)
0034         memcpy(p, src, len);
0035 
0036     return p;
0037 }
0038 
0039 /**
0040  * strtobool - convert common user inputs into boolean values
0041  * @s: input string
0042  * @res: result
0043  *
0044  * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
0045  * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL.  Value
0046  * pointed to by res is updated upon finding a match.
0047  */
0048 int strtobool(const char *s, bool *res)
0049 {
0050     if (!s)
0051         return -EINVAL;
0052 
0053     switch (s[0]) {
0054     case 'y':
0055     case 'Y':
0056     case '1':
0057         *res = true;
0058         return 0;
0059     case 'n':
0060     case 'N':
0061     case '0':
0062         *res = false;
0063         return 0;
0064     case 'o':
0065     case 'O':
0066         switch (s[1]) {
0067         case 'n':
0068         case 'N':
0069             *res = true;
0070             return 0;
0071         case 'f':
0072         case 'F':
0073             *res = false;
0074             return 0;
0075         default:
0076             break;
0077         }
0078     default:
0079         break;
0080     }
0081 
0082     return -EINVAL;
0083 }
0084 
0085 /**
0086  * strlcpy - Copy a C-string into a sized buffer
0087  * @dest: Where to copy the string to
0088  * @src: Where to copy the string from
0089  * @size: size of destination buffer
0090  *
0091  * Compatible with *BSD: the result is always a valid
0092  * NUL-terminated string that fits in the buffer (unless,
0093  * of course, the buffer size is zero). It does not pad
0094  * out the result like strncpy() does.
0095  *
0096  * If libc has strlcpy() then that version will override this
0097  * implementation:
0098  */
0099 #ifdef __clang__
0100 #pragma clang diagnostic push
0101 #pragma clang diagnostic ignored "-Wignored-attributes"
0102 #endif
0103 size_t __weak strlcpy(char *dest, const char *src, size_t size)
0104 {
0105     size_t ret = strlen(src);
0106 
0107     if (size) {
0108         size_t len = (ret >= size) ? size - 1 : ret;
0109         memcpy(dest, src, len);
0110         dest[len] = '\0';
0111     }
0112     return ret;
0113 }
0114 #ifdef __clang__
0115 #pragma clang diagnostic pop
0116 #endif
0117 
0118 /**
0119  * skip_spaces - Removes leading whitespace from @str.
0120  * @str: The string to be stripped.
0121  *
0122  * Returns a pointer to the first non-whitespace character in @str.
0123  */
0124 char *skip_spaces(const char *str)
0125 {
0126     while (isspace(*str))
0127         ++str;
0128     return (char *)str;
0129 }
0130 
0131 /**
0132  * strim - Removes leading and trailing whitespace from @s.
0133  * @s: The string to be stripped.
0134  *
0135  * Note that the first trailing whitespace is replaced with a %NUL-terminator
0136  * in the given string @s. Returns a pointer to the first non-whitespace
0137  * character in @s.
0138  */
0139 char *strim(char *s)
0140 {
0141     size_t size;
0142     char *end;
0143 
0144     size = strlen(s);
0145     if (!size)
0146         return s;
0147 
0148     end = s + size - 1;
0149     while (end >= s && isspace(*end))
0150         end--;
0151     *(end + 1) = '\0';
0152 
0153     return skip_spaces(s);
0154 }
0155 
0156 /**
0157  * strreplace - Replace all occurrences of character in string.
0158  * @s: The string to operate on.
0159  * @old: The character being replaced.
0160  * @new: The character @old is replaced with.
0161  *
0162  * Returns pointer to the nul byte at the end of @s.
0163  */
0164 char *strreplace(char *s, char old, char new)
0165 {
0166     for (; *s; ++s)
0167         if (*s == old)
0168             *s = new;
0169     return s;
0170 }
0171 
0172 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
0173 {
0174     while (bytes) {
0175         if (*start != value)
0176             return (void *)start;
0177         start++;
0178         bytes--;
0179     }
0180     return NULL;
0181 }
0182 
0183 /**
0184  * memchr_inv - Find an unmatching character in an area of memory.
0185  * @start: The memory area
0186  * @c: Find a character other than c
0187  * @bytes: The size of the area.
0188  *
0189  * returns the address of the first character other than @c, or %NULL
0190  * if the whole buffer contains just @c.
0191  */
0192 void *memchr_inv(const void *start, int c, size_t bytes)
0193 {
0194     u8 value = c;
0195     u64 value64;
0196     unsigned int words, prefix;
0197 
0198     if (bytes <= 16)
0199         return check_bytes8(start, value, bytes);
0200 
0201     value64 = value;
0202     value64 |= value64 << 8;
0203     value64 |= value64 << 16;
0204     value64 |= value64 << 32;
0205 
0206     prefix = (unsigned long)start % 8;
0207     if (prefix) {
0208         u8 *r;
0209 
0210         prefix = 8 - prefix;
0211         r = check_bytes8(start, value, prefix);
0212         if (r)
0213             return r;
0214         start += prefix;
0215         bytes -= prefix;
0216     }
0217 
0218     words = bytes / 8;
0219 
0220     while (words) {
0221         if (*(u64 *)start != value64)
0222             return check_bytes8(start, value, 8);
0223         start += 8;
0224         words--;
0225     }
0226 
0227     return check_bytes8(start, value, bytes % 8);
0228 }