xref: /openbsd/gnu/usr.bin/texinfo/lib/strncasecmp.c (revision 133306f0)
1 /* strncase.c -- compare at most N characters of two strings without
2 		 taking care for the case
3 Copyright (C) 1992 Free Software Foundation.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 
19 #include <string.h>
20 #include <ctype.h>
21 
22 /* Compare no more than N characters of S1 and S2,
23    ignoring case, returning less than, equal to or
24    greater than zero if S1 is lexicographically less
25    than, equal to or greater than S2.  */
26 int
27 strncasecmp (s1, s2, n)
28      const char *s1;
29      const char *s2;
30      size_t n;
31 {
32   register const unsigned char *p1 = (const unsigned char *) s1;
33   register const unsigned char *p2 = (const unsigned char *) s2;
34   unsigned char c1, c2;
35 
36   if (p1 == p2 || n == 0)
37     return 0;
38 
39   do
40     {
41       c1 = tolower (*p1++);
42       c2 = tolower (*p2++);
43       if (c1 == '\0' || c1 != c2)
44 	return c1 - c2;
45     } while (--n > 0);
46 
47   return c1 - c2;
48 }
49