Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2#include "string2.h"
  3#include <linux/kernel.h>
  4#include <linux/string.h>
  5#include <stdlib.h>
  6
  7#include <linux/ctype.h>
  8
  9const char *graph_dotted_line =
 10	"---------------------------------------------------------------------"
 11	"---------------------------------------------------------------------"
 12	"---------------------------------------------------------------------";
 13const char *dots =
 14	"....................................................................."
 15	"....................................................................."
 16	".....................................................................";
 17
 
 18/*
 19 * perf_atoll()
 20 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
 21 * and return its numeric value
 22 */
 23s64 perf_atoll(const char *str)
 24{
 25	s64 length;
 26	char *p;
 27	char c;
 28
 29	if (!isdigit(str[0]))
 30		goto out_err;
 31
 32	length = strtoll(str, &p, 10);
 33	switch (c = *p++) {
 34		case 'b': case 'B':
 35			if (*p)
 
 
 
 36				goto out_err;
 37
 38			__fallthrough;
 39		case '\0':
 40			return length;
 41		default:
 42			goto out_err;
 43		/* two-letter suffices */
 44		case 'k': case 'K':
 45			length <<= 10;
 
 
 
 
 
 
 
 
 
 46			break;
 47		case 'm': case 'M':
 48			length <<= 20;
 
 
 
 
 
 
 
 
 49			break;
 50		case 'g': case 'G':
 51			length <<= 30;
 
 
 
 
 
 
 
 
 52			break;
 53		case 't': case 'T':
 54			length <<= 40;
 55			break;
 
 
 
 
 
 56	}
 57	/* we want the cases to match */
 58	if (islower(c)) {
 59		if (strcmp(p, "b") != 0)
 60			goto out_err;
 61	} else {
 62		if (strcmp(p, "B") != 0)
 63			goto out_err;
 64	}
 65	return length;
 66
 67out_err:
 68	return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 69}
 70
 71/* Character class matching */
 72static bool __match_charclass(const char *pat, char c, const char **npat)
 73{
 74	bool complement = false, ret = true;
 75
 76	if (*pat == '!') {
 77		complement = true;
 78		pat++;
 79	}
 80	if (*pat++ == c)	/* First character is special */
 81		goto end;
 82
 83	while (*pat && *pat != ']') {	/* Matching */
 84		if (*pat == '-' && *(pat + 1) != ']') {	/* Range */
 85			if (*(pat - 1) <= c && c <= *(pat + 1))
 86				goto end;
 87			if (*(pat - 1) > *(pat + 1))
 88				goto error;
 89			pat += 2;
 90		} else if (*pat++ == c)
 91			goto end;
 92	}
 93	if (!*pat)
 94		goto error;
 95	ret = false;
 96
 97end:
 98	while (*pat && *pat != ']')	/* Searching closing */
 99		pat++;
100	if (!*pat)
101		goto error;
102	*npat = pat + 1;
103	return complement ? !ret : ret;
104
105error:
106	return false;
107}
108
109/* Glob/lazy pattern matching */
110static bool __match_glob(const char *str, const char *pat, bool ignore_space,
111			bool case_ins)
112{
113	while (*str && *pat && *pat != '*') {
114		if (ignore_space) {
115			/* Ignore spaces for lazy matching */
116			if (isspace(*str)) {
117				str++;
118				continue;
119			}
120			if (isspace(*pat)) {
121				pat++;
122				continue;
123			}
124		}
125		if (*pat == '?') {	/* Matches any single character */
126			str++;
127			pat++;
128			continue;
129		} else if (*pat == '[')	/* Character classes/Ranges */
130			if (__match_charclass(pat + 1, *str, &pat)) {
131				str++;
132				continue;
133			} else
134				return false;
135		else if (*pat == '\\') /* Escaped char match as normal char */
136			pat++;
137		if (case_ins) {
138			if (tolower(*str) != tolower(*pat))
139				return false;
140		} else if (*str != *pat)
141			return false;
142		str++;
143		pat++;
144	}
145	/* Check wild card */
146	if (*pat == '*') {
147		while (*pat == '*')
148			pat++;
149		if (!*pat)	/* Tail wild card matches all */
150			return true;
151		while (*str)
152			if (__match_glob(str++, pat, ignore_space, case_ins))
153				return true;
154	}
155	return !*str && !*pat;
156}
157
158/**
159 * strglobmatch - glob expression pattern matching
160 * @str: the target string to match
161 * @pat: the pattern string to match
162 *
163 * This returns true if the @str matches @pat. @pat can includes wildcards
164 * ('*','?') and character classes ([CHARS], complementation and ranges are
165 * also supported). Also, this supports escape character ('\') to use special
166 * characters as normal character.
167 *
168 * Note: if @pat syntax is broken, this always returns false.
169 */
170bool strglobmatch(const char *str, const char *pat)
171{
172	return __match_glob(str, pat, false, false);
173}
174
175bool strglobmatch_nocase(const char *str, const char *pat)
176{
177	return __match_glob(str, pat, false, true);
178}
179
180/**
181 * strlazymatch - matching pattern strings lazily with glob pattern
182 * @str: the target string to match
183 * @pat: the pattern string to match
184 *
185 * This is similar to strglobmatch, except this ignores spaces in
186 * the target string.
187 */
188bool strlazymatch(const char *str, const char *pat)
189{
190	return __match_glob(str, pat, true, false);
191}
192
193/**
194 * strtailcmp - Compare the tail of two strings
195 * @s1: 1st string to be compared
196 * @s2: 2nd string to be compared
197 *
198 * Return 0 if whole of either string is same as another's tail part.
199 */
200int strtailcmp(const char *s1, const char *s2)
201{
202	int i1 = strlen(s1);
203	int i2 = strlen(s2);
204	while (--i1 >= 0 && --i2 >= 0) {
205		if (s1[i1] != s2[i2])
206			return s1[i1] - s2[i2];
207	}
208	return 0;
209}
210
211char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
 
 
 
 
 
 
 
 
212{
213	/*
214	 * FIXME: replace this with an expression using log10() when we
215	 * find a suitable implementation, maybe the one in the dvb drivers...
216	 *
217	 * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
218	 */
219	size_t size = nints * 28 + 1; /* \0 */
220	size_t i, printed = 0;
221	char *expr = malloc(size);
222
223	if (expr) {
224		const char *or_and = "||", *eq_neq = "==";
225		char *e = expr;
226
227		if (!in) {
228			or_and = "&&";
229			eq_neq = "!=";
230		}
231
232		for (i = 0; i < nints; ++i) {
233			if (printed == size)
234				goto out_err_overflow;
235
236			if (i > 0)
237				printed += scnprintf(e + printed, size - printed, " %s ", or_and);
238			printed += scnprintf(e + printed, size - printed,
239					     "%s %s %d", var, eq_neq, ints[i]);
240		}
241	}
242
243	return expr;
 
244
245out_err_overflow:
246	free(expr);
247	return NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
248}
249
250/* Like strpbrk(), but not break if it is right after a backslash (escaped) */
251char *strpbrk_esc(char *str, const char *stopset)
 
 
 
 
 
 
252{
253	char *ptr;
 
254
255	do {
256		ptr = strpbrk(str, stopset);
257		if (ptr == str ||
258		    (ptr == str + 1 && *(ptr - 1) != '\\'))
259			break;
260		str = ptr + 1;
261	} while (ptr && *(ptr - 1) == '\\' && *(ptr - 2) != '\\');
262
263	return ptr;
264}
265
266/* Like strdup, but do not copy a single backslash */
267char *strdup_esc(const char *str)
 
 
 
 
268{
269	char *s, *d, *p, *ret = strdup(str);
270
271	if (!ret)
272		return NULL;
 
273
274	d = strchr(ret, '\\');
275	if (!d)
276		return ret;
277
278	s = d + 1;
279	do {
280		if (*s == '\0') {
281			*d = '\0';
282			break;
283		}
284		p = strchr(s + 1, '\\');
285		if (p) {
286			memmove(d, s, p - s);
287			d += p - s;
288			s = p + 1;
289		} else
290			memmove(d, s, strlen(s) + 1);
291	} while (p);
292
293	return ret;
294}
295
296unsigned int hex(char c)
297{
298	if (c >= '0' && c <= '9')
299		return c - '0';
300	if (c >= 'a' && c <= 'f')
301		return c - 'a' + 10;
302	return c - 'A' + 10;
303}
v3.15
  1#include "util.h"
  2#include "linux/string.h"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  3
  4#define K 1024LL
  5/*
  6 * perf_atoll()
  7 * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
  8 * and return its numeric value
  9 */
 10s64 perf_atoll(const char *str)
 11{
 12	unsigned int i;
 13	s64 length = -1, unit = 1;
 
 14
 15	if (!isdigit(str[0]))
 16		goto out_err;
 17
 18	for (i = 1; i < strlen(str); i++) {
 19		switch (str[i]) {
 20		case 'B':
 21		case 'b':
 22			break;
 23		case 'K':
 24			if (str[i + 1] != 'B')
 25				goto out_err;
 26			else
 27				goto kilo;
 28		case 'k':
 29			if (str[i + 1] != 'b')
 30				goto out_err;
 31kilo:
 32			unit = K;
 33			break;
 34		case 'M':
 35			if (str[i + 1] != 'B')
 36				goto out_err;
 37			else
 38				goto mega;
 39		case 'm':
 40			if (str[i + 1] != 'b')
 41				goto out_err;
 42mega:
 43			unit = K * K;
 44			break;
 45		case 'G':
 46			if (str[i + 1] != 'B')
 47				goto out_err;
 48			else
 49				goto giga;
 50		case 'g':
 51			if (str[i + 1] != 'b')
 52				goto out_err;
 53giga:
 54			unit = K * K * K;
 55			break;
 56		case 'T':
 57			if (str[i + 1] != 'B')
 58				goto out_err;
 59			else
 60				goto tera;
 61		case 't':
 62			if (str[i + 1] != 'b')
 63				goto out_err;
 64tera:
 65			unit = K * K * K * K;
 66			break;
 67		case '\0':	/* only specified figures */
 68			unit = 1;
 69			break;
 70		default:
 71			if (!isdigit(str[i]))
 72				goto out_err;
 73			break;
 74		}
 75	}
 76
 77	length = atoll(str) * unit;
 78	goto out;
 
 
 
 
 
 
 79
 80out_err:
 81	length = -1;
 82out:
 83	return length;
 84}
 85
 86/*
 87 * Helper function for splitting a string into an argv-like array.
 88 * originally copied from lib/argv_split.c
 89 */
 90static const char *skip_sep(const char *cp)
 91{
 92	while (*cp && isspace(*cp))
 93		cp++;
 94
 95	return cp;
 96}
 97
 98static const char *skip_arg(const char *cp)
 99{
100	while (*cp && !isspace(*cp))
101		cp++;
102
103	return cp;
104}
105
106static int count_argc(const char *str)
107{
108	int count = 0;
109
110	while (*str) {
111		str = skip_sep(str);
112		if (*str) {
113			count++;
114			str = skip_arg(str);
115		}
116	}
117
118	return count;
119}
120
121/**
122 * argv_free - free an argv
123 * @argv - the argument vector to be freed
124 *
125 * Frees an argv and the strings it points to.
126 */
127void argv_free(char **argv)
128{
129	char **p;
130	for (p = argv; *p; p++)
131		zfree(p);
132
133	free(argv);
134}
135
136/**
137 * argv_split - split a string at whitespace, returning an argv
138 * @str: the string to be split
139 * @argcp: returned argument count
140 *
141 * Returns an array of pointers to strings which are split out from
142 * @str.  This is performed by strictly splitting on white-space; no
143 * quote processing is performed.  Multiple whitespace characters are
144 * considered to be a single argument separator.  The returned array
145 * is always NULL-terminated.  Returns NULL on memory allocation
146 * failure.
147 */
148char **argv_split(const char *str, int *argcp)
149{
150	int argc = count_argc(str);
151	char **argv = zalloc(sizeof(*argv) * (argc+1));
152	char **argvp;
153
154	if (argv == NULL)
155		goto out;
156
157	if (argcp)
158		*argcp = argc;
159
160	argvp = argv;
161
162	while (*str) {
163		str = skip_sep(str);
164
165		if (*str) {
166			const char *p = str;
167			char *t;
168
169			str = skip_arg(str);
170
171			t = strndup(p, str-p);
172			if (t == NULL)
173				goto fail;
174			*argvp++ = t;
175		}
176	}
177	*argvp = NULL;
178
179out:
180	return argv;
181
182fail:
183	argv_free(argv);
184	return NULL;
185}
186
187/* Character class matching */
188static bool __match_charclass(const char *pat, char c, const char **npat)
189{
190	bool complement = false, ret = true;
191
192	if (*pat == '!') {
193		complement = true;
194		pat++;
195	}
196	if (*pat++ == c)	/* First character is special */
197		goto end;
198
199	while (*pat && *pat != ']') {	/* Matching */
200		if (*pat == '-' && *(pat + 1) != ']') {	/* Range */
201			if (*(pat - 1) <= c && c <= *(pat + 1))
202				goto end;
203			if (*(pat - 1) > *(pat + 1))
204				goto error;
205			pat += 2;
206		} else if (*pat++ == c)
207			goto end;
208	}
209	if (!*pat)
210		goto error;
211	ret = false;
212
213end:
214	while (*pat && *pat != ']')	/* Searching closing */
215		pat++;
216	if (!*pat)
217		goto error;
218	*npat = pat + 1;
219	return complement ? !ret : ret;
220
221error:
222	return false;
223}
224
225/* Glob/lazy pattern matching */
226static bool __match_glob(const char *str, const char *pat, bool ignore_space)
 
227{
228	while (*str && *pat && *pat != '*') {
229		if (ignore_space) {
230			/* Ignore spaces for lazy matching */
231			if (isspace(*str)) {
232				str++;
233				continue;
234			}
235			if (isspace(*pat)) {
236				pat++;
237				continue;
238			}
239		}
240		if (*pat == '?') {	/* Matches any single character */
241			str++;
242			pat++;
243			continue;
244		} else if (*pat == '[')	/* Character classes/Ranges */
245			if (__match_charclass(pat + 1, *str, &pat)) {
246				str++;
247				continue;
248			} else
249				return false;
250		else if (*pat == '\\') /* Escaped char match as normal char */
251			pat++;
252		if (*str++ != *pat++)
 
 
 
253			return false;
 
 
254	}
255	/* Check wild card */
256	if (*pat == '*') {
257		while (*pat == '*')
258			pat++;
259		if (!*pat)	/* Tail wild card matches all */
260			return true;
261		while (*str)
262			if (__match_glob(str++, pat, ignore_space))
263				return true;
264	}
265	return !*str && !*pat;
266}
267
268/**
269 * strglobmatch - glob expression pattern matching
270 * @str: the target string to match
271 * @pat: the pattern string to match
272 *
273 * This returns true if the @str matches @pat. @pat can includes wildcards
274 * ('*','?') and character classes ([CHARS], complementation and ranges are
275 * also supported). Also, this supports escape character ('\') to use special
276 * characters as normal character.
277 *
278 * Note: if @pat syntax is broken, this always returns false.
279 */
280bool strglobmatch(const char *str, const char *pat)
281{
282	return __match_glob(str, pat, false);
 
 
 
 
 
283}
284
285/**
286 * strlazymatch - matching pattern strings lazily with glob pattern
287 * @str: the target string to match
288 * @pat: the pattern string to match
289 *
290 * This is similar to strglobmatch, except this ignores spaces in
291 * the target string.
292 */
293bool strlazymatch(const char *str, const char *pat)
294{
295	return __match_glob(str, pat, true);
296}
297
298/**
299 * strtailcmp - Compare the tail of two strings
300 * @s1: 1st string to be compared
301 * @s2: 2nd string to be compared
302 *
303 * Return 0 if whole of either string is same as another's tail part.
304 */
305int strtailcmp(const char *s1, const char *s2)
306{
307	int i1 = strlen(s1);
308	int i2 = strlen(s2);
309	while (--i1 >= 0 && --i2 >= 0) {
310		if (s1[i1] != s2[i2])
311			return s1[i1] - s2[i2];
312	}
313	return 0;
314}
315
316/**
317 * strxfrchar - Locate and replace character in @s
318 * @s:    The string to be searched/changed.
319 * @from: Source character to be replaced.
320 * @to:   Destination character.
321 *
322 * Return pointer to the changed string.
323 */
324char *strxfrchar(char *s, char from, char to)
325{
326	char *p = s;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
328	while ((p = strchr(p, from)) != NULL)
329		*p++ = to;
 
 
 
 
 
 
 
 
330
331	return s;
332}
333
334/**
335 * ltrim - Removes leading whitespace from @s.
336 * @s: The string to be stripped.
337 *
338 * Return pointer to the first non-whitespace character in @s.
339 */
340char *ltrim(char *s)
341{
342	int len = strlen(s);
343
344	while (len && isspace(*s)) {
345		len--;
346		s++;
347	}
348
349	return s;
350}
351
352/**
353 * rtrim - Removes trailing whitespace from @s.
354 * @s: The string to be stripped.
355 *
356 * Note that the first trailing whitespace is replaced with a %NUL-terminator
357 * in the given string @s. Returns @s.
358 */
359char *rtrim(char *s)
360{
361	size_t size = strlen(s);
362	char *end;
363
364	if (!size)
365		return s;
366
367	end = s + size - 1;
368	while (end >= s && isspace(*end))
369		end--;
370	*(end + 1) = '\0';
371
372	return s;
373}
374
375/**
376 * memdup - duplicate region of memory
377 * @src: memory region to duplicate
378 * @len: memory region length
379 */
380void *memdup(const void *src, size_t len)
381{
382	void *p;
383
384	p = malloc(len);
385	if (p)
386		memcpy(p, src, len);
387
388	return p;
389}
 
390
391/**
392 * str_append - reallocate string and append another
393 * @s: pointer to string pointer
394 * @len: pointer to len (initialized)
395 * @a: string to append.
396 */
397int str_append(char **s, int *len, const char *a)
398{
399	int olen = *s ? strlen(*s) : 0;
400	int nlen = olen + strlen(a) + 1;
401	if (*len < nlen) {
402		*len = *len * 2;
403		if (*len < nlen)
404			*len = nlen;
405		*s = realloc(*s, *len);
406		if (!*s)
407			return -ENOMEM;
408		if (olen == 0)
409			**s = 0;
410	}
411	strcat(*s, a);
412	return 0;
 
 
 
413}