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