Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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