1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc. Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17 /*
18 FUNCTION
19 <<strncasecmp>>---case-insensitive character string compare
20
21 INDEX
22 strncasecmp
23
24 SYNOPSIS
25 #include <strings.h>
26 int strncasecmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
27
28 DESCRIPTION
29 <<strncasecmp>> compares up to <[length]> characters
30 from the string at <[a]> to the string at <[b]> in a
31 case-insensitive manner.
32
33 RETURNS
34
35 If <<*<[a]>>> sorts lexicographically after <<*<[b]>>> (after
36 both are converted to lowercase), <<strncasecmp>> returns a
37 number greater than zero. If the two strings are equivalent,
38 <<strncasecmp>> returns zero. If <<*<[a]>>> sorts
39 lexicographically before <<*<[b]>>>, <<strncasecmp>> returns a
40 number less than zero.
41
42 PORTABILITY
43 <<strncasecmp>> is in the Berkeley Software Distribution.
44
45 <<strncasecmp>> requires no supporting OS subroutines. It uses
46 tolower() from elsewhere in this library.
47
48 QUICKREF
49 strncasecmp
50 */
51
52 #include <strings.h>
53 #include <ctype.h>
54
55 int
strncasecmp(const char * s1,const char * s2,size_t n)56 strncasecmp (const char *s1,
57 const char *s2,
58 size_t n)
59 {
60 int d = 0;
61 for ( ; n != 0; n--)
62 {
63 const int c1 = tolower(*s1++);
64 const int c2 = tolower(*s2++);
65 if (((d = c1 - c2) != 0) || (c2 == '\0'))
66 break;
67 }
68 return d;
69 }
70