Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/ctype.h>
0003 #include <linux/kernel.h>
0004 #include <linux/errno.h>
0005 #undef CONFIG_KASAN
0006 #undef CONFIG_KASAN_GENERIC
0007 #include "../lib/string.c"
0008 
0009 int strncmp(const char *cs, const char *ct, size_t count)
0010 {
0011     unsigned char c1, c2;
0012 
0013     while (count) {
0014         c1 = *cs++;
0015         c2 = *ct++;
0016         if (c1 != c2)
0017             return c1 < c2 ? -1 : 1;
0018         if (!c1)
0019             break;
0020         count--;
0021     }
0022     return 0;
0023 }
0024 
0025 char *skip_spaces(const char *str)
0026 {
0027     while (isspace(*str))
0028         ++str;
0029     return (char *)str;
0030 }
0031 
0032 char *strim(char *s)
0033 {
0034     size_t size;
0035     char *end;
0036 
0037     size = strlen(s);
0038     if (!size)
0039         return s;
0040 
0041     end = s + size - 1;
0042     while (end >= s && isspace(*end))
0043         end--;
0044     *(end + 1) = '\0';
0045 
0046     return skip_spaces(s);
0047 }
0048 
0049 /* Works only for digits and letters, but small and fast */
0050 #define TOLOWER(x) ((x) | 0x20)
0051 
0052 static unsigned int simple_guess_base(const char *cp)
0053 {
0054     if (cp[0] == '0') {
0055         if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
0056             return 16;
0057         else
0058             return 8;
0059     } else {
0060         return 10;
0061     }
0062 }
0063 
0064 /**
0065  * simple_strtoull - convert a string to an unsigned long long
0066  * @cp: The start of the string
0067  * @endp: A pointer to the end of the parsed string will be placed here
0068  * @base: The number base to use
0069  */
0070 
0071 unsigned long long simple_strtoull(const char *cp, char **endp,
0072                    unsigned int base)
0073 {
0074     unsigned long long result = 0;
0075 
0076     if (!base)
0077         base = simple_guess_base(cp);
0078 
0079     if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
0080         cp += 2;
0081 
0082     while (isxdigit(*cp)) {
0083         unsigned int value;
0084 
0085         value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
0086         if (value >= base)
0087             break;
0088         result = result * base + value;
0089         cp++;
0090     }
0091     if (endp)
0092         *endp = (char *)cp;
0093 
0094     return result;
0095 }
0096 
0097 long simple_strtol(const char *cp, char **endp, unsigned int base)
0098 {
0099     if (*cp == '-')
0100         return -simple_strtoull(cp + 1, endp, base);
0101 
0102     return simple_strtoull(cp, endp, base);
0103 }
0104 
0105 int kstrtobool(const char *s, bool *res)
0106 {
0107     if (!s)
0108         return -EINVAL;
0109 
0110     switch (s[0]) {
0111     case 'y':
0112     case 'Y':
0113     case '1':
0114         *res = true;
0115         return 0;
0116     case 'n':
0117     case 'N':
0118     case '0':
0119         *res = false;
0120         return 0;
0121     case 'o':
0122     case 'O':
0123         switch (s[1]) {
0124         case 'n':
0125         case 'N':
0126             *res = true;
0127             return 0;
0128         case 'f':
0129         case 'F':
0130             *res = false;
0131             return 0;
0132         default:
0133             break;
0134         }
0135     default:
0136         break;
0137     }
0138 
0139     return -EINVAL;
0140 }