2 #include "linux/string.h"
7 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
8 * and return its numeric value
10 s64 perf_atoll(const char *str)
19 length = strtoll(str, &p, 10);
30 /* two-letter suffices */
44 /* we want the cases to match */
46 if (strcmp(p, "b") != 0)
49 if (strcmp(p, "B") != 0)
59 * Helper function for splitting a string into an argv-like array.
60 * originally copied from lib/argv_split.c
62 static const char *skip_sep(const char *cp)
64 while (*cp && isspace(*cp))
70 static const char *skip_arg(const char *cp)
72 while (*cp && !isspace(*cp))
78 static int count_argc(const char *str)
94 * argv_free - free an argv
95 * @argv - the argument vector to be freed
97 * Frees an argv and the strings it points to.
99 void argv_free(char **argv)
102 for (p = argv; *p; p++)
109 * argv_split - split a string at whitespace, returning an argv
110 * @str: the string to be split
111 * @argcp: returned argument count
113 * Returns an array of pointers to strings which are split out from
114 * @str. This is performed by strictly splitting on white-space; no
115 * quote processing is performed. Multiple whitespace characters are
116 * considered to be a single argument separator. The returned array
117 * is always NULL-terminated. Returns NULL on memory allocation
120 char **argv_split(const char *str, int *argcp)
122 int argc = count_argc(str);
123 char **argv = zalloc(sizeof(*argv) * (argc+1));
143 t = strndup(p, str-p);
159 /* Character class matching */
160 static bool __match_charclass(const char *pat, char c, const char **npat)
162 bool complement = false, ret = true;
168 if (*pat++ == c) /* First character is special */
171 while (*pat && *pat != ']') { /* Matching */
172 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
173 if (*(pat - 1) <= c && c <= *(pat + 1))
175 if (*(pat - 1) > *(pat + 1))
178 } else if (*pat++ == c)
186 while (*pat && *pat != ']') /* Searching closing */
191 return complement ? !ret : ret;
197 /* Glob/lazy pattern matching */
198 static bool __match_glob(const char *str, const char *pat, bool ignore_space)
200 while (*str && *pat && *pat != '*') {
202 /* Ignore spaces for lazy matching */
212 if (*pat == '?') { /* Matches any single character */
216 } else if (*pat == '[') /* Character classes/Ranges */
217 if (__match_charclass(pat + 1, *str, &pat)) {
222 else if (*pat == '\\') /* Escaped char match as normal char */
224 if (*str++ != *pat++)
227 /* Check wild card */
231 if (!*pat) /* Tail wild card matches all */
234 if (__match_glob(str++, pat, ignore_space))
237 return !*str && !*pat;
241 * strglobmatch - glob expression pattern matching
242 * @str: the target string to match
243 * @pat: the pattern string to match
245 * This returns true if the @str matches @pat. @pat can includes wildcards
246 * ('*','?') and character classes ([CHARS], complementation and ranges are
247 * also supported). Also, this supports escape character ('\') to use special
248 * characters as normal character.
250 * Note: if @pat syntax is broken, this always returns false.
252 bool strglobmatch(const char *str, const char *pat)
254 return __match_glob(str, pat, false);
258 * strlazymatch - matching pattern strings lazily with glob pattern
259 * @str: the target string to match
260 * @pat: the pattern string to match
262 * This is similar to strglobmatch, except this ignores spaces in
265 bool strlazymatch(const char *str, const char *pat)
267 return __match_glob(str, pat, true);
271 * strtailcmp - Compare the tail of two strings
272 * @s1: 1st string to be compared
273 * @s2: 2nd string to be compared
275 * Return 0 if whole of either string is same as another's tail part.
277 int strtailcmp(const char *s1, const char *s2)
281 while (--i1 >= 0 && --i2 >= 0) {
282 if (s1[i1] != s2[i2])
283 return s1[i1] - s2[i2];
289 * strxfrchar - Locate and replace character in @s
290 * @s: The string to be searched/changed.
291 * @from: Source character to be replaced.
292 * @to: Destination character.
294 * Return pointer to the changed string.
296 char *strxfrchar(char *s, char from, char to)
300 while ((p = strchr(p, from)) != NULL)
307 * ltrim - Removes leading whitespace from @s.
308 * @s: The string to be stripped.
310 * Return pointer to the first non-whitespace character in @s.
316 while (len && isspace(*s)) {
325 * rtrim - Removes trailing whitespace from @s.
326 * @s: The string to be stripped.
328 * Note that the first trailing whitespace is replaced with a %NUL-terminator
329 * in the given string @s. Returns @s.
333 size_t size = strlen(s);
340 while (end >= s && isspace(*end))
347 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
350 * FIXME: replace this with an expression using log10() when we
351 * find a suitable implementation, maybe the one in the dvb drivers...
353 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
355 size_t size = nints * 28 + 1; /* \0 */
356 size_t i, printed = 0;
357 char *expr = malloc(size);
360 const char *or_and = "||", *eq_neq = "==";
368 for (i = 0; i < nints; ++i) {
370 goto out_err_overflow;
373 printed += snprintf(e + printed, size - printed, " %s ", or_and);
374 printed += scnprintf(e + printed, size - printed,
375 "%s %s %d", var, eq_neq, ints[i]);