1 /* $Id: memcmp.c 5049 2001-12-12 09:06:00Z rra $
3 ** Replacement for a missing or broken memcmp.
5 ** Written by Russ Allbery <rra@stanford.edu>
6 ** This work is hereby placed in the public domain by its author.
8 ** Provides the same functionality as the standard library routine memcmp
9 ** for those platforms that don't have it or where it doesn't work right
10 ** (such as on SunOS where it can't deal with eight-bit characters).
14 #include <sys/types.h>
16 /* If we're running the test suite, rename memcmp to avoid conflicts with
17 the system version. */
19 # define memcmp test_memcmp
20 int test_memcmp(const void *, const void *, size_t);
24 memcmp(const void *s1, const void *s2, size_t n)
27 const unsigned char *p1, *p2;
29 /* It's technically illegal to call memcmp with NULL pointers, but we
30 may as well check anyway. */
36 p1 = (const unsigned char *) s1;
37 p2 = (const unsigned char *) s2;
38 for (i = 0; i < n; i++, p1++, p2++)
40 return (int) *p1 - (int) *p2;