2 #include <linux/kernel.h>
3 #include <linux/string.h>
6 #include "sane_ctype.h"
11 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
12 * and return its numeric value
14 s64
perf_atoll(const char *str
)
23 length
= strtoll(str
, &p
, 10);
34 /* two-letter suffices */
48 /* we want the cases to match */
50 if (strcmp(p
, "b") != 0)
53 if (strcmp(p
, "B") != 0)
63 * Helper function for splitting a string into an argv-like array.
64 * originally copied from lib/argv_split.c
66 static const char *skip_sep(const char *cp
)
68 while (*cp
&& isspace(*cp
))
74 static const char *skip_arg(const char *cp
)
76 while (*cp
&& !isspace(*cp
))
82 static int count_argc(const char *str
)
98 * argv_free - free an argv
99 * @argv - the argument vector to be freed
101 * Frees an argv and the strings it points to.
103 void argv_free(char **argv
)
106 for (p
= argv
; *p
; p
++) {
115 * argv_split - split a string at whitespace, returning an argv
116 * @str: the string to be split
117 * @argcp: returned argument count
119 * Returns an array of pointers to strings which are split out from
120 * @str. This is performed by strictly splitting on white-space; no
121 * quote processing is performed. Multiple whitespace characters are
122 * considered to be a single argument separator. The returned array
123 * is always NULL-terminated. Returns NULL on memory allocation
126 char **argv_split(const char *str
, int *argcp
)
128 int argc
= count_argc(str
);
129 char **argv
= calloc(argc
+ 1, sizeof(*argv
));
149 t
= strndup(p
, str
-p
);
165 /* Character class matching */
166 static bool __match_charclass(const char *pat
, char c
, const char **npat
)
168 bool complement
= false, ret
= true;
174 if (*pat
++ == c
) /* First character is special */
177 while (*pat
&& *pat
!= ']') { /* Matching */
178 if (*pat
== '-' && *(pat
+ 1) != ']') { /* Range */
179 if (*(pat
- 1) <= c
&& c
<= *(pat
+ 1))
181 if (*(pat
- 1) > *(pat
+ 1))
184 } else if (*pat
++ == c
)
192 while (*pat
&& *pat
!= ']') /* Searching closing */
197 return complement
? !ret
: ret
;
203 /* Glob/lazy pattern matching */
204 static bool __match_glob(const char *str
, const char *pat
, bool ignore_space
,
207 while (*str
&& *pat
&& *pat
!= '*') {
209 /* Ignore spaces for lazy matching */
219 if (*pat
== '?') { /* Matches any single character */
223 } else if (*pat
== '[') /* Character classes/Ranges */
224 if (__match_charclass(pat
+ 1, *str
, &pat
)) {
229 else if (*pat
== '\\') /* Escaped char match as normal char */
232 if (tolower(*str
) != tolower(*pat
))
234 } else if (*str
!= *pat
)
239 /* Check wild card */
243 if (!*pat
) /* Tail wild card matches all */
246 if (__match_glob(str
++, pat
, ignore_space
, case_ins
))
249 return !*str
&& !*pat
;
253 * strglobmatch - glob expression pattern matching
254 * @str: the target string to match
255 * @pat: the pattern string to match
257 * This returns true if the @str matches @pat. @pat can includes wildcards
258 * ('*','?') and character classes ([CHARS], complementation and ranges are
259 * also supported). Also, this supports escape character ('\') to use special
260 * characters as normal character.
262 * Note: if @pat syntax is broken, this always returns false.
264 bool strglobmatch(const char *str
, const char *pat
)
266 return __match_glob(str
, pat
, false, false);
269 bool strglobmatch_nocase(const char *str
, const char *pat
)
271 return __match_glob(str
, pat
, false, true);
275 * strlazymatch - matching pattern strings lazily with glob pattern
276 * @str: the target string to match
277 * @pat: the pattern string to match
279 * This is similar to strglobmatch, except this ignores spaces in
282 bool strlazymatch(const char *str
, const char *pat
)
284 return __match_glob(str
, pat
, true, false);
288 * strtailcmp - Compare the tail of two strings
289 * @s1: 1st string to be compared
290 * @s2: 2nd string to be compared
292 * Return 0 if whole of either string is same as another's tail part.
294 int strtailcmp(const char *s1
, const char *s2
)
298 while (--i1
>= 0 && --i2
>= 0) {
299 if (s1
[i1
] != s2
[i2
])
300 return s1
[i1
] - s2
[i2
];
306 * strxfrchar - Locate and replace character in @s
307 * @s: The string to be searched/changed.
308 * @from: Source character to be replaced.
309 * @to: Destination character.
311 * Return pointer to the changed string.
313 char *strxfrchar(char *s
, char from
, char to
)
317 while ((p
= strchr(p
, from
)) != NULL
)
324 * ltrim - Removes leading whitespace from @s.
325 * @s: The string to be stripped.
327 * Return pointer to the first non-whitespace character in @s.
338 * rtrim - Removes trailing whitespace from @s.
339 * @s: The string to be stripped.
341 * Note that the first trailing whitespace is replaced with a %NUL-terminator
342 * in the given string @s. Returns @s.
346 size_t size
= strlen(s
);
353 while (end
>= s
&& isspace(*end
))
360 char *asprintf_expr_inout_ints(const char *var
, bool in
, size_t nints
, int *ints
)
363 * FIXME: replace this with an expression using log10() when we
364 * find a suitable implementation, maybe the one in the dvb drivers...
366 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
368 size_t size
= nints
* 28 + 1; /* \0 */
369 size_t i
, printed
= 0;
370 char *expr
= malloc(size
);
373 const char *or_and
= "||", *eq_neq
= "==";
381 for (i
= 0; i
< nints
; ++i
) {
383 goto out_err_overflow
;
386 printed
+= scnprintf(e
+ printed
, size
- printed
, " %s ", or_and
);
387 printed
+= scnprintf(e
+ printed
, size
- printed
,
388 "%s %s %d", var
, eq_neq
, ints
[i
]);