Back to index

cell-binutils  2.17cvs20070401
memcmp.c
Go to the documentation of this file.
00001 /* memcmp -- compare two memory regions.
00002    This function is in the public domain.  */
00003 
00004 /*
00005 
00006 @deftypefn Supplemental int memcmp (const void *@var{x}, const void *@var{y}, size_t @var{count})
00007 
00008 Compares the first @var{count} bytes of two areas of memory.  Returns
00009 zero if they are the same, a value less than zero if @var{x} is
00010 lexically less than @var{y}, or a value greater than zero if @var{x}
00011 is lexically greater than @var{y}.  Note that lexical order is determined
00012 as if comparing unsigned char arrays.
00013 
00014 @end deftypefn
00015 
00016 */
00017 
00018 #include <ansidecl.h>
00019 #include <stddef.h>
00020 
00021 int
00022 memcmp (const PTR str1, const PTR str2, size_t count)
00023 {
00024   register const unsigned char *s1 = (const unsigned char*)str1;
00025   register const unsigned char *s2 = (const unsigned char*)str2;
00026 
00027   while (count-- > 0)
00028     {
00029       if (*s1++ != *s2++)
00030          return s1[-1] < s2[-1] ? -1 : 1;
00031     }
00032   return 0;
00033 }
00034