1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/string.h>
7 #include "sane_ctype.h"
12 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
13 * and return its numeric value
15 s64 perf_atoll(const char *str)
24 length = strtoll(str, &p, 10);
35 /* two-letter suffices */
49 /* we want the cases to match */
51 if (strcmp(p, "b") != 0)
54 if (strcmp(p, "B") != 0)
64 * Helper function for splitting a string into an argv-like array.
65 * originally copied from lib/argv_split.c
67 static const char *skip_sep(const char *cp)
69 while (*cp && isspace(*cp))
75 static const char *skip_arg(const char *cp)
77 while (*cp && !isspace(*cp))
83 static int count_argc(const char *str)
99 * argv_free - free an argv
100 * @argv - the argument vector to be freed
102 * Frees an argv and the strings it points to.
104 void argv_free(char **argv)
107 for (p = argv; *p; p++) {
116 * argv_split - split a string at whitespace, returning an argv
117 * @str: the string to be split
118 * @argcp: returned argument count
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
127 char **argv_split(const char *str, int *argcp)
129 int argc = count_argc(str);
130 char **argv = calloc(argc + 1, sizeof(*argv));
150 t = strndup(p, str-p);
166 /* Character class matching */
167 static bool __match_charclass(const char *pat, char c, const char **npat)
169 bool complement = false, ret = true;
175 if (*pat++ == c) /* First character is special */
178 while (*pat && *pat != ']') { /* Matching */
179 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
180 if (*(pat - 1) <= c && c <= *(pat + 1))
182 if (*(pat - 1) > *(pat + 1))
185 } else if (*pat++ == c)
193 while (*pat && *pat != ']') /* Searching closing */
198 return complement ? !ret : ret;
204 /* Glob/lazy pattern matching */
205 static bool __match_glob(const char *str, const char *pat, bool ignore_space,
208 while (*str && *pat && *pat != '*') {
210 /* Ignore spaces for lazy matching */
220 if (*pat == '?') { /* Matches any single character */
224 } else if (*pat == '[') /* Character classes/Ranges */
225 if (__match_charclass(pat + 1, *str, &pat)) {
230 else if (*pat == '\\') /* Escaped char match as normal char */
233 if (tolower(*str) != tolower(*pat))
235 } else if (*str != *pat)
240 /* Check wild card */
244 if (!*pat) /* Tail wild card matches all */
247 if (__match_glob(str++, pat, ignore_space, case_ins))
250 return !*str && !*pat;
254 * strglobmatch - glob expression pattern matching
255 * @str: the target string to match
256 * @pat: the pattern string to match
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.
263 * Note: if @pat syntax is broken, this always returns false.
265 bool strglobmatch(const char *str, const char *pat)
267 return __match_glob(str, pat, false, false);
270 bool strglobmatch_nocase(const char *str, const char *pat)
272 return __match_glob(str, pat, false, true);
276 * strlazymatch - matching pattern strings lazily with glob pattern
277 * @str: the target string to match
278 * @pat: the pattern string to match
280 * This is similar to strglobmatch, except this ignores spaces in
283 bool strlazymatch(const char *str, const char *pat)
285 return __match_glob(str, pat, true, false);
289 * strtailcmp - Compare the tail of two strings
290 * @s1: 1st string to be compared
291 * @s2: 2nd string to be compared
293 * Return 0 if whole of either string is same as another's tail part.
295 int strtailcmp(const char *s1, const char *s2)
299 while (--i1 >= 0 && --i2 >= 0) {
300 if (s1[i1] != s2[i2])
301 return s1[i1] - s2[i2];
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.
312 * Return pointer to the changed string.
314 char *strxfrchar(char *s, char from, char to)
318 while ((p = strchr(p, from)) != NULL)
325 * ltrim - Removes leading whitespace from @s.
326 * @s: The string to be stripped.
328 * Return pointer to the first non-whitespace character in @s.
339 * rtrim - Removes trailing whitespace from @s.
340 * @s: The string to be stripped.
342 * Note that the first trailing whitespace is replaced with a %NUL-terminator
343 * in the given string @s. Returns @s.
347 size_t size = strlen(s);
354 while (end >= s && isspace(*end))
361 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
364 * FIXME: replace this with an expression using log10() when we
365 * find a suitable implementation, maybe the one in the dvb drivers...
367 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
369 size_t size = nints * 28 + 1; /* \0 */
370 size_t i, printed = 0;
371 char *expr = malloc(size);
374 const char *or_and = "||", *eq_neq = "==";
382 for (i = 0; i < nints; ++i) {
384 goto out_err_overflow;
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]);
400 /* Like strpbrk(), but not break if it is right after a backslash (escaped) */
401 char *strpbrk_esc(char *str, const char *stopset)
406 ptr = strpbrk(str, stopset);
408 (ptr == str + 1 && *(ptr - 1) != '\\'))
411 } while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
416 /* Like strdup, but do not copy a single backslash */
417 char *strdup_esc(const char *str)
419 char *s, *d, *p, *ret = strdup(str);
424 d = strchr(ret, '\\');
434 p = strchr(s + 1, '\\');
436 memmove(d, s, p - s);
440 memmove(d, s, strlen(s) + 1);