Back to index

cell-binutils  2.17cvs20070401
strncmp.c
Go to the documentation of this file.
00001 /* strncmp -- compare two strings, stop after n bytes.
00002    This function is in the public domain.  */
00003 
00004 /*
00005 
00006 @deftypefn Supplemental int strncmp (const char *@var{s1}, const char *@var{s2}, size_t @var{n})
00007 
00008 Compares the first @var{n} bytes of two strings, returning a value as
00009 @code{strcmp}.
00010 
00011 @end deftypefn
00012 
00013 */
00014 
00015 #include <ansidecl.h>
00016 #include <stddef.h>
00017 
00018 int
00019 strncmp(const char *s1, const char *s2, register size_t n)
00020 {
00021   register unsigned char u1, u2;
00022 
00023   while (n-- > 0)
00024     {
00025       u1 = (unsigned char) *s1++;
00026       u2 = (unsigned char) *s2++;
00027       if (u1 != u2)
00028        return u1 - u2;
00029       if (u1 == '\0')
00030        return 0;
00031     }
00032   return 0;
00033 }