00001 /* strncasecmp.c -- case insensitive string comparator 00002 Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc. 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as published by 00006 the Free Software Foundation; either version 2.1, or (at your option) 00007 any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public License 00015 along with this program; if not, write to the Free Software Foundation, 00016 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 00017 00018 #ifdef HAVE_CONFIG_H 00019 # include <config.h> 00020 #endif 00021 00022 /* Specification. */ 00023 #include "strcase.h" 00024 00025 #include <ctype.h> 00026 #include <limits.h> 00027 00028 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) 00029 00030 /* Compare no more than N bytes of strings S1 and S2, 00031 ignoring case, returning less than, equal to or 00032 greater than zero if S1 is lexicographically less 00033 than, equal to or greater than S2. */ 00034 00035 int 00036 strncasecmp (const char *s1, const char *s2, size_t n) 00037 { 00038 register const unsigned char *p1 = (const unsigned char *) s1; 00039 register const unsigned char *p2 = (const unsigned char *) s2; 00040 unsigned char c1, c2; 00041 00042 if (p1 == p2 || n == 0) 00043 return 0; 00044 00045 do 00046 { 00047 c1 = TOLOWER (*p1); 00048 c2 = TOLOWER (*p2); 00049 00050 if (--n == 0 || c1 == '\0') 00051 break; 00052 00053 ++p1; 00054 ++p2; 00055 } 00056 while (c1 == c2); 00057 00058 if (UCHAR_MAX <= INT_MAX) 00059 return c1 - c2; 00060 else 00061 /* On machines where 'char' and 'int' are types of the same size, the 00062 difference of two 'unsigned char' values - including the sign bit - 00063 doesn't fit in an 'int'. */ 00064 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0); 00065 }