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 | |
| 15 | #include "boot.h" |
| 16 | |
| 17 | int strcmp(const char *str1, const char *str2) |
| 18 | { |
| 19 | const unsigned char *s1 = (const unsigned char *)str1; |
| 20 | const unsigned char *s2 = (const unsigned char *)str2; |
| 21 | int delta = 0; |
| 22 | |
| 23 | while (*s1 || *s2) { |
| 24 | delta = *s2 - *s1; |
| 25 | if (delta) |
| 26 | return delta; |
| 27 | s1++; |
| 28 | s2++; |
| 29 | } |
| 30 | return 0; |
| 31 | } |
| 32 | |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame^] | 33 | int strncmp(const char *cs, const char *ct, size_t count) |
| 34 | { |
| 35 | unsigned char c1, c2; |
| 36 | |
| 37 | while (count) { |
| 38 | c1 = *cs++; |
| 39 | c2 = *ct++; |
| 40 | if (c1 != c2) |
| 41 | return c1 < c2 ? -1 : 1; |
| 42 | if (!c1) |
| 43 | break; |
| 44 | count--; |
| 45 | } |
| 46 | return 0; |
| 47 | } |
| 48 | |
H. Peter Anvin | 5be8656 | 2007-07-11 12:18:41 -0700 | [diff] [blame] | 49 | size_t strnlen(const char *s, size_t maxlen) |
| 50 | { |
| 51 | const char *es = s; |
| 52 | while (*es && maxlen) { |
| 53 | es++; |
| 54 | maxlen--; |
| 55 | } |
| 56 | |
| 57 | return (es - s); |
| 58 | } |
| 59 | |
| 60 | unsigned int atou(const char *s) |
| 61 | { |
| 62 | unsigned int i = 0; |
| 63 | while (isdigit(*s)) |
| 64 | i = i * 10 + (*s++ - '0'); |
| 65 | return i; |
| 66 | } |
Pekka Enberg | fa97bdf | 2010-07-11 11:06:57 +0300 | [diff] [blame^] | 67 | |
| 68 | /* Works only for digits and letters, but small and fast */ |
| 69 | #define TOLOWER(x) ((x) | 0x20) |
| 70 | |
| 71 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
| 72 | { |
| 73 | unsigned long long result = 0; |
| 74 | |
| 75 | if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') |
| 76 | cp += 2; |
| 77 | |
| 78 | while (isxdigit(*cp)) { |
| 79 | unsigned int value; |
| 80 | |
| 81 | value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; |
| 82 | if (value >= base) |
| 83 | break; |
| 84 | result = result * base + value; |
| 85 | cp++; |
| 86 | } |
| 87 | if (endp) |
| 88 | *endp = (char *)cp; |
| 89 | |
| 90 | return result; |
| 91 | } |