aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Odell <aaronodell@google.com>2023-04-04 19:18:58 -0700
committerTravis Geiselbrecht <geist@foobox.com>2023-04-23 17:34:54 -0700
commit9ba1f165cdf947e443431a999d1943dbb94ba48e (patch)
tree064f66b1a255a2d6b87dab7e2bdd998ebce2b8c4
parent9ac5708eb5d1820efd5ace3871300b100cb6ea05 (diff)
downloadlk-9ba1f165cdf947e443431a999d1943dbb94ba48e.tar.gz
[libc][string] Add strcasecmp support
Add strcasecmp() for case insensitive string compares. There is no ISO standard function for this purpose but strcasecmp() is POSIX standard.
-rw-r--r--lib/libc/include/string.h1
-rw-r--r--lib/libc/string/rules.mk1
-rw-r--r--lib/libc/string/strcasecmp.c28
3 files changed, 30 insertions, 0 deletions
diff --git a/lib/libc/include/string.h b/lib/libc/include/string.h
index 2d3dc9d1..41f298ef 100644
--- a/lib/libc/include/string.h
+++ b/lib/libc/include/string.h
@@ -21,6 +21,7 @@ void *memset (void *, int, size_t);
char *strcat(char *, char const *);
char *strchr(char const *, int) __PURE;
int strcmp(char const *, char const *) __PURE;
+int strcasecmp(char const *, char const *) __PURE;
char *strcpy(char *, char const *);
char *strerror(int) __CONST;
size_t strlen(char const *) __PURE;
diff --git a/lib/libc/string/rules.mk b/lib/libc/string/rules.mk
index dd75c1c0..df61786e 100644
--- a/lib/libc/string/rules.mk
+++ b/lib/libc/string/rules.mk
@@ -8,6 +8,7 @@ C_STRING_OPS := \
memcpy \
memmove \
memset \
+ strcasecmp \
strcat \
strchr \
strcmp \
diff --git a/lib/libc/string/strcasecmp.c b/lib/libc/string/strcasecmp.c
new file mode 100644
index 00000000..48fc0a00
--- /dev/null
+++ b/lib/libc/string/strcasecmp.c
@@ -0,0 +1,28 @@
+/*
+** Copyright 2001, Travis Geiselbrecht. All rights reserved.
+** Distributed under the terms of the NewOS License.
+*/
+/*
+ * Copyright (c) 2008 Travis Geiselbrecht
+ * Copyright 2022 Google LLC
+ *
+ * Use of this source code is governed by a MIT-style
+ * license that can be found in the LICENSE file or at
+ * https://opensource.org/licenses/MIT
+ */
+#include <ctype.h>
+#include <string.h>
+#include <sys/types.h>
+
+int strcasecmp(char const *cs, char const *ct) {
+ signed char __res;
+
+ while (1) {
+ if ((__res = tolower(*cs) - tolower(*ct)) != 0 || !*cs)
+ break;
+ cs++;
+ ct++;
+ }
+
+ return __res;
+}