1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/ctype.h> 3 #include <linux/kernel.h> 4 #include "../lib/string.c" 5 6 int strncmp(const char *cs, const char *ct, size_t count) 7 { 8 unsigned char c1, c2; 9 10 while (count) { 11 c1 = *cs++; 12 c2 = *ct++; 13 if (c1 != c2) 14 return c1 < c2 ? -1 : 1; 15 if (!c1) 16 break; 17 count--; 18 } 19 return 0; 20 } 21 22 char *skip_spaces(const char *str) 23 { 24 while (isspace(*str)) 25 ++str; 26 return (char *)str; 27 } 28 29 char *strim(char *s) 30 { 31 size_t size; 32 char *end; 33 34 size = strlen(s); 35 if (!size) 36 return s; 37 38 end = s + size - 1; 39 while (end >= s && isspace(*end)) 40 end--; 41 *(end + 1) = '\0'; 42 43 return skip_spaces(s); 44 } 45 46 /* Works only for digits and letters, but small and fast */ 47 #define TOLOWER(x) ((x) | 0x20) 48 49 static unsigned int simple_guess_base(const char *cp) 50 { 51 if (cp[0] == '0') { 52 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) 53 return 16; 54 else 55 return 8; 56 } else { 57 return 10; 58 } 59 } 60 61 /** 62 * simple_strtoull - convert a string to an unsigned long long 63 * @cp: The start of the string 64 * @endp: A pointer to the end of the parsed string will be placed here 65 * @base: The number base to use 66 */ 67 68 unsigned long long simple_strtoull(const char *cp, char **endp, 69 unsigned int base) 70 { 71 unsigned long long result = 0; 72 73 if (!base) 74 base = simple_guess_base(cp); 75 76 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 77 cp += 2; 78 79 while (isxdigit(*cp)) { 80 unsigned int value; 81 82 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 83 if (value >= base) 84 break; 85 result = result * base + value; 86 cp++; 87 } 88 if (endp) 89 *endp = (char *)cp; 90 91 return result; 92 } 93 94 long simple_strtol(const char *cp, char **endp, unsigned int base) 95 { 96 if (*cp == '-') 97 return -simple_strtoull(cp + 1, endp, base); 98 99 return simple_strtoull(cp, endp, base); 100 } 101