Back to index

glibc  2.9
strncase.c
Go to the documentation of this file.
00001 /* Compare at most N characters of two strings without taking care for
00002    the case.
00003    Copyright (C) 1992, 1996, 1997, 2001, 2004 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 # include <config.h>
00023 #endif
00024 
00025 #include <string.h>
00026 #include <ctype.h>
00027 
00028 #ifndef weak_alias
00029 # define __strncasecmp strncasecmp
00030 # define TOLOWER(Ch) tolower (Ch)
00031 #else
00032 # include <locale/localeinfo.h>
00033 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
00034 #  define __strncasecmp __strncasecmp_l
00035 # endif
00036 # define TOLOWER(Ch) __tolower_l ((Ch), loc)
00037 #endif
00038 
00039 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
00040 # define LOCALE_PARAM , loc
00041 # define LOCALE_PARAM_DECL __locale_t loc;
00042 #else
00043 # define LOCALE_PARAM
00044 # define LOCALE_PARAM_DECL
00045 #endif
00046 
00047 /* Compare no more than N characters of S1 and S2,
00048    ignoring case, returning less than, equal to or
00049    greater than zero if S1 is lexicographically less
00050    than, equal to or greater than S2.  */
00051 int
00052 __strncasecmp (s1, s2, n LOCALE_PARAM)
00053      const char *s1;
00054      const char *s2;
00055      size_t n;
00056      LOCALE_PARAM_DECL
00057 {
00058 #if defined _LIBC && !defined USE_IN_EXTENDED_LOCALE_MODEL
00059   __locale_t loc = _NL_CURRENT_LOCALE;
00060 #endif
00061   const unsigned char *p1 = (const unsigned char *) s1;
00062   const unsigned char *p2 = (const unsigned char *) s2;
00063   int result;
00064 
00065   if (p1 == p2 || n == 0)
00066     return 0;
00067 
00068   while ((result = TOLOWER (*p1) - TOLOWER (*p2++)) == 0)
00069     if (*p1++ == '\0' || --n == 0)
00070       break;
00071 
00072   return result;
00073 }
00074 #ifndef __strncasecmp
00075 weak_alias (__strncasecmp, strncasecmp)
00076 #endif