2 #include <linux/compiler.h>
3 #include "linux/string.h"
8 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
9 * and return its numeric value
11 s64 perf_atoll(const char *str)
20 length = strtoll(str, &p, 10);
31 /* two-letter suffices */
45 /* we want the cases to match */
47 if (strcmp(p, "b") != 0)
50 if (strcmp(p, "B") != 0)
60 * Helper function for splitting a string into an argv-like array.
61 * originally copied from lib/argv_split.c
63 static const char *skip_sep(const char *cp)
65 while (*cp && isspace(*cp))
71 static const char *skip_arg(const char *cp)
73 while (*cp && !isspace(*cp))
79 static int count_argc(const char *str)
95 * argv_free - free an argv
96 * @argv - the argument vector to be freed
98 * Frees an argv and the strings it points to.
100 void argv_free(char **argv)
103 for (p = argv; *p; p++)
110 * argv_split - split a string at whitespace, returning an argv
111 * @str: the string to be split
112 * @argcp: returned argument count
114 * Returns an array of pointers to strings which are split out from
115 * @str. This is performed by strictly splitting on white-space; no
116 * quote processing is performed. Multiple whitespace characters are
117 * considered to be a single argument separator. The returned array
118 * is always NULL-terminated. Returns NULL on memory allocation
121 char **argv_split(const char *str, int *argcp)
123 int argc = count_argc(str);
124 char **argv = zalloc(sizeof(*argv) * (argc+1));
144 t = strndup(p, str-p);
160 /* Character class matching */
161 static bool __match_charclass(const char *pat, char c, const char **npat)
163 bool complement = false, ret = true;
169 if (*pat++ == c) /* First character is special */
172 while (*pat && *pat != ']') { /* Matching */
173 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
174 if (*(pat - 1) <= c && c <= *(pat + 1))
176 if (*(pat - 1) > *(pat + 1))
179 } else if (*pat++ == c)
187 while (*pat && *pat != ']') /* Searching closing */
192 return complement ? !ret : ret;
198 /* Glob/lazy pattern matching */
199 static bool __match_glob(const char *str, const char *pat, bool ignore_space)
201 while (*str && *pat && *pat != '*') {
203 /* Ignore spaces for lazy matching */
213 if (*pat == '?') { /* Matches any single character */
217 } else if (*pat == '[') /* Character classes/Ranges */
218 if (__match_charclass(pat + 1, *str, &pat)) {
223 else if (*pat == '\\') /* Escaped char match as normal char */
225 if (*str++ != *pat++)
228 /* Check wild card */
232 if (!*pat) /* Tail wild card matches all */
235 if (__match_glob(str++, pat, ignore_space))
238 return !*str && !*pat;
242 * strglobmatch - glob expression pattern matching
243 * @str: the target string to match
244 * @pat: the pattern string to match
246 * This returns true if the @str matches @pat. @pat can includes wildcards
247 * ('*','?') and character classes ([CHARS], complementation and ranges are
248 * also supported). Also, this supports escape character ('\') to use special
249 * characters as normal character.
251 * Note: if @pat syntax is broken, this always returns false.
253 bool strglobmatch(const char *str, const char *pat)
255 return __match_glob(str, pat, false);
259 * strlazymatch - matching pattern strings lazily with glob pattern
260 * @str: the target string to match
261 * @pat: the pattern string to match
263 * This is similar to strglobmatch, except this ignores spaces in
266 bool strlazymatch(const char *str, const char *pat)
268 return __match_glob(str, pat, true);
272 * strtailcmp - Compare the tail of two strings
273 * @s1: 1st string to be compared
274 * @s2: 2nd string to be compared
276 * Return 0 if whole of either string is same as another's tail part.
278 int strtailcmp(const char *s1, const char *s2)
282 while (--i1 >= 0 && --i2 >= 0) {
283 if (s1[i1] != s2[i2])
284 return s1[i1] - s2[i2];
290 * strxfrchar - Locate and replace character in @s
291 * @s: The string to be searched/changed.
292 * @from: Source character to be replaced.
293 * @to: Destination character.
295 * Return pointer to the changed string.
297 char *strxfrchar(char *s, char from, char to)
301 while ((p = strchr(p, from)) != NULL)
308 * ltrim - Removes leading whitespace from @s.
309 * @s: The string to be stripped.
311 * Return pointer to the first non-whitespace character in @s.
317 while (len && isspace(*s)) {
326 * rtrim - Removes trailing whitespace from @s.
327 * @s: The string to be stripped.
329 * Note that the first trailing whitespace is replaced with a %NUL-terminator
330 * in the given string @s. Returns @s.
334 size_t size = strlen(s);
341 while (end >= s && isspace(*end))
349 * memdup - duplicate region of memory
350 * @src: memory region to duplicate
351 * @len: memory region length
353 void *memdup(const void *src, size_t len)
364 char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
367 * FIXME: replace this with an expression using log10() when we
368 * find a suitable implementation, maybe the one in the dvb drivers...
370 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
372 size_t size = nints * 28 + 1; /* \0 */
373 size_t i, printed = 0;
374 char *expr = malloc(size);
377 const char *or_and = "||", *eq_neq = "==";
385 for (i = 0; i < nints; ++i) {
387 goto out_err_overflow;
390 printed += snprintf(e + printed, size - printed, " %s ", or_and);
391 printed += scnprintf(e + printed, size - printed,
392 "%s %s %d", var, eq_neq, ints[i]);