blob: 630e3664906bfc399c885ff7c3525fc27bfb7218 [file] [log] [blame]
H. Peter Anvin5be86562007-07-11 12:18:41 -07001/* -*- 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/*
H. Peter Anvin5be86562007-07-11 12:18:41 -070012 * Very basic string functions
13 */
14
Vivek Goyal3d379222014-04-25 13:46:11 -040015#include <linux/types.h>
16#include "ctype.h"
Nicholas Mc Guirefac69d02017-01-07 10:38:31 +010017#include "string.h"
H. Peter Anvin5be86562007-07-11 12:18:41 -070018
Vivek Goyalfb4cac572014-03-18 15:26:39 -040019int memcmp(const void *s1, const void *s2, size_t len)
20{
H. Peter Anvin117780e2016-06-08 12:38:38 -070021 bool diff;
Vivek Goyalfb4cac572014-03-18 15:26:39 -040022 asm("repe; cmpsb; setnz %0"
23 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
24 return diff;
25}
26
H. Peter Anvin5be86562007-07-11 12:18:41 -070027int 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) {
Arjun Sreedharan1c1d0462015-03-16 21:07:47 +053034 delta = *s1 - *s2;
H. Peter Anvin5be86562007-07-11 12:18:41 -070035 if (delta)
36 return delta;
37 s1++;
38 s2++;
39 }
40 return 0;
41}
42
Pekka Enbergfa97bdf2010-07-11 11:06:57 +030043int 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
H. Peter Anvin5be86562007-07-11 12:18:41 -070059size_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}
Pekka Enbergfa97bdf2010-07-11 11:06:57 +030077
78/* Works only for digits and letters, but small and fast */
79#define TOLOWER(x) ((x) | 0x20)
80
Yinghai Luce0aa5d2010-07-13 13:35:17 -070081static 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
Pekka Enbergfa97bdf2010-07-11 11:06:57 +0300100unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
101{
102 unsigned long long result = 0;
103
Yinghai Luce0aa5d2010-07-13 13:35:17 -0700104 if (!base)
105 base = simple_guess_base(cp);
106
Pekka Enbergfa97bdf2010-07-11 11:06:57 +0300107 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}
Matt Fleming291f3632011-12-12 21:27:52 +0000124
Baoquan Hed52e7d52017-05-13 13:46:28 +0800125long simple_strtol(const char *cp, char **endp, unsigned int base)
126{
127 if (*cp == '-')
128 return -simple_strtoull(cp + 1, endp, base);
129
130 return simple_strtoull(cp, endp, base);
131}
132
Matt Fleming291f3632011-12-12 21:27:52 +0000133/**
134 * strlen - Find the length of a string
135 * @s: The string to be sized
136 */
137size_t strlen(const char *s)
138{
139 const char *sc;
140
141 for (sc = s; *sc != '\0'; ++sc)
142 /* nothing */;
143 return sc - s;
144}
145
146/**
147 * strstr - Find the first substring in a %NUL terminated string
148 * @s1: The string to be searched
149 * @s2: The string to search for
150 */
151char *strstr(const char *s1, const char *s2)
152{
153 size_t l1, l2;
154
155 l2 = strlen(s2);
156 if (!l2)
157 return (char *)s1;
158 l1 = strlen(s1);
159 while (l1 >= l2) {
160 l1--;
161 if (!memcmp(s1, s2, l2))
162 return (char *)s1;
163 s1++;
164 }
165 return NULL;
166}
Dave Jiangf2844242017-01-11 16:20:01 -0700167
168/**
169 * strchr - Find the first occurrence of the character c in the string s.
170 * @s: the string to be searched
171 * @c: the character to search for
172 */
173char *strchr(const char *s, int c)
174{
175 while (*s != (char)c)
176 if (*s++ == '\0')
177 return NULL;
178 return (char *)s;
179}