blob: aca52b83e31dab48ae1cab7acc6b149258b4da03 [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"
H. Peter Anvin5be86562007-07-11 12:18:41 -070017
Vivek Goyalfb4cac572014-03-18 15:26:39 -040018/*
19 * This file gets included in compressed/string.c which might pull in
20 * string_32.h and which in turn maps memcmp to __builtin_memcmp(). Undo
21 * that first.
22 */
23#undef memcmp
24int memcmp(const void *s1, const void *s2, size_t len)
25{
26 u8 diff;
27 asm("repe; cmpsb; setnz %0"
28 : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len));
29 return diff;
30}
31
H. Peter Anvin5be86562007-07-11 12:18:41 -070032int strcmp(const char *str1, const char *str2)
33{
34 const unsigned char *s1 = (const unsigned char *)str1;
35 const unsigned char *s2 = (const unsigned char *)str2;
36 int delta = 0;
37
38 while (*s1 || *s2) {
39 delta = *s2 - *s1;
40 if (delta)
41 return delta;
42 s1++;
43 s2++;
44 }
45 return 0;
46}
47
Pekka Enbergfa97bdf2010-07-11 11:06:57 +030048int strncmp(const char *cs, const char *ct, size_t count)
49{
50 unsigned char c1, c2;
51
52 while (count) {
53 c1 = *cs++;
54 c2 = *ct++;
55 if (c1 != c2)
56 return c1 < c2 ? -1 : 1;
57 if (!c1)
58 break;
59 count--;
60 }
61 return 0;
62}
63
H. Peter Anvin5be86562007-07-11 12:18:41 -070064size_t strnlen(const char *s, size_t maxlen)
65{
66 const char *es = s;
67 while (*es && maxlen) {
68 es++;
69 maxlen--;
70 }
71
72 return (es - s);
73}
74
75unsigned int atou(const char *s)
76{
77 unsigned int i = 0;
78 while (isdigit(*s))
79 i = i * 10 + (*s++ - '0');
80 return i;
81}
Pekka Enbergfa97bdf2010-07-11 11:06:57 +030082
83/* Works only for digits and letters, but small and fast */
84#define TOLOWER(x) ((x) | 0x20)
85
Yinghai Luce0aa5d2010-07-13 13:35:17 -070086static unsigned int simple_guess_base(const char *cp)
87{
88 if (cp[0] == '0') {
89 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
90 return 16;
91 else
92 return 8;
93 } else {
94 return 10;
95 }
96}
97
98/**
99 * simple_strtoull - convert a string to an unsigned long long
100 * @cp: The start of the string
101 * @endp: A pointer to the end of the parsed string will be placed here
102 * @base: The number base to use
103 */
104
Pekka Enbergfa97bdf2010-07-11 11:06:57 +0300105unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
106{
107 unsigned long long result = 0;
108
Yinghai Luce0aa5d2010-07-13 13:35:17 -0700109 if (!base)
110 base = simple_guess_base(cp);
111
Pekka Enbergfa97bdf2010-07-11 11:06:57 +0300112 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
113 cp += 2;
114
115 while (isxdigit(*cp)) {
116 unsigned int value;
117
118 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
119 if (value >= base)
120 break;
121 result = result * base + value;
122 cp++;
123 }
124 if (endp)
125 *endp = (char *)cp;
126
127 return result;
128}
Matt Fleming291f3632011-12-12 21:27:52 +0000129
130/**
131 * strlen - Find the length of a string
132 * @s: The string to be sized
133 */
134size_t strlen(const char *s)
135{
136 const char *sc;
137
138 for (sc = s; *sc != '\0'; ++sc)
139 /* nothing */;
140 return sc - s;
141}
142
143/**
144 * strstr - Find the first substring in a %NUL terminated string
145 * @s1: The string to be searched
146 * @s2: The string to search for
147 */
148char *strstr(const char *s1, const char *s2)
149{
150 size_t l1, l2;
151
152 l2 = strlen(s2);
153 if (!l2)
154 return (char *)s1;
155 l1 = strlen(s1);
156 while (l1 >= l2) {
157 l1--;
158 if (!memcmp(s1, s2, l2))
159 return (char *)s1;
160 s1++;
161 }
162 return NULL;
163}