4 static int hex(char ch)
6 if ((ch >= '0') && (ch <= '9'))
8 if ((ch >= 'a') && (ch <= 'f'))
10 if ((ch >= 'A') && (ch <= 'F'))
16 * While we find nice hex chars, build a long_val.
17 * Return number of chars processed.
19 int hex2u64(const char *ptr, u64 *long_val)
25 const int hex_val = hex(*p);
30 *long_val = (*long_val << 4) | hex_val;
37 char *strxfrchar(char *s, char from, char to)
41 while ((p = strchr(p, from)) != NULL)
50 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
51 * and return its numeric value
53 s64 perf_atoll(const char *str)
56 s64 length = -1, unit = 1;
61 for (i = 1; i < strlen(str); i++) {
67 if (str[i + 1] != 'B')
72 if (str[i + 1] != 'b')
78 if (str[i + 1] != 'B')
83 if (str[i + 1] != 'b')
89 if (str[i + 1] != 'B')
94 if (str[i + 1] != 'b')
100 if (str[i + 1] != 'B')
105 if (str[i + 1] != 'b')
108 unit = K * K * K * K;
110 case '\0': /* only specified figures */
114 if (!isdigit(str[i]))
120 length = atoll(str) * unit;
130 * Helper function for splitting a string into an argv-like array.
131 * originaly copied from lib/argv_split.c
133 static const char *skip_sep(const char *cp)
135 while (*cp && isspace(*cp))
141 static const char *skip_arg(const char *cp)
143 while (*cp && !isspace(*cp))
149 static int count_argc(const char *str)
165 * argv_free - free an argv
166 * @argv - the argument vector to be freed
168 * Frees an argv and the strings it points to.
170 void argv_free(char **argv)
173 for (p = argv; *p; p++)
180 * argv_split - split a string at whitespace, returning an argv
181 * @str: the string to be split
182 * @argcp: returned argument count
184 * Returns an array of pointers to strings which are split out from
185 * @str. This is performed by strictly splitting on white-space; no
186 * quote processing is performed. Multiple whitespace characters are
187 * considered to be a single argument separator. The returned array
188 * is always NULL-terminated. Returns NULL on memory allocation
191 char **argv_split(const char *str, int *argcp)
193 int argc = count_argc(str);
194 char **argv = zalloc(sizeof(*argv) * (argc+1));
214 t = strndup(p, str-p);
230 /* Character class matching */
231 static bool __match_charclass(const char *pat, char c, const char **npat)
233 bool complement = false, ret = true;
239 if (*pat++ == c) /* First character is special */
242 while (*pat && *pat != ']') { /* Matching */
243 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
244 if (*(pat - 1) <= c && c <= *(pat + 1))
246 if (*(pat - 1) > *(pat + 1))
249 } else if (*pat++ == c)
257 while (*pat && *pat != ']') /* Searching closing */
262 return complement ? !ret : ret;
269 * strglobmatch - glob expression pattern matching
270 * @str: the target string to match
271 * @pat: the pattern string to match
273 * This returns true if the @str matches @pat. @pat can includes wildcards
274 * ('*','?') and character classes ([CHARS], complementation and ranges are
275 * also supported). Also, this supports escape character ('\') to use special
276 * characters as normal character.
278 * Note: if @pat syntax is broken, this always returns false.
280 bool strglobmatch(const char *str, const char *pat)
282 while (*str && *pat && *pat != '*') {
283 if (*pat == '?') { /* Matches any single character */
287 } else if (*pat == '[') /* Character classes/Ranges */
288 if (__match_charclass(pat + 1, *str, &pat)) {
293 else if (*pat == '\\') /* Escaped char match as normal char */
295 if (*str++ != *pat++)
298 /* Check wild card */
302 if (!*pat) /* Tail wild card matches all */
305 if (strglobmatch(str++, pat))
308 return !*str && !*pat;