13fb98d4aSespie /* strcasecmp.c -- case insensitive string comparator
23fb98d4aSespie Copyright (C) 1998, 1999 Free Software Foundation, Inc.
31cc83814Sespie
41cc83814Sespie This program is free software; you can redistribute it and/or modify
51cc83814Sespie it under the terms of the GNU General Public License as published by
61cc83814Sespie the Free Software Foundation; either version 2, or (at your option)
71cc83814Sespie any later version.
81cc83814Sespie
91cc83814Sespie This program is distributed in the hope that it will be useful,
101cc83814Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
111cc83814Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121cc83814Sespie GNU General Public License for more details.
131cc83814Sespie
141cc83814Sespie You should have received a copy of the GNU General Public License
153fb98d4aSespie along with this program; if not, write to the Free Software Foundation,
163fb98d4aSespie Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
171cc83814Sespie
183fb98d4aSespie #if HAVE_CONFIG_H
191cc83814Sespie # include <config.h>
201cc83814Sespie #endif
211cc83814Sespie
223fb98d4aSespie #ifdef LENGTH_LIMIT
233fb98d4aSespie # define STRXCASECMP_FUNCTION strncasecmp
243fb98d4aSespie # define STRXCASECMP_DECLARE_N , size_t n
253fb98d4aSespie # define LENGTH_LIMIT_EXPR(Expr) Expr
263fb98d4aSespie #else
273fb98d4aSespie # define STRXCASECMP_FUNCTION strcasecmp
283fb98d4aSespie # define STRXCASECMP_DECLARE_N /* empty */
293fb98d4aSespie # define LENGTH_LIMIT_EXPR(Expr) 0
303fb98d4aSespie #endif
313fb98d4aSespie
32*a1acfa9bSespie #include <stddef.h>
331cc83814Sespie #include <ctype.h>
341cc83814Sespie
353fb98d4aSespie #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
363fb98d4aSespie
373fb98d4aSespie /* Compare {{no more than N characters of }}strings S1 and S2,
383fb98d4aSespie ignoring case, returning less than, equal to or
393fb98d4aSespie greater than zero if S1 is lexicographically less
403fb98d4aSespie than, equal to or greater than S2. */
413fb98d4aSespie
421cc83814Sespie int
STRXCASECMP_FUNCTION(const char * s1,const char * s2 STRXCASECMP_DECLARE_N)433fb98d4aSespie STRXCASECMP_FUNCTION (const char *s1, const char *s2 STRXCASECMP_DECLARE_N)
441cc83814Sespie {
451cc83814Sespie register const unsigned char *p1 = (const unsigned char *) s1;
461cc83814Sespie register const unsigned char *p2 = (const unsigned char *) s2;
471cc83814Sespie unsigned char c1, c2;
481cc83814Sespie
493fb98d4aSespie if (p1 == p2 || LENGTH_LIMIT_EXPR (n == 0))
501cc83814Sespie return 0;
511cc83814Sespie
521cc83814Sespie do
531cc83814Sespie {
543fb98d4aSespie c1 = TOLOWER (*p1);
553fb98d4aSespie c2 = TOLOWER (*p2);
563fb98d4aSespie
573fb98d4aSespie if (LENGTH_LIMIT_EXPR (--n == 0) || c1 == '\0')
581cc83814Sespie break;
593fb98d4aSespie
603fb98d4aSespie ++p1;
613fb98d4aSespie ++p2;
621cc83814Sespie }
631cc83814Sespie while (c1 == c2);
641cc83814Sespie
651cc83814Sespie return c1 - c2;
661cc83814Sespie }
67