00001 /* 00002 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009 00003 * The President and Fellows of Harvard College. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the University nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 */ 00029 00030 /* 00031 * This file is shared between libc and the kernel, so don't put anything 00032 * in here that won't work in both contexts. 00033 */ 00034 00035 #ifdef _KERNEL 00036 #include <types.h> 00037 #include <lib.h> 00038 #else 00039 #include <string.h> 00040 #endif 00041 00042 /* 00043 * Standard C string function: compare two strings and return their 00044 * sort order. 00045 */ 00046 00047 int 00048 strcmp(const char *a, const char *b) 00049 { 00050 size_t i; 00051 00052 /* 00053 * Walk down both strings until either they're different 00054 * or we hit the end of A. 00055 * 00056 * If A and B strings are not the same length, when the 00057 * shorter one ends, the two will be different, and we'll 00058 * stop before running off the end of either. 00059 * 00060 * If they *are* the same length, it's sufficient to check 00061 * that we haven't run off the end of A, because that's the 00062 * same as checking to make sure we haven't run off the end of 00063 * B. 00064 */ 00065 00066 for (i=0; a[i]!=0 && a[i]==b[i]; i++) { 00067 /* nothing */ 00068 } 00069 00070 /* 00071 * If A is greater than B, return 1. If A is less than B, 00072 * return -1. If they're the same, return 0. Since we have 00073 * stopped at the first character of difference (or the end of 00074 * both strings) checking the character under I accomplishes 00075 * this. 00076 * 00077 * Note that strcmp does not handle accented characters, 00078 * internationalization, or locale sort order; strcoll() does 00079 * that. 00080 * 00081 * The rules say we compare order in terms of *unsigned* char. 00082 */ 00083 if ((unsigned char)a[i] > (unsigned char)b[i]) { 00084 return 1; 00085 } 00086 else if (a[i] == b[i]) { 00087 return 0; 00088 } 00089 return -1; 00090 }