Skip to content

Commit 1c7961a

Browse files
committed
libc: Add strcasecmp support
Add strcasecmp() for case insensitive string compares. There is no ISO standard function for this purpose but strcasecmp() is POSIX standard. Backported from upstream lk commit 9ba1f165cdf947e ("[libc][string] Add strcasecmp support") without adding to the "string.h" header as POSIX says it's in "strings.h" which we already have for whatever reason.
1 parent f4dd008 commit 1c7961a

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/libc/string/rules.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ C_STRING_OPS := \
99
memmove \
1010
memset \
1111
memscpy \
12+
strcasecmp \
1213
strcat \
1314
strchr \
1415
strcmp \

lib/libc/string/strcasecmp.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3+
** Distributed under the terms of the NewOS License.
4+
*/
5+
/*
6+
* Copyright (c) 2008 Travis Geiselbrecht
7+
* Copyright 2022 Google LLC
8+
*
9+
* Use of this source code is governed by a MIT-style
10+
* license that can be found in the LICENSE file or at
11+
* https://opensource.org/licenses/MIT
12+
*/
13+
#include <ctype.h>
14+
#include <strings.h>
15+
#include <sys/types.h>
16+
17+
int strcasecmp(char const *cs, char const *ct) {
18+
signed char __res;
19+
20+
while (1) {
21+
if ((__res = tolower(*cs) - tolower(*ct)) != 0 || !*cs)
22+
break;
23+
cs++;
24+
ct++;
25+
}
26+
27+
return __res;
28+
}

0 commit comments

Comments
 (0)