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" |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 17 | |
Vivek Goyal | fb4cac57 | 2014-03-18 15:26:39 -0400 | [diff] [blame] | 18 | /* |
| 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 |
| 24 | int 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 Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 32 | int 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 Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 48 | int 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 Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 64 | size_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 | |
| 75 | unsigned 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 Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 82 | |
| 83 | /* Works only for digits and letters, but small and fast */ |
| 84 | #define TOLOWER(x) ((x) | 0x20) |
| 85 | |
Yinghai Lu | ce0aa5d | 2010-07-13 13:35:17 -0700 | [diff] [blame] | 86 | static 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 Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 105 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
| 106 | { |
| 107 | unsigned long long result = 0; |
| 108 | |
Yinghai Lu | ce0aa5d | 2010-07-13 13:35:17 -0700 | [diff] [blame] | 109 | if (!base) |
| 110 | base = simple_guess_base(cp); |
| 111 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame] | 112 | 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 Fleming | 291f363 | 2011-12-12 21:27:52 +0000 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * strlen - Find the length of a string |
| 132 | * @s: The string to be sized |
| 133 | */ |
| 134 | size_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 | */ |
| 148 | char *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 | } |