File: memcmp.c

package info (click to toggle)
inn2 2.4.5-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,912 kB
  • ctags: 7,860
  • sloc: ansic: 85,104; perl: 11,427; sh: 9,863; makefile: 2,498; yacc: 1,563; python: 298; lex: 252; tcl: 7
file content (42 lines) | stat: -rw-r--r-- 1,166 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*  $Id: memcmp.c 5049 2001-12-12 09:06:00Z rra $
**
**  Replacement for a missing or broken memcmp.
**
**  Written by Russ Allbery <rra@stanford.edu>
**  This work is hereby placed in the public domain by its author.
**
**  Provides the same functionality as the standard library routine memcmp
**  for those platforms that don't have it or where it doesn't work right
**  (such as on SunOS where it can't deal with eight-bit characters).
*/

#include "config.h"
#include <sys/types.h>

/* If we're running the test suite, rename memcmp to avoid conflicts with
   the system version. */
#if TESTING
# define memcmp test_memcmp
int test_memcmp(const void *, const void *, size_t);
#endif

int
memcmp(const void *s1, const void *s2, size_t n)
{
    size_t i;
    const unsigned char *p1, *p2;

    /* It's technically illegal to call memcmp with NULL pointers, but we
       may as well check anyway. */
    if (!s1)
        return !s2 ? 0 : -1;
    if (!s2)
        return 1;

    p1 = (const unsigned char *) s1;
    p2 = (const unsigned char *) s2;
    for (i = 0; i < n; i++, p1++, p2++)
        if (*p1 != *p2)
	    return (int) *p1 - (int) *p2;
    return 0;
}