Back to index

tetex-bin  3.0
strcasecmp.c
Go to the documentation of this file.
00001 /* strcasecmp.c -- case insensitive string comparator
00002    Copyright (C) 1998, 1999 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 General Public License as published by
00006    the Free Software Foundation; either version 2, 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 General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software Foundation,
00016    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00017 
00018 #if HAVE_CONFIG_H
00019 # include <config.h>
00020 #endif
00021 
00022 #ifdef LENGTH_LIMIT
00023 # define STRXCASECMP_FUNCTION strncasecmp
00024 # define STRXCASECMP_DECLARE_N , size_t n
00025 # define LENGTH_LIMIT_EXPR(Expr) Expr
00026 #else
00027 # define STRXCASECMP_FUNCTION strcasecmp
00028 # define STRXCASECMP_DECLARE_N /* empty */
00029 # define LENGTH_LIMIT_EXPR(Expr) 0
00030 #endif
00031 
00032 #include <stddef.h>
00033 #include <ctype.h>
00034 
00035 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
00036 
00037 /* Compare {{no more than N characters of }}strings S1 and S2,
00038    ignoring case, returning less than, equal to or
00039    greater than zero if S1 is lexicographically less
00040    than, equal to or greater than S2.  */
00041 
00042 int
00043 STRXCASECMP_FUNCTION (const char *s1, const char *s2 STRXCASECMP_DECLARE_N)
00044 {
00045   register const unsigned char *p1 = (const unsigned char *) s1;
00046   register const unsigned char *p2 = (const unsigned char *) s2;
00047   unsigned char c1, c2;
00048 
00049   if (p1 == p2 || LENGTH_LIMIT_EXPR (n == 0))
00050     return 0;
00051 
00052   do
00053     {
00054       c1 = TOLOWER (*p1);
00055       c2 = TOLOWER (*p2);
00056 
00057       if (LENGTH_LIMIT_EXPR (--n == 0) || c1 == '\0')
00058        break;
00059 
00060       ++p1;
00061       ++p2;
00062     }
00063   while (c1 == c2);
00064 
00065   return c1 - c2;
00066 }