1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
4 #include <linux/string.h>
7 #include <linux/ctype.h>
9 const char *graph_dotted_line
=
10 "---------------------------------------------------------------------"
11 "---------------------------------------------------------------------"
12 "---------------------------------------------------------------------";
14 "....................................................................."
15 "....................................................................."
16 ".....................................................................";
21 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
22 * and return its numeric value
24 s64
perf_atoll(const char *str
)
33 length
= strtoll(str
, &p
, 10);
44 /* two-letter suffices */
58 /* we want the cases to match */
60 if (strcmp(p
, "b") != 0)
63 if (strcmp(p
, "B") != 0)
72 /* Character class matching */
73 static bool __match_charclass(const char *pat
, char c
, const char **npat
)
75 bool complement
= false, ret
= true;
81 if (*pat
++ == c
) /* First character is special */
84 while (*pat
&& *pat
!= ']') { /* Matching */
85 if (*pat
== '-' && *(pat
+ 1) != ']') { /* Range */
86 if (*(pat
- 1) <= c
&& c
<= *(pat
+ 1))
88 if (*(pat
- 1) > *(pat
+ 1))
91 } else if (*pat
++ == c
)
99 while (*pat
&& *pat
!= ']') /* Searching closing */
104 return complement
? !ret
: ret
;
110 /* Glob/lazy pattern matching */
111 static bool __match_glob(const char *str
, const char *pat
, bool ignore_space
,
114 while (*str
&& *pat
&& *pat
!= '*') {
116 /* Ignore spaces for lazy matching */
126 if (*pat
== '?') { /* Matches any single character */
130 } else if (*pat
== '[') /* Character classes/Ranges */
131 if (__match_charclass(pat
+ 1, *str
, &pat
)) {
136 else if (*pat
== '\\') /* Escaped char match as normal char */
139 if (tolower(*str
) != tolower(*pat
))
141 } else if (*str
!= *pat
)
146 /* Check wild card */
150 if (!*pat
) /* Tail wild card matches all */
153 if (__match_glob(str
++, pat
, ignore_space
, case_ins
))
156 return !*str
&& !*pat
;
160 * strglobmatch - glob expression pattern matching
161 * @str: the target string to match
162 * @pat: the pattern string to match
164 * This returns true if the @str matches @pat. @pat can includes wildcards
165 * ('*','?') and character classes ([CHARS], complementation and ranges are
166 * also supported). Also, this supports escape character ('\') to use special
167 * characters as normal character.
169 * Note: if @pat syntax is broken, this always returns false.
171 bool strglobmatch(const char *str
, const char *pat
)
173 return __match_glob(str
, pat
, false, false);
176 bool strglobmatch_nocase(const char *str
, const char *pat
)
178 return __match_glob(str
, pat
, false, true);
182 * strlazymatch - matching pattern strings lazily with glob pattern
183 * @str: the target string to match
184 * @pat: the pattern string to match
186 * This is similar to strglobmatch, except this ignores spaces in
189 bool strlazymatch(const char *str
, const char *pat
)
191 return __match_glob(str
, pat
, true, false);
195 * strtailcmp - Compare the tail of two strings
196 * @s1: 1st string to be compared
197 * @s2: 2nd string to be compared
199 * Return 0 if whole of either string is same as another's tail part.
201 int strtailcmp(const char *s1
, const char *s2
)
205 while (--i1
>= 0 && --i2
>= 0) {
206 if (s1
[i1
] != s2
[i2
])
207 return s1
[i1
] - s2
[i2
];
212 char *asprintf_expr_inout_ints(const char *var
, bool in
, size_t nints
, int *ints
)
215 * FIXME: replace this with an expression using log10() when we
216 * find a suitable implementation, maybe the one in the dvb drivers...
218 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
220 size_t size
= nints
* 28 + 1; /* \0 */
221 size_t i
, printed
= 0;
222 char *expr
= malloc(size
);
225 const char *or_and
= "||", *eq_neq
= "==";
233 for (i
= 0; i
< nints
; ++i
) {
235 goto out_err_overflow
;
238 printed
+= scnprintf(e
+ printed
, size
- printed
, " %s ", or_and
);
239 printed
+= scnprintf(e
+ printed
, size
- printed
,
240 "%s %s %d", var
, eq_neq
, ints
[i
]);
251 /* Like strpbrk(), but not break if it is right after a backslash (escaped) */
252 char *strpbrk_esc(char *str
, const char *stopset
)
257 ptr
= strpbrk(str
, stopset
);
259 (ptr
== str
+ 1 && *(ptr
- 1) != '\\'))
262 } while (ptr
&& *(ptr
- 1) == '\\' && *(ptr
- 2) != '\\');
267 /* Like strdup, but do not copy a single backslash */
268 char *strdup_esc(const char *str
)
270 char *s
, *d
, *p
, *ret
= strdup(str
);
275 d
= strchr(ret
, '\\');
285 p
= strchr(s
+ 1, '\\');
287 memmove(d
, s
, p
- s
);
291 memmove(d
, s
, strlen(s
) + 1);