Header And Logo

PostgreSQL
| The world's most advanced open source database.

memcmp.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * memcmp.c
00004  *    compares memory bytes
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  *
00008  *
00009  * IDENTIFICATION
00010  *    src/port/memcmp.c
00011  *
00012  * This file was taken from NetBSD and is used by SunOS because memcmp
00013  * on that platform does not properly compare negative bytes. The
00014  * NetBSD copyright terms follow.
00015  */
00016 
00017 /*-
00018  * Copyright (c) 1990, 1993
00019  *  The Regents of the University of California.  All rights reserved.
00020  *
00021  * This code is derived from software contributed to Berkeley by
00022  * Chris Torek.
00023  *
00024  * Redistribution and use in source and binary forms, with or without
00025  * modification, are permitted provided that the following conditions
00026  * are met:
00027  * 1. Redistributions of source code must retain the above copyright
00028  *    notice, this list of conditions and the following disclaimer.
00029  * 2. Redistributions in binary form must reproduce the above copyright
00030  *    notice, this list of conditions and the following disclaimer in the
00031  *    documentation and/or other materials provided with the distribution.
00032  * 3. Neither the name of the University nor the names of its contributors
00033  *    may be used to endorse or promote products derived from this software
00034  *    without specific prior written permission.
00035  *
00036  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00037  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00038  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00039  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00040  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00041  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00042  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00043  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00044  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00045  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00046  * SUCH DAMAGE.
00047  */
00048 
00049 #include "c.h"
00050 
00051 
00052 /*
00053  * Compare memory regions.
00054  */
00055 int
00056 memcmp(const void *s1, const void *s2, size_t n)
00057 {
00058     if (n != 0)
00059     {
00060         const unsigned char *p1 = s1,
00061                    *p2 = s2;
00062 
00063         do
00064         {
00065             if (*p1++ != *p2++)
00066                 return (*--p1 - *--p2);
00067         } while (--n != 0);
00068     }
00069     return 0;
00070 }