WIP FPC-III support
[linux/fpc-iii.git] / arch / s390 / boot / string.c
blobfaccb33b462cf2ab85bb4d315688ac1aa6d644ed
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ctype.h>
3 #include <linux/kernel.h>
4 #include <linux/errno.h>
5 #undef CONFIG_KASAN
6 #undef CONFIG_KASAN_GENERIC
7 #include "../lib/string.c"
9 int strncmp(const char *cs, const char *ct, size_t count)
11 unsigned char c1, c2;
13 while (count) {
14 c1 = *cs++;
15 c2 = *ct++;
16 if (c1 != c2)
17 return c1 < c2 ? -1 : 1;
18 if (!c1)
19 break;
20 count--;
22 return 0;
25 char *skip_spaces(const char *str)
27 while (isspace(*str))
28 ++str;
29 return (char *)str;
32 char *strim(char *s)
34 size_t size;
35 char *end;
37 size = strlen(s);
38 if (!size)
39 return s;
41 end = s + size - 1;
42 while (end >= s && isspace(*end))
43 end--;
44 *(end + 1) = '\0';
46 return skip_spaces(s);
49 /* Works only for digits and letters, but small and fast */
50 #define TOLOWER(x) ((x) | 0x20)
52 static unsigned int simple_guess_base(const char *cp)
54 if (cp[0] == '0') {
55 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
56 return 16;
57 else
58 return 8;
59 } else {
60 return 10;
64 /**
65 * simple_strtoull - convert a string to an unsigned long long
66 * @cp: The start of the string
67 * @endp: A pointer to the end of the parsed string will be placed here
68 * @base: The number base to use
71 unsigned long long simple_strtoull(const char *cp, char **endp,
72 unsigned int base)
74 unsigned long long result = 0;
76 if (!base)
77 base = simple_guess_base(cp);
79 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
80 cp += 2;
82 while (isxdigit(*cp)) {
83 unsigned int value;
85 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
86 if (value >= base)
87 break;
88 result = result * base + value;
89 cp++;
91 if (endp)
92 *endp = (char *)cp;
94 return result;
97 long simple_strtol(const char *cp, char **endp, unsigned int base)
99 if (*cp == '-')
100 return -simple_strtoull(cp + 1, endp, base);
102 return simple_strtoull(cp, endp, base);
105 int kstrtobool(const char *s, bool *res)
107 if (!s)
108 return -EINVAL;
110 switch (s[0]) {
111 case 'y':
112 case 'Y':
113 case '1':
114 *res = true;
115 return 0;
116 case 'n':
117 case 'N':
118 case '0':
119 *res = false;
120 return 0;
121 case 'o':
122 case 'O':
123 switch (s[1]) {
124 case 'n':
125 case 'N':
126 *res = true;
127 return 0;
128 case 'f':
129 case 'F':
130 *res = false;
131 return 0;
132 default:
133 break;
135 default:
136 break;
139 return -EINVAL;