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);
28 /* two-letter suffices */
42 /* we want the cases to match */
44 if (strcmp(p
, "b") != 0)
47 if (strcmp(p
, "B") != 0)
57 * Helper function for splitting a string into an argv-like array.
58 * originally copied from lib/argv_split.c
60 static const char *skip_sep(const char *cp
)
62 while (*cp
&& isspace(*cp
))
68 static const char *skip_arg(const char *cp
)
70 while (*cp
&& !isspace(*cp
))
76 static int count_argc(const char *str
)
92 * argv_free - free an argv
93 * @argv - the argument vector to be freed
95 * Frees an argv and the strings it points to.
97 void argv_free(char **argv
)
100 for (p
= argv
; *p
; p
++)
107 * argv_split - split a string at whitespace, returning an argv
108 * @str: the string to be split
109 * @argcp: returned argument count
111 * Returns an array of pointers to strings which are split out from
112 * @str. This is performed by strictly splitting on white-space; no
113 * quote processing is performed. Multiple whitespace characters are
114 * considered to be a single argument separator. The returned array
115 * is always NULL-terminated. Returns NULL on memory allocation
118 char **argv_split(const char *str
, int *argcp
)
120 int argc
= count_argc(str
);
121 char **argv
= zalloc(sizeof(*argv
) * (argc
+1));
141 t
= strndup(p
, str
-p
);
157 /* Character class matching */
158 static bool __match_charclass(const char *pat
, char c
, const char **npat
)
160 bool complement
= false, ret
= true;
166 if (*pat
++ == c
) /* First character is special */
169 while (*pat
&& *pat
!= ']') { /* Matching */
170 if (*pat
== '-' && *(pat
+ 1) != ']') { /* Range */
171 if (*(pat
- 1) <= c
&& c
<= *(pat
+ 1))
173 if (*(pat
- 1) > *(pat
+ 1))
176 } else if (*pat
++ == c
)
184 while (*pat
&& *pat
!= ']') /* Searching closing */
189 return complement
? !ret
: ret
;
195 /* Glob/lazy pattern matching */
196 static bool __match_glob(const char *str
, const char *pat
, bool ignore_space
)
198 while (*str
&& *pat
&& *pat
!= '*') {
200 /* Ignore spaces for lazy matching */
210 if (*pat
== '?') { /* Matches any single character */
214 } else if (*pat
== '[') /* Character classes/Ranges */
215 if (__match_charclass(pat
+ 1, *str
, &pat
)) {
220 else if (*pat
== '\\') /* Escaped char match as normal char */
222 if (*str
++ != *pat
++)
225 /* Check wild card */
229 if (!*pat
) /* Tail wild card matches all */
232 if (__match_glob(str
++, pat
, ignore_space
))
235 return !*str
&& !*pat
;
239 * strglobmatch - glob expression pattern matching
240 * @str: the target string to match
241 * @pat: the pattern string to match
243 * This returns true if the @str matches @pat. @pat can includes wildcards
244 * ('*','?') and character classes ([CHARS], complementation and ranges are
245 * also supported). Also, this supports escape character ('\') to use special
246 * characters as normal character.
248 * Note: if @pat syntax is broken, this always returns false.
250 bool strglobmatch(const char *str
, const char *pat
)
252 return __match_glob(str
, pat
, false);
256 * strlazymatch - matching pattern strings lazily with glob pattern
257 * @str: the target string to match
258 * @pat: the pattern string to match
260 * This is similar to strglobmatch, except this ignores spaces in
263 bool strlazymatch(const char *str
, const char *pat
)
265 return __match_glob(str
, pat
, true);
269 * strtailcmp - Compare the tail of two strings
270 * @s1: 1st string to be compared
271 * @s2: 2nd string to be compared
273 * Return 0 if whole of either string is same as another's tail part.
275 int strtailcmp(const char *s1
, const char *s2
)
279 while (--i1
>= 0 && --i2
>= 0) {
280 if (s1
[i1
] != s2
[i2
])
281 return s1
[i1
] - s2
[i2
];
287 * strxfrchar - Locate and replace character in @s
288 * @s: The string to be searched/changed.
289 * @from: Source character to be replaced.
290 * @to: Destination character.
292 * Return pointer to the changed string.
294 char *strxfrchar(char *s
, char from
, char to
)
298 while ((p
= strchr(p
, from
)) != NULL
)
305 * ltrim - Removes leading whitespace from @s.
306 * @s: The string to be stripped.
308 * Return pointer to the first non-whitespace character in @s.
314 while (len
&& isspace(*s
)) {
323 * rtrim - Removes trailing whitespace from @s.
324 * @s: The string to be stripped.
326 * Note that the first trailing whitespace is replaced with a %NUL-terminator
327 * in the given string @s. Returns @s.
331 size_t size
= strlen(s
);
338 while (end
>= s
&& isspace(*end
))
346 * memdup - duplicate region of memory
347 * @src: memory region to duplicate
348 * @len: memory region length
350 void *memdup(const void *src
, size_t len
)