1 /* Compare at most N characters of two strings without taking care for
2    the case.
3    Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
4 
5    NOTE: The canonical source of this file is maintained with the GNU C Library.
6    Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7 
8    This program is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21    USA.  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26 #ifdef HAVE_SUPPORT_H
27 #  include "support.h"
28 #endif
29 
30 #ifdef HAVE_STRING_H
31 #  include <string.h>
32 #endif
33 
34 #include <ctype.h>
35 
36 #ifndef weak_alias
37 # define __strncasecmp strncasecmp
38 # define TOLOWER(Ch) tolower (Ch)
39 #else
40 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
41 #  define __strncasecmp __strncasecmp_l
42 #  define TOLOWER(Ch) __tolower_l ((Ch), loc)
43 # else
44 #  define TOLOWER(Ch) tolower (Ch)
45 # endif
46 #endif
47 
48 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
49 # define LOCALE_PARAM , loc
50 # define LOCALE_PARAM_DECL __locale_t loc;
51 #else
52 # define LOCALE_PARAM
53 # define LOCALE_PARAM_DECL
54 #endif
55 
56 /* Compare no more than N characters of S1 and S2,
57    ignoring case, returning less than, equal to or
58    greater than zero if S1 is lexicographically less
59    than, equal to or greater than S2.  */
60 int
61 __strncasecmp (s1, s2, n LOCALE_PARAM)
62      const char *s1;
63      const char *s2;
64      size_t n;
65      LOCALE_PARAM_DECL
66 {
67   const unsigned char *p1 = (const unsigned char *) s1;
68   const unsigned char *p2 = (const unsigned char *) s2;
69   unsigned char c1, c2;
70 
71   if (p1 == p2 || n == 0)
72     return 0;
73 
74   do
75     {
76       c1 = TOLOWER (*p1++);
77       c2 = TOLOWER (*p2++);
78       if (c1 == '\0' || c1 != c2)
79 	return c1 - c2;
80     } while (--n > 0);
81 
82   return c1 - c2;
83 }
84 #ifndef __strncasecmp
85 weak_alias (__strncasecmp, strncasecmp)
86 #endif
87