xref: /netbsd-src/external/gpl3/binutils/dist/libiberty/memcmp.c (revision 4f645668ed707e1f969c546666f8c8e45e6f8888)
12a6b7db3Sskrll /* memcmp -- compare two memory regions.
22a6b7db3Sskrll    This function is in the public domain.  */
32a6b7db3Sskrll 
42a6b7db3Sskrll /*
52a6b7db3Sskrll 
6883529b6Schristos @deftypefn Supplemental int memcmp (const void *@var{x}, const void *@var{y}, @
7883529b6Schristos   size_t @var{count})
82a6b7db3Sskrll 
92a6b7db3Sskrll Compares the first @var{count} bytes of two areas of memory.  Returns
102a6b7db3Sskrll zero if they are the same, a value less than zero if @var{x} is
112a6b7db3Sskrll lexically less than @var{y}, or a value greater than zero if @var{x}
122a6b7db3Sskrll is lexically greater than @var{y}.  Note that lexical order is determined
132a6b7db3Sskrll as if comparing unsigned char arrays.
142a6b7db3Sskrll 
152a6b7db3Sskrll @end deftypefn
162a6b7db3Sskrll 
172a6b7db3Sskrll */
182a6b7db3Sskrll 
192a6b7db3Sskrll #include <ansidecl.h>
202a6b7db3Sskrll #include <stddef.h>
212a6b7db3Sskrll 
222a6b7db3Sskrll int
memcmp(const void * str1,const void * str2,size_t count)23*4f645668Schristos memcmp (const void *str1, const void *str2, size_t count)
242a6b7db3Sskrll {
252a6b7db3Sskrll   register const unsigned char *s1 = (const unsigned char*)str1;
262a6b7db3Sskrll   register const unsigned char *s2 = (const unsigned char*)str2;
272a6b7db3Sskrll 
282a6b7db3Sskrll   while (count-- > 0)
292a6b7db3Sskrll     {
302a6b7db3Sskrll       if (*s1++ != *s2++)
312a6b7db3Sskrll 	  return s1[-1] < s2[-1] ? -1 : 1;
322a6b7db3Sskrll     }
332a6b7db3Sskrll   return 0;
342a6b7db3Sskrll }
352a6b7db3Sskrll 
36