Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/ctype.h>
  3#include <linux/kernel.h>
  4#include <linux/errno.h>
  5#undef CONFIG_KASAN
  6#undef CONFIG_KASAN_GENERIC
  7#include "../lib/string.c"
  8
  9int strncmp(const char *cs, const char *ct, size_t count)
 10{
 11	unsigned char c1, c2;
 12
 13	while (count) {
 14		c1 = *cs++;
 15		c2 = *ct++;
 16		if (c1 != c2)
 17			return c1 < c2 ? -1 : 1;
 18		if (!c1)
 19			break;
 20		count--;
 21	}
 22	return 0;
 23}
 24
 25char *skip_spaces(const char *str)
 26{
 27	while (isspace(*str))
 28		++str;
 29	return (char *)str;
 30}
 31
 32char *strim(char *s)
 33{
 34	size_t size;
 35	char *end;
 36
 37	size = strlen(s);
 38	if (!size)
 39		return s;
 40
 41	end = s + size - 1;
 42	while (end >= s && isspace(*end))
 43		end--;
 44	*(end + 1) = '\0';
 45
 46	return skip_spaces(s);
 47}
 48
 49/* Works only for digits and letters, but small and fast */
 50#define TOLOWER(x) ((x) | 0x20)
 51
 52static unsigned int simple_guess_base(const char *cp)
 53{
 54	if (cp[0] == '0') {
 55		if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
 56			return 16;
 57		else
 58			return 8;
 59	} else {
 60		return 10;
 61	}
 62}
 63
 64/**
 65 * simple_strtoull - convert a string to an unsigned long long
 66 * @cp: The start of the string
 67 * @endp: A pointer to the end of the parsed string will be placed here
 68 * @base: The number base to use
 69 */
 70
 71unsigned long long simple_strtoull(const char *cp, char **endp,
 72				   unsigned int base)
 73{
 74	unsigned long long result = 0;
 75
 76	if (!base)
 77		base = simple_guess_base(cp);
 78
 79	if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
 80		cp += 2;
 81
 82	while (isxdigit(*cp)) {
 83		unsigned int value;
 84
 85		value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
 86		if (value >= base)
 87			break;
 88		result = result * base + value;
 89		cp++;
 90	}
 91	if (endp)
 92		*endp = (char *)cp;
 93
 94	return result;
 95}
 96
 97long simple_strtol(const char *cp, char **endp, unsigned int base)
 98{
 99	if (*cp == '-')
100		return -simple_strtoull(cp + 1, endp, base);
101
102	return simple_strtoull(cp, endp, base);
103}
104
105int kstrtobool(const char *s, bool *res)
106{
107	if (!s)
108		return -EINVAL;
109
110	switch (s[0]) {
111	case 'y':
112	case 'Y':
113	case '1':
114		*res = true;
115		return 0;
116	case 'n':
117	case 'N':
118	case '0':
119		*res = false;
120		return 0;
121	case 'o':
122	case 'O':
123		switch (s[1]) {
124		case 'n':
125		case 'N':
126			*res = true;
127			return 0;
128		case 'f':
129		case 'F':
130			*res = false;
131			return 0;
132		default:
133			break;
134		}
135	default:
136		break;
137	}
138
139	return -EINVAL;
140}