root/common/libc/string/strcmp.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. strcmp

   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * This file is shared between libc and the kernel, so don't put anything
  32  * in here that won't work in both contexts.
  33  */
  34 
  35 #ifdef _KERNEL
  36 #include <types.h>
  37 #include <lib.h>
  38 #else
  39 #include <string.h>
  40 #endif
  41 
  42 /*
  43  * Standard C string function: compare two strings and return their
  44  * sort order.
  45  */
  46 
  47 int
  48 strcmp(const char *a, const char *b)
  49 {
  50         size_t i;
  51 
  52         /*
  53          * Walk down both strings until either they're different
  54          * or we hit the end of A.
  55          *
  56          * If A and B strings are not the same length, when the
  57          * shorter one ends, the two will be different, and we'll
  58          * stop before running off the end of either.
  59          *
  60          * If they *are* the same length, it's sufficient to check
  61          * that we haven't run off the end of A, because that's the
  62          * same as checking to make sure we haven't run off the end of
  63          * B.
  64          */
  65 
  66         for (i=0; a[i]!=0 && a[i]==b[i]; i++) {
  67                 /* nothing */
  68         }
  69 
  70         /*
  71          * If A is greater than B, return 1. If A is less than B,
  72          * return -1.  If they're the same, return 0. Since we have
  73          * stopped at the first character of difference (or the end of
  74          * both strings) checking the character under I accomplishes
  75          * this.
  76          *
  77          * Note that strcmp does not handle accented characters,
  78          * internationalization, or locale sort order; strcoll() does
  79          * that.
  80          *
  81          * The rules say we compare order in terms of *unsigned* char.
  82          */
  83         if ((unsigned char)a[i] > (unsigned char)b[i]) {
  84                 return 1;
  85         }
  86         else if (a[i] == b[i]) {
  87                 return 0;
  88         }
  89         return -1;
  90 }

/* [<][>][^][v][top][bottom][index][help] */