1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/string.h>
7 #include <linux/ctype.h>
9 const char *graph_dotted_line =
10 "---------------------------------------------------------------------"
11 "---------------------------------------------------------------------"
12 "---------------------------------------------------------------------";
14 "....................................................................."
15 "....................................................................."
16 ".....................................................................";
21 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
22 * and return its numeric value
24 s64 perf_atoll(const char *str)
33 length = strtoll(str, &p, 10);
44 /* two-letter suffices */
58 /* we want the cases to match */
60 if (strcmp(p, "b") != 0)
63 if (strcmp(p, "B") != 0)
73 * Helper function for splitting a string into an argv-like array.
74 * originally copied from lib/argv_split.c
76 static const char *skip_sep(const char *cp)
78 while (*cp && isspace(*cp))
84 static const char *skip_arg(const char *cp)
86 while (*cp && !isspace(*cp))
92 static int count_argc(const char *str)
108 * argv_free - free an argv
109 * @argv - the argument vector to be freed
111 * Frees an argv and the strings it points to.
113 void argv_free(char **argv)
116 for (p = argv; *p; p++) {
125 * argv_split - split a string at whitespace, returning an argv
126 * @str: the string to be split
127 * @argcp: returned argument count
129 * Returns an array of pointers to strings which are split out from
130 * @str. This is performed by strictly splitting on white-space; no
131 * quote processing is performed. Multiple whitespace characters are
132 * considered to be a single argument separator. The returned array
133 * is always NULL-terminated. Returns NULL on memory allocation
136 char **argv_split(const char *str, int *argcp)
138 int argc = count_argc(str);
139 char **argv = calloc(argc + 1, sizeof(*argv));
159 t = strndup(p, str-p);
175 /* Character class matching */
176 static bool __match_charclass(const char *pat, char c, const char **npat)
178 bool complement = false, ret = true;
184 if (*pat++ == c) /* First character is special */
187 while (*pat && *pat != ']') { /* Matching */
188 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
189 if (*(pat - 1) <= c && c <= *(pat + 1))
191 if (*(pat - 1) > *(pat + 1))
194 } else if (*pat++ == c)
202 while (*pat && *pat != ']') /* Searching closing */
207 return complement ? !ret : ret;
213 /* Glob/lazy pattern matching */
214 static bool __match_glob(const char *str, const char *pat, bool ignore_space,
217 while (*str && *pat && *pat != '*') {
219 /* Ignore spaces for lazy matching */
229 if (*pat == '?') { /* Matches any single character */
233 } else if (*pat == '[') /* Character classes/Ranges */
234 if (__match_charclass(pat + 1, *str, &pat)) {
239 else if (*pat == '\\') /* Escaped char match as normal char */
242 if (tolower(*str) != tolower(*pat))
244 } else if (*str != *pat)
249 /* Check wild card */
253 if (!*pat) /* Tail wild card matches all */
256 if (__match_glob(str++, pat, ignore_space, case_ins))
259 return !*str && !*pat;
263 * strglobmatch - glob expression pattern matching
264 * @str: the target string to match
265 * @pat: the pattern string to match
267 * This returns true if the @str matches @pat. @pat can includes wildcards
268 * ('*','?') and character classes ([CHARS], complementation and ranges are
269 * also supported). Also, this supports escape character ('\') to use special
270 * characters as normal character.
272 * Note: if @pat syntax is broken, this always returns false.
274 bool strglobmatch(const char *str, const char *pat)
276 return __match_glob(str, pat, false, false);
279 bool strglobmatch_nocase(const char *str, const char *pat)
281 return __match_glob(str, pat, false, true);
285 * strlazymatch - matching pattern strings lazily with glob pattern
286 * @str: the target string to match
287 * @pat: the pattern string to match
289 * This is similar to strglobmatch, except this ignores spaces in
292 bool strlazymatch(const char *str, const char *pat)
294 return __match_glob(str, pat, true, false);
298 * strtailcmp - Compare the tail of two strings
299 * @s1: 1st string to be compared
300 * @s2: 2nd string to be compared
302 * Return 0 if whole of either string is same as another's tail part.
304 int strtailcmp(const char *s1, const char *s2)
308 while (--i1 >= 0 && --i2 >= 0) {
309 if (s1[i1] != s2[i2])
310 return s1[i1] - s2[i2];
316 * strxfrchar - Locate and replace character in @s
317 * @s: The string to be searched/changed.
318 * @from: Source character to be replaced.
319 * @to: Destination character.
321 * Return pointer to the changed string.
323 char *strxfrchar(char *s, char from, char to)
327 while ((p = strchr(p, from)) != NULL)
334 * ltrim - Removes leading whitespace from @s.
335 * @s: The string to be stripped.
337 * Return pointer to the first non-whitespace character in @s.
348 * rtrim - Removes trailing whitespace from @s.
349 * @s: The string to be stripped.
351 * Note that the first trailing whitespace is replaced with a %NUL-terminator
352 * in the given string @s. Returns @s.
356 size_t size = strlen(s);
363 while (end >= s && isspace(*end))
370 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
373 * FIXME: replace this with an expression using log10() when we
374 * find a suitable implementation, maybe the one in the dvb drivers...
376 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
378 size_t size = nints * 28 + 1; /* \0 */
379 size_t i, printed = 0;
380 char *expr = malloc(size);
383 const char *or_and = "||", *eq_neq = "==";
391 for (i = 0; i < nints; ++i) {
393 goto out_err_overflow;
396 printed += scnprintf(e + printed, size - printed, " %s ", or_and);
397 printed += scnprintf(e + printed, size - printed,
398 "%s %s %d", var, eq_neq, ints[i]);
409 /* Like strpbrk(), but not break if it is right after a backslash (escaped) */
410 char *strpbrk_esc(char *str, const char *stopset)
415 ptr = strpbrk(str, stopset);
417 (ptr == str + 1 && *(ptr - 1) != '\\'))
420 } while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
425 /* Like strdup, but do not copy a single backslash */
426 char *strdup_esc(const char *str)
428 char *s, *d, *p, *ret = strdup(str);
433 d = strchr(ret, '\\');
443 p = strchr(s + 1, '\\');
445 memmove(d, s, p - s);
449 memmove(d, s, strlen(s) + 1);