pty: Repair TIOCGPTPEER
[cris-mirror.git] / tools / perf / util / string.c
blobcca53b693a48d852450c00bf7ef6f90285d7ce4d
1 #include "string2.h"
2 #include <linux/kernel.h>
3 #include <linux/string.h>
4 #include <stdlib.h>
6 #include "sane_ctype.h"
8 #define K 1024LL
9 /*
10 * perf_atoll()
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)
16 s64 length;
17 char *p;
18 char c;
20 if (!isdigit(str[0]))
21 goto out_err;
23 length = strtoll(str, &p, 10);
24 switch (c = *p++) {
25 case 'b': case 'B':
26 if (*p)
27 goto out_err;
29 __fallthrough;
30 case '\0':
31 return length;
32 default:
33 goto out_err;
34 /* two-letter suffices */
35 case 'k': case 'K':
36 length <<= 10;
37 break;
38 case 'm': case 'M':
39 length <<= 20;
40 break;
41 case 'g': case 'G':
42 length <<= 30;
43 break;
44 case 't': case 'T':
45 length <<= 40;
46 break;
48 /* we want the cases to match */
49 if (islower(c)) {
50 if (strcmp(p, "b") != 0)
51 goto out_err;
52 } else {
53 if (strcmp(p, "B") != 0)
54 goto out_err;
56 return length;
58 out_err:
59 return -1;
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))
69 cp++;
71 return cp;
74 static const char *skip_arg(const char *cp)
76 while (*cp && !isspace(*cp))
77 cp++;
79 return cp;
82 static int count_argc(const char *str)
84 int count = 0;
86 while (*str) {
87 str = skip_sep(str);
88 if (*str) {
89 count++;
90 str = skip_arg(str);
94 return count;
97 /**
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)
105 char **p;
106 for (p = argv; *p; p++) {
107 free(*p);
108 *p = NULL;
111 free(argv);
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
124 * failure.
126 char **argv_split(const char *str, int *argcp)
128 int argc = count_argc(str);
129 char **argv = calloc(argc + 1, sizeof(*argv));
130 char **argvp;
132 if (argv == NULL)
133 goto out;
135 if (argcp)
136 *argcp = argc;
138 argvp = argv;
140 while (*str) {
141 str = skip_sep(str);
143 if (*str) {
144 const char *p = str;
145 char *t;
147 str = skip_arg(str);
149 t = strndup(p, str-p);
150 if (t == NULL)
151 goto fail;
152 *argvp++ = t;
155 *argvp = NULL;
157 out:
158 return argv;
160 fail:
161 argv_free(argv);
162 return NULL;
165 /* Character class matching */
166 static bool __match_charclass(const char *pat, char c, const char **npat)
168 bool complement = false, ret = true;
170 if (*pat == '!') {
171 complement = true;
172 pat++;
174 if (*pat++ == c) /* First character is special */
175 goto end;
177 while (*pat && *pat != ']') { /* Matching */
178 if (*pat == '-' && *(pat + 1) != ']') { /* Range */
179 if (*(pat - 1) <= c && c <= *(pat + 1))
180 goto end;
181 if (*(pat - 1) > *(pat + 1))
182 goto error;
183 pat += 2;
184 } else if (*pat++ == c)
185 goto end;
187 if (!*pat)
188 goto error;
189 ret = false;
191 end:
192 while (*pat && *pat != ']') /* Searching closing */
193 pat++;
194 if (!*pat)
195 goto error;
196 *npat = pat + 1;
197 return complement ? !ret : ret;
199 error:
200 return false;
203 /* Glob/lazy pattern matching */
204 static bool __match_glob(const char *str, const char *pat, bool ignore_space,
205 bool case_ins)
207 while (*str && *pat && *pat != '*') {
208 if (ignore_space) {
209 /* Ignore spaces for lazy matching */
210 if (isspace(*str)) {
211 str++;
212 continue;
214 if (isspace(*pat)) {
215 pat++;
216 continue;
219 if (*pat == '?') { /* Matches any single character */
220 str++;
221 pat++;
222 continue;
223 } else if (*pat == '[') /* Character classes/Ranges */
224 if (__match_charclass(pat + 1, *str, &pat)) {
225 str++;
226 continue;
227 } else
228 return false;
229 else if (*pat == '\\') /* Escaped char match as normal char */
230 pat++;
231 if (case_ins) {
232 if (tolower(*str) != tolower(*pat))
233 return false;
234 } else if (*str != *pat)
235 return false;
236 str++;
237 pat++;
239 /* Check wild card */
240 if (*pat == '*') {
241 while (*pat == '*')
242 pat++;
243 if (!*pat) /* Tail wild card matches all */
244 return true;
245 while (*str)
246 if (__match_glob(str++, pat, ignore_space, case_ins))
247 return true;
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
280 * the target string.
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)
296 int i1 = strlen(s1);
297 int i2 = strlen(s2);
298 while (--i1 >= 0 && --i2 >= 0) {
299 if (s1[i1] != s2[i2])
300 return s1[i1] - s2[i2];
302 return 0;
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)
315 char *p = s;
317 while ((p = strchr(p, from)) != NULL)
318 *p++ = to;
320 return s;
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.
329 char *ltrim(char *s)
331 while (isspace(*s))
332 s++;
334 return 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.
344 char *rtrim(char *s)
346 size_t size = strlen(s);
347 char *end;
349 if (!size)
350 return s;
352 end = s + size - 1;
353 while (end >= s && isspace(*end))
354 end--;
355 *(end + 1) = '\0';
357 return s;
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);
372 if (expr) {
373 const char *or_and = "||", *eq_neq = "==";
374 char *e = expr;
376 if (!in) {
377 or_and = "&&";
378 eq_neq = "!=";
381 for (i = 0; i < nints; ++i) {
382 if (printed == size)
383 goto out_err_overflow;
385 if (i > 0)
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]);
392 return expr;
394 out_err_overflow:
395 free(expr);
396 return NULL;