Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / arch / x86 / boot / string.c
blob574dedfe28900c0445b24d51b09d168116cb205f
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 "boot.h"
17 int strcmp(const char *str1, const char *str2)
19 const unsigned char *s1 = (const unsigned char *)str1;
20 const unsigned char *s2 = (const unsigned char *)str2;
21 int delta = 0;
23 while (*s1 || *s2) {
24 delta = *s2 - *s1;
25 if (delta)
26 return delta;
27 s1++;
28 s2++;
30 return 0;
33 int strncmp(const char *cs, const char *ct, size_t count)
35 unsigned char c1, c2;
37 while (count) {
38 c1 = *cs++;
39 c2 = *ct++;
40 if (c1 != c2)
41 return c1 < c2 ? -1 : 1;
42 if (!c1)
43 break;
44 count--;
46 return 0;
49 size_t strnlen(const char *s, size_t maxlen)
51 const char *es = s;
52 while (*es && maxlen) {
53 es++;
54 maxlen--;
57 return (es - s);
60 unsigned int atou(const char *s)
62 unsigned int i = 0;
63 while (isdigit(*s))
64 i = i * 10 + (*s++ - '0');
65 return i;
68 /* Works only for digits and letters, but small and fast */
69 #define TOLOWER(x) ((x) | 0x20)
71 static unsigned int simple_guess_base(const char *cp)
73 if (cp[0] == '0') {
74 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
75 return 16;
76 else
77 return 8;
78 } else {
79 return 10;
83 /**
84 * simple_strtoull - convert a string to an unsigned long long
85 * @cp: The start of the string
86 * @endp: A pointer to the end of the parsed string will be placed here
87 * @base: The number base to use
90 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
92 unsigned long long result = 0;
94 if (!base)
95 base = simple_guess_base(cp);
97 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
98 cp += 2;
100 while (isxdigit(*cp)) {
101 unsigned int value;
103 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
104 if (value >= base)
105 break;
106 result = result * base + value;
107 cp++;
109 if (endp)
110 *endp = (char *)cp;
112 return result;
116 * strlen - Find the length of a string
117 * @s: The string to be sized
119 size_t strlen(const char *s)
121 const char *sc;
123 for (sc = s; *sc != '\0'; ++sc)
124 /* nothing */;
125 return sc - s;
129 * strstr - Find the first substring in a %NUL terminated string
130 * @s1: The string to be searched
131 * @s2: The string to search for
133 char *strstr(const char *s1, const char *s2)
135 size_t l1, l2;
137 l2 = strlen(s2);
138 if (!l2)
139 return (char *)s1;
140 l1 = strlen(s1);
141 while (l1 >= l2) {
142 l1--;
143 if (!memcmp(s1, s2, l2))
144 return (char *)s1;
145 s1++;
147 return NULL;