hugetlb: introduce generic version of hugetlb_free_pgd_range
[linux/fpc-iii.git] / arch / s390 / boot / string.c
blob25aca07898bac7c6e21ea6839bf0065dc4b57f31
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ctype.h>
3 #include <linux/kernel.h>
4 #include <linux/errno.h>
5 #include "../lib/string.c"
7 int strncmp(const char *cs, const char *ct, size_t count)
9 unsigned char c1, c2;
11 while (count) {
12 c1 = *cs++;
13 c2 = *ct++;
14 if (c1 != c2)
15 return c1 < c2 ? -1 : 1;
16 if (!c1)
17 break;
18 count--;
20 return 0;
23 char *skip_spaces(const char *str)
25 while (isspace(*str))
26 ++str;
27 return (char *)str;
30 char *strim(char *s)
32 size_t size;
33 char *end;
35 size = strlen(s);
36 if (!size)
37 return s;
39 end = s + size - 1;
40 while (end >= s && isspace(*end))
41 end--;
42 *(end + 1) = '\0';
44 return skip_spaces(s);
47 /* Works only for digits and letters, but small and fast */
48 #define TOLOWER(x) ((x) | 0x20)
50 static unsigned int simple_guess_base(const char *cp)
52 if (cp[0] == '0') {
53 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
54 return 16;
55 else
56 return 8;
57 } else {
58 return 10;
62 /**
63 * simple_strtoull - convert a string to an unsigned long long
64 * @cp: The start of the string
65 * @endp: A pointer to the end of the parsed string will be placed here
66 * @base: The number base to use
69 unsigned long long simple_strtoull(const char *cp, char **endp,
70 unsigned int base)
72 unsigned long long result = 0;
74 if (!base)
75 base = simple_guess_base(cp);
77 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
78 cp += 2;
80 while (isxdigit(*cp)) {
81 unsigned int value;
83 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
84 if (value >= base)
85 break;
86 result = result * base + value;
87 cp++;
89 if (endp)
90 *endp = (char *)cp;
92 return result;
95 long simple_strtol(const char *cp, char **endp, unsigned int base)
97 if (*cp == '-')
98 return -simple_strtoull(cp + 1, endp, base);
100 return simple_strtoull(cp, endp, base);
103 int kstrtobool(const char *s, bool *res)
105 if (!s)
106 return -EINVAL;
108 switch (s[0]) {
109 case 'y':
110 case 'Y':
111 case '1':
112 *res = true;
113 return 0;
114 case 'n':
115 case 'N':
116 case '0':
117 *res = false;
118 return 0;
119 case 'o':
120 case 'O':
121 switch (s[1]) {
122 case 'n':
123 case 'N':
124 *res = true;
125 return 0;
126 case 'f':
127 case 'F':
128 *res = false;
129 return 0;
130 default:
131 break;
133 default:
134 break;
137 return -EINVAL;