1 // Copyright 2016 The Fuchsia Authors
2 // Copyright (c) 2008 Travis Geiselbrecht
3 //
4 // Use of this source code is governed by a MIT-style
5 // license that can be found in the LICENSE file or at
6 // https://opensource.org/licenses/MIT
7 
8 #include <string.h>
9 #include <ctype.h>
10 #include <sys/types.h>
11 
12 int
strnicmp(char const * s1,char const * s2,size_t len)13 strnicmp(char const *s1, char const *s2, size_t len)
14 {
15     unsigned char c1 = '\0';
16     unsigned char c2 = '\0';
17 
18     if (len > 0) {
19         do {
20             c1 = *s1;
21             c2 = *s2;
22             s1++;
23             s2++;
24             if (!c1)
25                 break;
26             if (!c2)
27                 break;
28             if (c1 == c2)
29                 continue;
30             c1 = tolower(c1);
31             c2 = tolower(c2);
32             if (c1 != c2)
33                 break;
34         } while (--len);
35     }
36     return (int)c1 - (int)c2;
37 }
38 #pragma weak strncasecmp=strnicmp
39