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 /* Glob expression pattern matching */
231 bool strglobmatch(const char *str
, const char *pat
)
233 while (*str
&& *pat
&& *pat
!= '*') {
238 if (*str
++ != *pat
++)
241 /* Check wild card */
245 if (!*pat
) /* Tail wild card matches all */
248 if (strglobmatch(str
++, pat
))
251 return !*str
&& !*pat
;