2 #include "linux/string.h"
4 #include "sane_ctype.h"
9 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
10 * and return its numeric value
12 s64 perf_atoll(const char *str)
21 length = strtoll(str, &p, 10);
32 /* two-letter suffices */
46 /* we want the cases to match */
48 if (strcmp(p, "b") != 0)
51 if (strcmp(p, "B") != 0)
61 * Helper function for splitting a string into an argv-like array.
62 * originally copied from lib/argv_split.c
64 static const char *skip_sep(const char *cp)
66 while (*cp && isspace(*cp))
72 static const char *skip_arg(const char *cp)
74 while (*cp && !isspace(*cp))
80 static int count_argc(const char *str)
96 * argv_free - free an argv
97 * @argv - the argument vector to be freed
99 * Frees an argv and the strings it points to.
101 void argv_free(char **argv)
104 for (p = argv; *p; p++)
111 * argv_split - split a string at whitespace, returning an argv
112 * @str: the string to be split
113 * @argcp: returned argument count
115 * Returns an array of pointers to strings which are split out from
116 * @str. This is performed by strictly splitting on white-space; no
117 * quote processing is performed. Multiple whitespace characters are
118 * considered to be a single argument separator. The returned array
119 * is always NULL-terminated. Returns NULL on memory allocation
122 char **argv_split(const char *str, int *argcp)
124 int argc = count_argc(str);
125 char **argv = zalloc(sizeof(*argv) * (argc+1));
145 t = strndup(p, str-p);
161 /* Character class matching */
162 static bool __match_charclass(const char *pat, char c, const char **npat)
164 bool complement = false, ret = true;
170 if (*pat++ == c) /* First character is special */
173 while (*pat && *pat != ']') { /* Matching */
174 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
175 if (*(pat - 1) <= c && c <= *(pat + 1))
177 if (*(pat - 1) > *(pat + 1))
180 } else if (*pat++ == c)
188 while (*pat && *pat != ']') /* Searching closing */
193 return complement ? !ret : ret;
199 /* Glob/lazy pattern matching */
200 static bool __match_glob(const char *str, const char *pat, bool ignore_space,
203 while (*str && *pat && *pat != '*') {
205 /* Ignore spaces for lazy matching */
215 if (*pat == '?') { /* Matches any single character */
219 } else if (*pat == '[') /* Character classes/Ranges */
220 if (__match_charclass(pat + 1, *str, &pat)) {
225 else if (*pat == '\\') /* Escaped char match as normal char */
228 if (tolower(*str) != tolower(*pat))
230 } else if (*str != *pat)
235 /* Check wild card */
239 if (!*pat) /* Tail wild card matches all */
242 if (__match_glob(str++, pat, ignore_space, case_ins))
245 return !*str && !*pat;
249 * strglobmatch - glob expression pattern matching
250 * @str: the target string to match
251 * @pat: the pattern string to match
253 * This returns true if the @str matches @pat. @pat can includes wildcards
254 * ('*','?') and character classes ([CHARS], complementation and ranges are
255 * also supported). Also, this supports escape character ('\') to use special
256 * characters as normal character.
258 * Note: if @pat syntax is broken, this always returns false.
260 bool strglobmatch(const char *str, const char *pat)
262 return __match_glob(str, pat, false, false);
265 bool strglobmatch_nocase(const char *str, const char *pat)
267 return __match_glob(str, pat, false, true);
271 * strlazymatch - matching pattern strings lazily with glob pattern
272 * @str: the target string to match
273 * @pat: the pattern string to match
275 * This is similar to strglobmatch, except this ignores spaces in
278 bool strlazymatch(const char *str, const char *pat)
280 return __match_glob(str, pat, true, false);
284 * strtailcmp - Compare the tail of two strings
285 * @s1: 1st string to be compared
286 * @s2: 2nd string to be compared
288 * Return 0 if whole of either string is same as another's tail part.
290 int strtailcmp(const char *s1, const char *s2)
294 while (--i1 >= 0 && --i2 >= 0) {
295 if (s1[i1] != s2[i2])
296 return s1[i1] - s2[i2];
302 * strxfrchar - Locate and replace character in @s
303 * @s: The string to be searched/changed.
304 * @from: Source character to be replaced.
305 * @to: Destination character.
307 * Return pointer to the changed string.
309 char *strxfrchar(char *s, char from, char to)
313 while ((p = strchr(p, from)) != NULL)
320 * ltrim - Removes leading whitespace from @s.
321 * @s: The string to be stripped.
323 * Return pointer to the first non-whitespace character in @s.
334 * rtrim - Removes trailing whitespace from @s.
335 * @s: The string to be stripped.
337 * Note that the first trailing whitespace is replaced with a %NUL-terminator
338 * in the given string @s. Returns @s.
342 size_t size = strlen(s);
349 while (end >= s && isspace(*end))
356 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
359 * FIXME: replace this with an expression using log10() when we
360 * find a suitable implementation, maybe the one in the dvb drivers...
362 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
364 size_t size = nints * 28 + 1; /* \0 */
365 size_t i, printed = 0;
366 char *expr = malloc(size);
369 const char *or_and = "||", *eq_neq = "==";
377 for (i = 0; i < nints; ++i) {
379 goto out_err_overflow;
382 printed += snprintf(e + printed, size - printed, " %s ", or_and);
383 printed += scnprintf(e + printed, size - printed,
384 "%s %s %d", var, eq_neq, ints[i]);