Loading...
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
5 *
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.
8 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * Very basic string functions
13 */
14
15#include "boot.h"
16
17int strcmp(const char *str1, const char *str2)
18{
19 const unsigned char *s1 = (const unsigned char *)str1;
20 const unsigned char *s2 = (const unsigned char *)str2;
21 int delta = 0;
22
23 while (*s1 || *s2) {
24 delta = *s2 - *s1;
25 if (delta)
26 return delta;
27 s1++;
28 s2++;
29 }
30 return 0;
31}
32
33int strncmp(const char *cs, const char *ct, size_t count)
34{
35 unsigned char c1, c2;
36
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--;
45 }
46 return 0;
47}
48
49size_t strnlen(const char *s, size_t maxlen)
50{
51 const char *es = s;
52 while (*es && maxlen) {
53 es++;
54 maxlen--;
55 }
56
57 return (es - s);
58}
59
60unsigned int atou(const char *s)
61{
62 unsigned int i = 0;
63 while (isdigit(*s))
64 i = i * 10 + (*s++ - '0');
65 return i;
66}
67
68/* Works only for digits and letters, but small and fast */
69#define TOLOWER(x) ((x) | 0x20)
70
71static unsigned int simple_guess_base(const char *cp)
72{
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;
80 }
81}
82
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
88 */
89
90unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
91{
92 unsigned long long result = 0;
93
94 if (!base)
95 base = simple_guess_base(cp);
96
97 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
98 cp += 2;
99
100 while (isxdigit(*cp)) {
101 unsigned int value;
102
103 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
104 if (value >= base)
105 break;
106 result = result * base + value;
107 cp++;
108 }
109 if (endp)
110 *endp = (char *)cp;
111
112 return result;
113}
1/* -*- linux-c -*- ------------------------------------------------------- *
2 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
5 *
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.
8 *
9 * ----------------------------------------------------------------------- */
10
11/*
12 * Very basic string functions
13 */
14
15#include <linux/types.h>
16#include "ctype.h"
17#include "string.h"
18
19int memcmp(const void *s1, const void *s2, size_t len)
20{
21 bool diff;
22 asm("repe; cmpsb; setnz %0"
23 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
24 return diff;
25}
26
27int strcmp(const char *str1, const char *str2)
28{
29 const unsigned char *s1 = (const unsigned char *)str1;
30 const unsigned char *s2 = (const unsigned char *)str2;
31 int delta = 0;
32
33 while (*s1 || *s2) {
34 delta = *s1 - *s2;
35 if (delta)
36 return delta;
37 s1++;
38 s2++;
39 }
40 return 0;
41}
42
43int strncmp(const char *cs, const char *ct, size_t count)
44{
45 unsigned char c1, c2;
46
47 while (count) {
48 c1 = *cs++;
49 c2 = *ct++;
50 if (c1 != c2)
51 return c1 < c2 ? -1 : 1;
52 if (!c1)
53 break;
54 count--;
55 }
56 return 0;
57}
58
59size_t strnlen(const char *s, size_t maxlen)
60{
61 const char *es = s;
62 while (*es && maxlen) {
63 es++;
64 maxlen--;
65 }
66
67 return (es - s);
68}
69
70unsigned int atou(const char *s)
71{
72 unsigned int i = 0;
73 while (isdigit(*s))
74 i = i * 10 + (*s++ - '0');
75 return i;
76}
77
78/* Works only for digits and letters, but small and fast */
79#define TOLOWER(x) ((x) | 0x20)
80
81static unsigned int simple_guess_base(const char *cp)
82{
83 if (cp[0] == '0') {
84 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
85 return 16;
86 else
87 return 8;
88 } else {
89 return 10;
90 }
91}
92
93/**
94 * simple_strtoull - convert a string to an unsigned long long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
98 */
99
100unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
101{
102 unsigned long long result = 0;
103
104 if (!base)
105 base = simple_guess_base(cp);
106
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
109
110 while (isxdigit(*cp)) {
111 unsigned int value;
112
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
117 cp++;
118 }
119 if (endp)
120 *endp = (char *)cp;
121
122 return result;
123}
124
125/**
126 * strlen - Find the length of a string
127 * @s: The string to be sized
128 */
129size_t strlen(const char *s)
130{
131 const char *sc;
132
133 for (sc = s; *sc != '\0'; ++sc)
134 /* nothing */;
135 return sc - s;
136}
137
138/**
139 * strstr - Find the first substring in a %NUL terminated string
140 * @s1: The string to be searched
141 * @s2: The string to search for
142 */
143char *strstr(const char *s1, const char *s2)
144{
145 size_t l1, l2;
146
147 l2 = strlen(s2);
148 if (!l2)
149 return (char *)s1;
150 l1 = strlen(s1);
151 while (l1 >= l2) {
152 l1--;
153 if (!memcmp(s1, s2, l2))
154 return (char *)s1;
155 s1++;
156 }
157 return NULL;
158}