Back to index

glibc  2.9
strncmp.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1996, 1997, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library 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 GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <string.h>
00020 #include <memcopy.h>
00021 
00022 #undef strncmp
00023 
00024 /* Compare no more than N characters of S1 and S2,
00025    returning less than, equal to or greater than zero
00026    if S1 is lexicographically less than, equal to or
00027    greater than S2.  */
00028 int
00029 strncmp (s1, s2, n)
00030      const char *s1;
00031      const char *s2;
00032      size_t n;
00033 {
00034   unsigned reg_char c1 = '\0';
00035   unsigned reg_char c2 = '\0';
00036 
00037   if (n >= 4)
00038     {
00039       size_t n4 = n >> 2;
00040       do
00041        {
00042          c1 = (unsigned char) *s1++;
00043          c2 = (unsigned char) *s2++;
00044          if (c1 == '\0' || c1 != c2)
00045            return c1 - c2;
00046          c1 = (unsigned char) *s1++;
00047          c2 = (unsigned char) *s2++;
00048          if (c1 == '\0' || c1 != c2)
00049            return c1 - c2;
00050          c1 = (unsigned char) *s1++;
00051          c2 = (unsigned char) *s2++;
00052          if (c1 == '\0' || c1 != c2)
00053            return c1 - c2;
00054          c1 = (unsigned char) *s1++;
00055          c2 = (unsigned char) *s2++;
00056          if (c1 == '\0' || c1 != c2)
00057            return c1 - c2;
00058        } while (--n4 > 0);
00059       n &= 3;
00060     }
00061 
00062   while (n > 0)
00063     {
00064       c1 = (unsigned char) *s1++;
00065       c2 = (unsigned char) *s2++;
00066       if (c1 == '\0' || c1 != c2)
00067        return c1 - c2;
00068       n--;
00069     }
00070 
00071   return c1 - c2;
00072 }
00073 libc_hidden_builtin_def (strncmp)