Linux 4.9.243
[linux/fpc-iii.git] / arch / x86 / boot / string.c
blob08dfce02362c2cee996e48d4796f3efd98ebfe8d
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>
16 #include "ctype.h"
17 #include "string.h"
20 * Undef these macros so that the functions that we provide
21 * here will have the correct names regardless of how string.h
22 * may have chosen to #define them.
24 #undef memcpy
25 #undef memset
26 #undef memcmp
28 int memcmp(const void *s1, const void *s2, size_t len)
30 bool diff;
31 asm("repe; cmpsb; setnz %0"
32 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
33 return diff;
36 int strcmp(const char *str1, const char *str2)
38 const unsigned char *s1 = (const unsigned char *)str1;
39 const unsigned char *s2 = (const unsigned char *)str2;
40 int delta = 0;
42 while (*s1 || *s2) {
43 delta = *s1 - *s2;
44 if (delta)
45 return delta;
46 s1++;
47 s2++;
49 return 0;
52 int strncmp(const char *cs, const char *ct, size_t count)
54 unsigned char c1, c2;
56 while (count) {
57 c1 = *cs++;
58 c2 = *ct++;
59 if (c1 != c2)
60 return c1 < c2 ? -1 : 1;
61 if (!c1)
62 break;
63 count--;
65 return 0;
68 size_t strnlen(const char *s, size_t maxlen)
70 const char *es = s;
71 while (*es && maxlen) {
72 es++;
73 maxlen--;
76 return (es - s);
79 unsigned int atou(const char *s)
81 unsigned int i = 0;
82 while (isdigit(*s))
83 i = i * 10 + (*s++ - '0');
84 return i;
87 /* Works only for digits and letters, but small and fast */
88 #define TOLOWER(x) ((x) | 0x20)
90 static unsigned int simple_guess_base(const char *cp)
92 if (cp[0] == '0') {
93 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
94 return 16;
95 else
96 return 8;
97 } else {
98 return 10;
103 * simple_strtoull - convert a string to an unsigned long long
104 * @cp: The start of the string
105 * @endp: A pointer to the end of the parsed string will be placed here
106 * @base: The number base to use
109 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
111 unsigned long long result = 0;
113 if (!base)
114 base = simple_guess_base(cp);
116 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
117 cp += 2;
119 while (isxdigit(*cp)) {
120 unsigned int value;
122 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
123 if (value >= base)
124 break;
125 result = result * base + value;
126 cp++;
128 if (endp)
129 *endp = (char *)cp;
131 return result;
135 * strlen - Find the length of a string
136 * @s: The string to be sized
138 size_t strlen(const char *s)
140 const char *sc;
142 for (sc = s; *sc != '\0'; ++sc)
143 /* nothing */;
144 return sc - s;
148 * strstr - Find the first substring in a %NUL terminated string
149 * @s1: The string to be searched
150 * @s2: The string to search for
152 char *strstr(const char *s1, const char *s2)
154 size_t l1, l2;
156 l2 = strlen(s2);
157 if (!l2)
158 return (char *)s1;
159 l1 = strlen(s1);
160 while (l1 >= l2) {
161 l1--;
162 if (!memcmp(s1, s2, l2))
163 return (char *)s1;
164 s1++;
166 return NULL;