Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Taken from:
0004  *  linux/lib/string.c
0005  *
0006  *  Copyright (C) 1991, 1992  Linus Torvalds
0007  */
0008 
0009 #include <linux/ctype.h>
0010 #include <linux/kernel.h>
0011 #include <linux/types.h>
0012 #include <linux/string.h>
0013 
0014 #ifndef __HAVE_ARCH_STRSTR
0015 /**
0016  * strstr - Find the first substring in a %NUL terminated string
0017  * @s1: The string to be searched
0018  * @s2: The string to search for
0019  */
0020 char *strstr(const char *s1, const char *s2)
0021 {
0022     size_t l1, l2;
0023 
0024     l2 = strlen(s2);
0025     if (!l2)
0026         return (char *)s1;
0027     l1 = strlen(s1);
0028     while (l1 >= l2) {
0029         l1--;
0030         if (!memcmp(s1, s2, l2))
0031             return (char *)s1;
0032         s1++;
0033     }
0034     return NULL;
0035 }
0036 #endif
0037 
0038 #ifndef __HAVE_ARCH_STRNCMP
0039 /**
0040  * strncmp - Compare two length-limited strings
0041  * @cs: One string
0042  * @ct: Another string
0043  * @count: The maximum number of bytes to compare
0044  */
0045 int strncmp(const char *cs, const char *ct, size_t count)
0046 {
0047     unsigned char c1, c2;
0048 
0049     while (count) {
0050         c1 = *cs++;
0051         c2 = *ct++;
0052         if (c1 != c2)
0053             return c1 < c2 ? -1 : 1;
0054         if (!c1)
0055             break;
0056         count--;
0057     }
0058     return 0;
0059 }
0060 #endif
0061 
0062 /* Works only for digits and letters, but small and fast */
0063 #define TOLOWER(x) ((x) | 0x20)
0064 
0065 static unsigned int simple_guess_base(const char *cp)
0066 {
0067     if (cp[0] == '0') {
0068         if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
0069             return 16;
0070         else
0071             return 8;
0072     } else {
0073         return 10;
0074     }
0075 }
0076 
0077 /**
0078  * simple_strtoull - convert a string to an unsigned long long
0079  * @cp: The start of the string
0080  * @endp: A pointer to the end of the parsed string will be placed here
0081  * @base: The number base to use
0082  */
0083 
0084 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
0085 {
0086     unsigned long long result = 0;
0087 
0088     if (!base)
0089         base = simple_guess_base(cp);
0090 
0091     if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
0092         cp += 2;
0093 
0094     while (isxdigit(*cp)) {
0095         unsigned int value;
0096 
0097         value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
0098         if (value >= base)
0099             break;
0100         result = result * base + value;
0101         cp++;
0102     }
0103     if (endp)
0104         *endp = (char *)cp;
0105 
0106     return result;
0107 }
0108 
0109 long simple_strtol(const char *cp, char **endp, unsigned int base)
0110 {
0111     if (*cp == '-')
0112         return -simple_strtoull(cp + 1, endp, base);
0113 
0114     return simple_strtoull(cp, endp, base);
0115 }