xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/memcmp.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
198b9484cSchristos /* memcmp -- compare two memory regions.
298b9484cSchristos    This function is in the public domain.  */
398b9484cSchristos 
498b9484cSchristos /*
598b9484cSchristos 
698b9484cSchristos @deftypefn Supplemental int memcmp (const void *@var{x}, const void *@var{y}, @
798b9484cSchristos   size_t @var{count})
898b9484cSchristos 
998b9484cSchristos Compares the first @var{count} bytes of two areas of memory.  Returns
1098b9484cSchristos zero if they are the same, a value less than zero if @var{x} is
1198b9484cSchristos lexically less than @var{y}, or a value greater than zero if @var{x}
1298b9484cSchristos is lexically greater than @var{y}.  Note that lexical order is determined
1398b9484cSchristos as if comparing unsigned char arrays.
1498b9484cSchristos 
1598b9484cSchristos @end deftypefn
1698b9484cSchristos 
1798b9484cSchristos */
1898b9484cSchristos 
1998b9484cSchristos #include <ansidecl.h>
2098b9484cSchristos #include <stddef.h>
2198b9484cSchristos 
2298b9484cSchristos int
memcmp(const void * str1,const void * str2,size_t count)23*4b169a6bSchristos memcmp (const void *str1, const void *str2, size_t count)
2498b9484cSchristos {
2598b9484cSchristos   register const unsigned char *s1 = (const unsigned char*)str1;
2698b9484cSchristos   register const unsigned char *s2 = (const unsigned char*)str2;
2798b9484cSchristos 
2898b9484cSchristos   while (count-- > 0)
2998b9484cSchristos     {
3098b9484cSchristos       if (*s1++ != *s2++)
3198b9484cSchristos 	  return s1[-1] < s2[-1] ? -1 : 1;
3298b9484cSchristos     }
3398b9484cSchristos   return 0;
3498b9484cSchristos }
3598b9484cSchristos 
36