1114402Sru/* strcasecmp.c -- case insensitive string comparator 2114402Sru Copyright (C) 1998, 1999 Free Software Foundation, Inc. 3114402Sru 4114402Sru This program is free software; you can redistribute it and/or modify 5114402Sru it under the terms of the GNU General Public License as published by 6114402Sru the Free Software Foundation; either version 2, or (at your option) 7114402Sru any later version. 8114402Sru 9114402Sru This program is distributed in the hope that it will be useful, 10114402Sru but WITHOUT ANY WARRANTY; without even the implied warranty of 11114402Sru MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12114402Sru GNU General Public License for more details. 13114402Sru 14114402Sru You should have received a copy of the GNU General Public License 15114402Sru along with this program; if not, write to the Free Software Foundation, 16151497Sru Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 17114402Sru 18114402Sru#if HAVE_CONFIG_H 19114402Sru# include <config.h> 20114402Sru#endif 21114402Sru 22114402Sru#ifdef LENGTH_LIMIT 23114402Sru# define STRXCASECMP_FUNCTION strncasecmp 24114402Sru# define STRXCASECMP_DECLARE_N , size_t n 25114402Sru# define LENGTH_LIMIT_EXPR(Expr) Expr 26114402Sru#else 27114402Sru# define STRXCASECMP_FUNCTION strcasecmp 28114402Sru# define STRXCASECMP_DECLARE_N /* empty */ 29114402Sru# define LENGTH_LIMIT_EXPR(Expr) 0 30114402Sru#endif 31114402Sru 32151497Sru#include <stddef.h> 33114402Sru#include <ctype.h> 34114402Sru 35114402Sru#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) 36114402Sru 37114402Sru/* Compare {{no more than N characters of }}strings S1 and S2, 38114402Sru ignoring case, returning less than, equal to or 39114402Sru greater than zero if S1 is lexicographically less 40114402Sru than, equal to or greater than S2. */ 41114402Sru 42114402Sruint 43114402SruSTRXCASECMP_FUNCTION (const char *s1, const char *s2 STRXCASECMP_DECLARE_N) 44114402Sru{ 45114402Sru register const unsigned char *p1 = (const unsigned char *) s1; 46114402Sru register const unsigned char *p2 = (const unsigned char *) s2; 47114402Sru unsigned char c1, c2; 48114402Sru 49114402Sru if (p1 == p2 || LENGTH_LIMIT_EXPR (n == 0)) 50114402Sru return 0; 51114402Sru 52114402Sru do 53114402Sru { 54114402Sru c1 = TOLOWER (*p1); 55114402Sru c2 = TOLOWER (*p2); 56114402Sru 57114402Sru if (LENGTH_LIMIT_EXPR (--n == 0) || c1 == '\0') 58114402Sru break; 59114402Sru 60114402Sru ++p1; 61114402Sru ++p2; 62114402Sru } 63114402Sru while (c1 == c2); 64114402Sru 65114402Sru return c1 - c2; 66114402Sru} 67