diff options
author | Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com> | 2013-06-11 14:57:26 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2013-06-20 17:17:32 +0200 |
commit | 196be5c77608bb282913f15743aa73acdfa828ba (patch) | |
tree | efdc5123d8e8c573ee0ed27d603a622c2eee77a8 /lib | |
parent | c179e7c46b573749bd59332c8b5d0a32a506fd8f (diff) | |
download | barebox-196be5c77608bb282913f15743aa73acdfa828ba.tar.gz barebox-196be5c77608bb282913f15743aa73acdfa828ba.tar.xz |
lib: string: import case-insensitive string compare
This imports strnicmp, strcasecmp, and strncasecmp from Linux to barebox.
Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/string.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c index db4f2ae7db..f544b23664 100644 --- a/lib/string.c +++ b/lib/string.c @@ -22,6 +22,66 @@ char * ___strtok; +#ifndef __HAVE_ARCH_STRNICMP +/** + * strnicmp - Case insensitive, length-limited string comparison + * @s1: One string + * @s2: The other string + * @len: the maximum number of characters to compare + */ +int strnicmp(const char *s1, const char *s2, size_t len) +{ + /* Yes, Virginia, it had better be unsigned */ + unsigned char c1, c2; + + if (!len) + return 0; + + do { + c1 = *s1++; + c2 = *s2++; + if (!c1 || !c2) + break; + if (c1 == c2) + continue; + c1 = tolower(c1); + c2 = tolower(c2); + if (c1 != c2) + break; + } while (--len); + return (int)c1 - (int)c2; +} +EXPORT_SYMBOL(strnicmp); +#endif + +#ifndef __HAVE_ARCH_STRCASECMP +int strcasecmp(const char *s1, const char *s2) +{ + int c1, c2; + + do { + c1 = tolower(*s1++); + c2 = tolower(*s2++); + } while (c1 == c2 && c1 != 0); + return c1 - c2; +} +EXPORT_SYMBOL(strcasecmp); +#endif + +#ifndef __HAVE_ARCH_STRNCASECMP +int strncasecmp(const char *s1, const char *s2, size_t n) +{ + int c1, c2; + + do { + c1 = tolower(*s1++); + c2 = tolower(*s2++); + } while ((--n > 0) && c1 == c2 && c1 != 0); + return c1 - c2; +} +EXPORT_SYMBOL(strncasecmp); +#endif + #ifndef __HAVE_ARCH_STRCPY /** * strcpy - Copy a %NUL terminated string |