Loading...
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"
8
9int strncmp(const char *cs, const char *ct, size_t count)
10{
11 unsigned char c1, c2;
12
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--;
21 }
22 return 0;
23}
24
25char *skip_spaces(const char *str)
26{
27 while (isspace(*str))
28 ++str;
29 return (char *)str;
30}
31
32char *strim(char *s)
33{
34 size_t size;
35 char *end;
36
37 size = strlen(s);
38 if (!size)
39 return s;
40
41 end = s + size - 1;
42 while (end >= s && isspace(*end))
43 end--;
44 *(end + 1) = '\0';
45
46 return skip_spaces(s);
47}
48
49/* Works only for digits and letters, but small and fast */
50#define TOLOWER(x) ((x) | 0x20)
51
52static unsigned int simple_guess_base(const char *cp)
53{
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;
61 }
62}
63
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
69 */
70
71unsigned long long simple_strtoull(const char *cp, char **endp,
72 unsigned int base)
73{
74 unsigned long long result = 0;
75
76 if (!base)
77 base = simple_guess_base(cp);
78
79 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
80 cp += 2;
81
82 while (isxdigit(*cp)) {
83 unsigned int value;
84
85 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
86 if (value >= base)
87 break;
88 result = result * base + value;
89 cp++;
90 }
91 if (endp)
92 *endp = (char *)cp;
93
94 return result;
95}
96
97long simple_strtol(const char *cp, char **endp, unsigned int base)
98{
99 if (*cp == '-')
100 return -simple_strtoull(cp + 1, endp, base);
101
102 return simple_strtoull(cp, endp, base);
103}
104
105int kstrtobool(const char *s, bool *res)
106{
107 if (!s)
108 return -EINVAL;
109
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;
134 }
135 default:
136 break;
137 }
138
139 return -EINVAL;
140}
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#include "../lib/string.c"
7
8int strncmp(const char *cs, const char *ct, size_t count)
9{
10 unsigned char c1, c2;
11
12 while (count) {
13 c1 = *cs++;
14 c2 = *ct++;
15 if (c1 != c2)
16 return c1 < c2 ? -1 : 1;
17 if (!c1)
18 break;
19 count--;
20 }
21 return 0;
22}
23
24char *skip_spaces(const char *str)
25{
26 while (isspace(*str))
27 ++str;
28 return (char *)str;
29}
30
31char *strim(char *s)
32{
33 size_t size;
34 char *end;
35
36 size = strlen(s);
37 if (!size)
38 return s;
39
40 end = s + size - 1;
41 while (end >= s && isspace(*end))
42 end--;
43 *(end + 1) = '\0';
44
45 return skip_spaces(s);
46}
47
48/* Works only for digits and letters, but small and fast */
49#define TOLOWER(x) ((x) | 0x20)
50
51static unsigned int simple_guess_base(const char *cp)
52{
53 if (cp[0] == '0') {
54 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
55 return 16;
56 else
57 return 8;
58 } else {
59 return 10;
60 }
61}
62
63/**
64 * simple_strtoull - convert a string to an unsigned long long
65 * @cp: The start of the string
66 * @endp: A pointer to the end of the parsed string will be placed here
67 * @base: The number base to use
68 */
69
70unsigned long long simple_strtoull(const char *cp, char **endp,
71 unsigned int base)
72{
73 unsigned long long result = 0;
74
75 if (!base)
76 base = simple_guess_base(cp);
77
78 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
79 cp += 2;
80
81 while (isxdigit(*cp)) {
82 unsigned int value;
83
84 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
85 if (value >= base)
86 break;
87 result = result * base + value;
88 cp++;
89 }
90 if (endp)
91 *endp = (char *)cp;
92
93 return result;
94}
95
96long simple_strtol(const char *cp, char **endp, unsigned int base)
97{
98 if (*cp == '-')
99 return -simple_strtoull(cp + 1, endp, base);
100
101 return simple_strtoull(cp, endp, base);
102}
103
104int kstrtobool(const char *s, bool *res)
105{
106 if (!s)
107 return -EINVAL;
108
109 switch (s[0]) {
110 case 'y':
111 case 'Y':
112 case '1':
113 *res = true;
114 return 0;
115 case 'n':
116 case 'N':
117 case '0':
118 *res = false;
119 return 0;
120 case 'o':
121 case 'O':
122 switch (s[1]) {
123 case 'n':
124 case 'N':
125 *res = true;
126 return 0;
127 case 'f':
128 case 'F':
129 *res = false;
130 return 0;
131 default:
132 break;
133 }
134 default:
135 break;
136 }
137
138 return -EINVAL;
139}