1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
9 * ----------------------------------------------------------------------- */
12 * Very basic string functions
15 #include <linux/types.h>
21 * Undef these macros so that the functions that we provide
22 * here will have the correct names regardless of how string.h
23 * may have chosen to #define them.
29 int memcmp(const void *s1, const void *s2, size_t len)
32 asm("repe; cmpsb" CC_SET(nz)
33 : CC_OUT(nz) (diff), "+D" (s1), "+S" (s2), "+c" (len));
38 * Clang may lower `memcmp == 0` to `bcmp == 0`.
40 int bcmp(const void *s1, const void *s2, size_t len)
42 return memcmp(s1, s2, len);
45 int strcmp(const char *str1, const char *str2)
47 const unsigned char *s1 = (const unsigned char *)str1;
48 const unsigned char *s2 = (const unsigned char *)str2;
61 int strncmp(const char *cs, const char *ct, size_t count)
69 return c1 < c2 ? -1 : 1;
77 size_t strnlen(const char *s, size_t maxlen)
80 while (*es && maxlen) {
88 unsigned int atou(const char *s)
92 i = i * 10 + (*s++ - '0');
96 /* Works only for digits and letters, but small and fast */
97 #define TOLOWER(x) ((x) | 0x20)
99 static unsigned int simple_guess_base(const char *cp)
102 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
112 * simple_strtoull - convert a string to an unsigned long long
113 * @cp: The start of the string
114 * @endp: A pointer to the end of the parsed string will be placed here
115 * @base: The number base to use
118 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
120 unsigned long long result = 0;
123 base = simple_guess_base(cp);
125 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
128 while (isxdigit(*cp)) {
131 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
134 result = result * base + value;
143 long simple_strtol(const char *cp, char **endp, unsigned int base)
146 return -simple_strtoull(cp + 1, endp, base);
148 return simple_strtoull(cp, endp, base);
152 * strlen - Find the length of a string
153 * @s: The string to be sized
155 size_t strlen(const char *s)
159 for (sc = s; *sc != '\0'; ++sc)
165 * strstr - Find the first substring in a %NUL terminated string
166 * @s1: The string to be searched
167 * @s2: The string to search for
169 char *strstr(const char *s1, const char *s2)
179 if (!memcmp(s1, s2, l2))
187 * strchr - Find the first occurrence of the character c in the string s.
188 * @s: the string to be searched
189 * @c: the character to search for
191 char *strchr(const char *s, int c)
193 while (*s != (char)c)