H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 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 | /* |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 12 | * Very basic string functions |
| 13 | */ |
| 14 | |
Vivek Goyal | 3d37922 | 2014-04-25 13:46:11 -0400 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include "ctype.h" |
Nicholas Mc Guire | fac69d0 | 2017-01-07 10:38:31 +0100 | [diff] [blame] | 17 | #include "string.h" |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 18 | |
Michael Davidson | 18d5e6c | 2017-07-24 16:51:55 -0700 | [diff] [blame] | 19 | /* |
| 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. |
| 23 | */ |
| 24 | #undef memcpy |
| 25 | #undef memset |
| 26 | #undef memcmp |
| 27 | |
Vivek Goyal | fb4cac57 | 2014-03-18 15:26:39 -0400 | [diff] [blame] | 28 | int memcmp(const void *s1, const void *s2, size_t len) |
| 29 | { |
H. Peter Anvin | 117780e | 2016-06-08 12:38:38 -0700 | [diff] [blame] | 30 | bool diff; |
Vivek Goyal | fb4cac57 | 2014-03-18 15:26:39 -0400 | [diff] [blame] | 31 | asm("repe; cmpsb; setnz %0" |
| 32 | : "=qm" (diff), "+D" (s1), "+S" (s2), "+c" (len)); |
| 33 | return diff; |
| 34 | } |
| 35 | |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 36 | int strcmp(const char *str1, const char *str2) |
| 37 | { |
| 38 | const unsigned char *s1 = (const unsigned char *)str1; |
| 39 | const unsigned char *s2 = (const unsigned char *)str2; |
| 40 | int delta = 0; |
| 41 | |
| 42 | while (*s1 || *s2) { |
Arjun Sreedharan | 1c1d046 | 2015-03-16 21:07:47 +0530 | [diff] [blame] | 43 | delta = *s1 - *s2; |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 44 | if (delta) |
| 45 | return delta; |
| 46 | s1++; |
| 47 | s2++; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 52 | int strncmp(const char *cs, const char *ct, size_t count) |
| 53 | { |
| 54 | unsigned char c1, c2; |
| 55 | |
| 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--; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 68 | size_t strnlen(const char *s, size_t maxlen) |
| 69 | { |
| 70 | const char *es = s; |
| 71 | while (*es && maxlen) { |
| 72 | es++; |
| 73 | maxlen--; |
| 74 | } |
| 75 | |
| 76 | return (es - s); |
| 77 | } |
| 78 | |
| 79 | unsigned int atou(const char *s) |
| 80 | { |
| 81 | unsigned int i = 0; |
| 82 | while (isdigit(*s)) |
| 83 | i = i * 10 + (*s++ - '0'); |
| 84 | return i; |
| 85 | } |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 86 | |
| 87 | /* Works only for digits and letters, but small and fast */ |
| 88 | #define TOLOWER(x) ((x) | 0x20) |
| 89 | |
Yinghai Lu | ce0aa5d | 2010-07-13 13:35:17 -0700 | [diff] [blame] | 90 | static unsigned int simple_guess_base(const char *cp) |
| 91 | { |
| 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; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 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 |
| 107 | */ |
| 108 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 109 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
| 110 | { |
| 111 | unsigned long long result = 0; |
| 112 | |
Yinghai Lu | ce0aa5d | 2010-07-13 13:35:17 -0700 | [diff] [blame] | 113 | if (!base) |
| 114 | base = simple_guess_base(cp); |
| 115 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 116 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
| 117 | cp += 2; |
| 118 | |
| 119 | while (isxdigit(*cp)) { |
| 120 | unsigned int value; |
| 121 | |
| 122 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; |
| 123 | if (value >= base) |
| 124 | break; |
| 125 | result = result * base + value; |
| 126 | cp++; |
| 127 | } |
| 128 | if (endp) |
| 129 | *endp = (char *)cp; |
| 130 | |
| 131 | return result; |
| 132 | } |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 133 | |
Baoquan He | d52e7d5 | 2017-05-13 13:46:28 +0800 | [diff] [blame] | 134 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
| 135 | { |
| 136 | if (*cp == '-') |
| 137 | return -simple_strtoull(cp + 1, endp, base); |
| 138 | |
| 139 | return simple_strtoull(cp, endp, base); |
| 140 | } |
| 141 | |
Matt Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 142 | /** |
| 143 | * strlen - Find the length of a string |
| 144 | * @s: The string to be sized |
| 145 | */ |
| 146 | size_t strlen(const char *s) |
| 147 | { |
| 148 | const char *sc; |
| 149 | |
| 150 | for (sc = s; *sc != '\0'; ++sc) |
| 151 | /* nothing */; |
| 152 | return sc - s; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * strstr - Find the first substring in a %NUL terminated string |
| 157 | * @s1: The string to be searched |
| 158 | * @s2: The string to search for |
| 159 | */ |
| 160 | char *strstr(const char *s1, const char *s2) |
| 161 | { |
| 162 | size_t l1, l2; |
| 163 | |
| 164 | l2 = strlen(s2); |
| 165 | if (!l2) |
| 166 | return (char *)s1; |
| 167 | l1 = strlen(s1); |
| 168 | while (l1 >= l2) { |
| 169 | l1--; |
| 170 | if (!memcmp(s1, s2, l2)) |
| 171 | return (char *)s1; |
| 172 | s1++; |
| 173 | } |
| 174 | return NULL; |
| 175 | } |
Dave Jiang | f284424 | 2017-01-11 16:20:01 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * strchr - Find the first occurrence of the character c in the string s. |
| 179 | * @s: the string to be searched |
| 180 | * @c: the character to search for |
| 181 | */ |
| 182 | char *strchr(const char *s, int c) |
| 183 | { |
| 184 | while (*s != (char)c) |
| 185 | if (*s++ == '\0') |
| 186 | return NULL; |
| 187 | return (char *)s; |
| 188 | } |