Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include "string2.h"
3#include <linux/kernel.h>
4#include <linux/string.h>
5#include <stdlib.h>
6
7#include "sane_ctype.h"
8
9#define K 1024LL
10/*
11 * perf_atoll()
12 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
13 * and return its numeric value
14 */
15s64 perf_atoll(const char *str)
16{
17 s64 length;
18 char *p;
19 char c;
20
21 if (!isdigit(str[0]))
22 goto out_err;
23
24 length = strtoll(str, &p, 10);
25 switch (c = *p++) {
26 case 'b': case 'B':
27 if (*p)
28 goto out_err;
29
30 __fallthrough;
31 case '\0':
32 return length;
33 default:
34 goto out_err;
35 /* two-letter suffices */
36 case 'k': case 'K':
37 length <<= 10;
38 break;
39 case 'm': case 'M':
40 length <<= 20;
41 break;
42 case 'g': case 'G':
43 length <<= 30;
44 break;
45 case 't': case 'T':
46 length <<= 40;
47 break;
48 }
49 /* we want the cases to match */
50 if (islower(c)) {
51 if (strcmp(p, "b") != 0)
52 goto out_err;
53 } else {
54 if (strcmp(p, "B") != 0)
55 goto out_err;
56 }
57 return length;
58
59out_err:
60 return -1;
61}
62
63/*
64 * Helper function for splitting a string into an argv-like array.
65 * originally copied from lib/argv_split.c
66 */
67static const char *skip_sep(const char *cp)
68{
69 while (*cp && isspace(*cp))
70 cp++;
71
72 return cp;
73}
74
75static const char *skip_arg(const char *cp)
76{
77 while (*cp && !isspace(*cp))
78 cp++;
79
80 return cp;
81}
82
83static int count_argc(const char *str)
84{
85 int count = 0;
86
87 while (*str) {
88 str = skip_sep(str);
89 if (*str) {
90 count++;
91 str = skip_arg(str);
92 }
93 }
94
95 return count;
96}
97
98/**
99 * argv_free - free an argv
100 * @argv - the argument vector to be freed
101 *
102 * Frees an argv and the strings it points to.
103 */
104void argv_free(char **argv)
105{
106 char **p;
107 for (p = argv; *p; p++) {
108 free(*p);
109 *p = NULL;
110 }
111
112 free(argv);
113}
114
115/**
116 * argv_split - split a string at whitespace, returning an argv
117 * @str: the string to be split
118 * @argcp: returned argument count
119 *
120 * Returns an array of pointers to strings which are split out from
121 * @str. This is performed by strictly splitting on white-space; no
122 * quote processing is performed. Multiple whitespace characters are
123 * considered to be a single argument separator. The returned array
124 * is always NULL-terminated. Returns NULL on memory allocation
125 * failure.
126 */
127char **argv_split(const char *str, int *argcp)
128{
129 int argc = count_argc(str);
130 char **argv = calloc(argc + 1, sizeof(*argv));
131 char **argvp;
132
133 if (argv == NULL)
134 goto out;
135
136 if (argcp)
137 *argcp = argc;
138
139 argvp = argv;
140
141 while (*str) {
142 str = skip_sep(str);
143
144 if (*str) {
145 const char *p = str;
146 char *t;
147
148 str = skip_arg(str);
149
150 t = strndup(p, str-p);
151 if (t == NULL)
152 goto fail;
153 *argvp++ = t;
154 }
155 }
156 *argvp = NULL;
157
158out:
159 return argv;
160
161fail:
162 argv_free(argv);
163 return NULL;
164}
165
166/* Character class matching */
167static bool __match_charclass(const char *pat, char c, const char **npat)
168{
169 bool complement = false, ret = true;
170
171 if (*pat == '!') {
172 complement = true;
173 pat++;
174 }
175 if (*pat++ == c) /* First character is special */
176 goto end;
177
178 while (*pat && *pat != ']') { /* Matching */
179 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
180 if (*(pat - 1) <= c && c <= *(pat + 1))
181 goto end;
182 if (*(pat - 1) > *(pat + 1))
183 goto error;
184 pat += 2;
185 } else if (*pat++ == c)
186 goto end;
187 }
188 if (!*pat)
189 goto error;
190 ret = false;
191
192end:
193 while (*pat && *pat != ']') /* Searching closing */
194 pat++;
195 if (!*pat)
196 goto error;
197 *npat = pat + 1;
198 return complement ? !ret : ret;
199
200error:
201 return false;
202}
203
204/* Glob/lazy pattern matching */
205static bool __match_glob(const char *str, const char *pat, bool ignore_space,
206 bool case_ins)
207{
208 while (*str && *pat && *pat != '*') {
209 if (ignore_space) {
210 /* Ignore spaces for lazy matching */
211 if (isspace(*str)) {
212 str++;
213 continue;
214 }
215 if (isspace(*pat)) {
216 pat++;
217 continue;
218 }
219 }
220 if (*pat == '?') { /* Matches any single character */
221 str++;
222 pat++;
223 continue;
224 } else if (*pat == '[') /* Character classes/Ranges */
225 if (__match_charclass(pat + 1, *str, &pat)) {
226 str++;
227 continue;
228 } else
229 return false;
230 else if (*pat == '\\') /* Escaped char match as normal char */
231 pat++;
232 if (case_ins) {
233 if (tolower(*str) != tolower(*pat))
234 return false;
235 } else if (*str != *pat)
236 return false;
237 str++;
238 pat++;
239 }
240 /* Check wild card */
241 if (*pat == '*') {
242 while (*pat == '*')
243 pat++;
244 if (!*pat) /* Tail wild card matches all */
245 return true;
246 while (*str)
247 if (__match_glob(str++, pat, ignore_space, case_ins))
248 return true;
249 }
250 return !*str && !*pat;
251}
252
253/**
254 * strglobmatch - glob expression pattern matching
255 * @str: the target string to match
256 * @pat: the pattern string to match
257 *
258 * This returns true if the @str matches @pat. @pat can includes wildcards
259 * ('*','?') and character classes ([CHARS], complementation and ranges are
260 * also supported). Also, this supports escape character ('\') to use special
261 * characters as normal character.
262 *
263 * Note: if @pat syntax is broken, this always returns false.
264 */
265bool strglobmatch(const char *str, const char *pat)
266{
267 return __match_glob(str, pat, false, false);
268}
269
270bool strglobmatch_nocase(const char *str, const char *pat)
271{
272 return __match_glob(str, pat, false, true);
273}
274
275/**
276 * strlazymatch - matching pattern strings lazily with glob pattern
277 * @str: the target string to match
278 * @pat: the pattern string to match
279 *
280 * This is similar to strglobmatch, except this ignores spaces in
281 * the target string.
282 */
283bool strlazymatch(const char *str, const char *pat)
284{
285 return __match_glob(str, pat, true, false);
286}
287
288/**
289 * strtailcmp - Compare the tail of two strings
290 * @s1: 1st string to be compared
291 * @s2: 2nd string to be compared
292 *
293 * Return 0 if whole of either string is same as another's tail part.
294 */
295int strtailcmp(const char *s1, const char *s2)
296{
297 int i1 = strlen(s1);
298 int i2 = strlen(s2);
299 while (--i1 >= 0 && --i2 >= 0) {
300 if (s1[i1] != s2[i2])
301 return s1[i1] - s2[i2];
302 }
303 return 0;
304}
305
306/**
307 * strxfrchar - Locate and replace character in @s
308 * @s: The string to be searched/changed.
309 * @from: Source character to be replaced.
310 * @to: Destination character.
311 *
312 * Return pointer to the changed string.
313 */
314char *strxfrchar(char *s, char from, char to)
315{
316 char *p = s;
317
318 while ((p = strchr(p, from)) != NULL)
319 *p++ = to;
320
321 return s;
322}
323
324/**
325 * ltrim - Removes leading whitespace from @s.
326 * @s: The string to be stripped.
327 *
328 * Return pointer to the first non-whitespace character in @s.
329 */
330char *ltrim(char *s)
331{
332 while (isspace(*s))
333 s++;
334
335 return s;
336}
337
338/**
339 * rtrim - Removes trailing whitespace from @s.
340 * @s: The string to be stripped.
341 *
342 * Note that the first trailing whitespace is replaced with a %NUL-terminator
343 * in the given string @s. Returns @s.
344 */
345char *rtrim(char *s)
346{
347 size_t size = strlen(s);
348 char *end;
349
350 if (!size)
351 return s;
352
353 end = s + size - 1;
354 while (end >= s && isspace(*end))
355 end--;
356 *(end + 1) = '\0';
357
358 return s;
359}
360
361char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
362{
363 /*
364 * FIXME: replace this with an expression using log10() when we
365 * find a suitable implementation, maybe the one in the dvb drivers...
366 *
367 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
368 */
369 size_t size = nints * 28 + 1; /* \0 */
370 size_t i, printed = 0;
371 char *expr = malloc(size);
372
373 if (expr) {
374 const char *or_and = "||", *eq_neq = "==";
375 char *e = expr;
376
377 if (!in) {
378 or_and = "&&";
379 eq_neq = "!=";
380 }
381
382 for (i = 0; i < nints; ++i) {
383 if (printed == size)
384 goto out_err_overflow;
385
386 if (i > 0)
387 printed += scnprintf(e + printed, size - printed, " %s ", or_and);
388 printed += scnprintf(e + printed, size - printed,
389 "%s %s %d", var, eq_neq, ints[i]);
390 }
391 }
392
393 return expr;
394
395out_err_overflow:
396 free(expr);
397 return NULL;
398}
399
400/* Like strpbrk(), but not break if it is right after a backslash (escaped) */
401char *strpbrk_esc(char *str, const char *stopset)
402{
403 char *ptr;
404
405 do {
406 ptr = strpbrk(str, stopset);
407 if (ptr == str ||
408 (ptr == str + 1 && *(ptr - 1) != '\\'))
409 break;
410 str = ptr + 1;
411 } while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
412
413 return ptr;
414}
415
416/* Like strdup, but do not copy a single backslash */
417char *strdup_esc(const char *str)
418{
419 char *s, *d, *p, *ret = strdup(str);
420
421 if (!ret)
422 return NULL;
423
424 d = strchr(ret, '\\');
425 if (!d)
426 return ret;
427
428 s = d + 1;
429 do {
430 if (*s == '\0') {
431 *d = '\0';
432 break;
433 }
434 p = strchr(s + 1, '\\');
435 if (p) {
436 memmove(d, s, p - s);
437 d += p - s;
438 s = p + 1;
439 } else
440 memmove(d, s, strlen(s) + 1);
441 } while (p);
442
443 return ret;
444}
1#include "util.h"
2#include "string.h"
3
4#define K 1024LL
5/*
6 * perf_atoll()
7 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
8 * and return its numeric value
9 */
10s64 perf_atoll(const char *str)
11{
12 unsigned int i;
13 s64 length = -1, unit = 1;
14
15 if (!isdigit(str[0]))
16 goto out_err;
17
18 for (i = 1; i < strlen(str); i++) {
19 switch (str[i]) {
20 case 'B':
21 case 'b':
22 break;
23 case 'K':
24 if (str[i + 1] != 'B')
25 goto out_err;
26 else
27 goto kilo;
28 case 'k':
29 if (str[i + 1] != 'b')
30 goto out_err;
31kilo:
32 unit = K;
33 break;
34 case 'M':
35 if (str[i + 1] != 'B')
36 goto out_err;
37 else
38 goto mega;
39 case 'm':
40 if (str[i + 1] != 'b')
41 goto out_err;
42mega:
43 unit = K * K;
44 break;
45 case 'G':
46 if (str[i + 1] != 'B')
47 goto out_err;
48 else
49 goto giga;
50 case 'g':
51 if (str[i + 1] != 'b')
52 goto out_err;
53giga:
54 unit = K * K * K;
55 break;
56 case 'T':
57 if (str[i + 1] != 'B')
58 goto out_err;
59 else
60 goto tera;
61 case 't':
62 if (str[i + 1] != 'b')
63 goto out_err;
64tera:
65 unit = K * K * K * K;
66 break;
67 case '\0': /* only specified figures */
68 unit = 1;
69 break;
70 default:
71 if (!isdigit(str[i]))
72 goto out_err;
73 break;
74 }
75 }
76
77 length = atoll(str) * unit;
78 goto out;
79
80out_err:
81 length = -1;
82out:
83 return length;
84}
85
86/*
87 * Helper function for splitting a string into an argv-like array.
88 * originally copied from lib/argv_split.c
89 */
90static const char *skip_sep(const char *cp)
91{
92 while (*cp && isspace(*cp))
93 cp++;
94
95 return cp;
96}
97
98static const char *skip_arg(const char *cp)
99{
100 while (*cp && !isspace(*cp))
101 cp++;
102
103 return cp;
104}
105
106static int count_argc(const char *str)
107{
108 int count = 0;
109
110 while (*str) {
111 str = skip_sep(str);
112 if (*str) {
113 count++;
114 str = skip_arg(str);
115 }
116 }
117
118 return count;
119}
120
121/**
122 * argv_free - free an argv
123 * @argv - the argument vector to be freed
124 *
125 * Frees an argv and the strings it points to.
126 */
127void argv_free(char **argv)
128{
129 char **p;
130 for (p = argv; *p; p++)
131 free(*p);
132
133 free(argv);
134}
135
136/**
137 * argv_split - split a string at whitespace, returning an argv
138 * @str: the string to be split
139 * @argcp: returned argument count
140 *
141 * Returns an array of pointers to strings which are split out from
142 * @str. This is performed by strictly splitting on white-space; no
143 * quote processing is performed. Multiple whitespace characters are
144 * considered to be a single argument separator. The returned array
145 * is always NULL-terminated. Returns NULL on memory allocation
146 * failure.
147 */
148char **argv_split(const char *str, int *argcp)
149{
150 int argc = count_argc(str);
151 char **argv = zalloc(sizeof(*argv) * (argc+1));
152 char **argvp;
153
154 if (argv == NULL)
155 goto out;
156
157 if (argcp)
158 *argcp = argc;
159
160 argvp = argv;
161
162 while (*str) {
163 str = skip_sep(str);
164
165 if (*str) {
166 const char *p = str;
167 char *t;
168
169 str = skip_arg(str);
170
171 t = strndup(p, str-p);
172 if (t == NULL)
173 goto fail;
174 *argvp++ = t;
175 }
176 }
177 *argvp = NULL;
178
179out:
180 return argv;
181
182fail:
183 argv_free(argv);
184 return NULL;
185}
186
187/* Character class matching */
188static bool __match_charclass(const char *pat, char c, const char **npat)
189{
190 bool complement = false, ret = true;
191
192 if (*pat == '!') {
193 complement = true;
194 pat++;
195 }
196 if (*pat++ == c) /* First character is special */
197 goto end;
198
199 while (*pat && *pat != ']') { /* Matching */
200 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
201 if (*(pat - 1) <= c && c <= *(pat + 1))
202 goto end;
203 if (*(pat - 1) > *(pat + 1))
204 goto error;
205 pat += 2;
206 } else if (*pat++ == c)
207 goto end;
208 }
209 if (!*pat)
210 goto error;
211 ret = false;
212
213end:
214 while (*pat && *pat != ']') /* Searching closing */
215 pat++;
216 if (!*pat)
217 goto error;
218 *npat = pat + 1;
219 return complement ? !ret : ret;
220
221error:
222 return false;
223}
224
225/* Glob/lazy pattern matching */
226static bool __match_glob(const char *str, const char *pat, bool ignore_space)
227{
228 while (*str && *pat && *pat != '*') {
229 if (ignore_space) {
230 /* Ignore spaces for lazy matching */
231 if (isspace(*str)) {
232 str++;
233 continue;
234 }
235 if (isspace(*pat)) {
236 pat++;
237 continue;
238 }
239 }
240 if (*pat == '?') { /* Matches any single character */
241 str++;
242 pat++;
243 continue;
244 } else if (*pat == '[') /* Character classes/Ranges */
245 if (__match_charclass(pat + 1, *str, &pat)) {
246 str++;
247 continue;
248 } else
249 return false;
250 else if (*pat == '\\') /* Escaped char match as normal char */
251 pat++;
252 if (*str++ != *pat++)
253 return false;
254 }
255 /* Check wild card */
256 if (*pat == '*') {
257 while (*pat == '*')
258 pat++;
259 if (!*pat) /* Tail wild card matches all */
260 return true;
261 while (*str)
262 if (__match_glob(str++, pat, ignore_space))
263 return true;
264 }
265 return !*str && !*pat;
266}
267
268/**
269 * strglobmatch - glob expression pattern matching
270 * @str: the target string to match
271 * @pat: the pattern string to match
272 *
273 * This returns true if the @str matches @pat. @pat can includes wildcards
274 * ('*','?') and character classes ([CHARS], complementation and ranges are
275 * also supported). Also, this supports escape character ('\') to use special
276 * characters as normal character.
277 *
278 * Note: if @pat syntax is broken, this always returns false.
279 */
280bool strglobmatch(const char *str, const char *pat)
281{
282 return __match_glob(str, pat, false);
283}
284
285/**
286 * strlazymatch - matching pattern strings lazily with glob pattern
287 * @str: the target string to match
288 * @pat: the pattern string to match
289 *
290 * This is similar to strglobmatch, except this ignores spaces in
291 * the target string.
292 */
293bool strlazymatch(const char *str, const char *pat)
294{
295 return __match_glob(str, pat, true);
296}
297
298/**
299 * strtailcmp - Compare the tail of two strings
300 * @s1: 1st string to be compared
301 * @s2: 2nd string to be compared
302 *
303 * Return 0 if whole of either string is same as another's tail part.
304 */
305int strtailcmp(const char *s1, const char *s2)
306{
307 int i1 = strlen(s1);
308 int i2 = strlen(s2);
309 while (--i1 >= 0 && --i2 >= 0) {
310 if (s1[i1] != s2[i2])
311 return s1[i1] - s2[i2];
312 }
313 return 0;
314}
315