1 /* 2 * Copyright (c) 1994-2009 Red Hat, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* This file is copied from newlib-1.19 */ 33 34 /* 35 FUNCTION 36 <<strcmp>>---character string compare 37 38 INDEX 39 strcmp 40 41 ANSI_SYNOPSIS 42 #include <string.h> 43 int strcmp(const char *<[a]>, const char *<[b]>); 44 45 TRAD_SYNOPSIS 46 #include <string.h> 47 int strcmp(<[a]>, <[b]>) 48 char *<[a]>; 49 char *<[b]>; 50 51 DESCRIPTION 52 <<strcmp>> compares the string at <[a]> to 53 the string at <[b]>. 54 55 RETURNS 56 If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>, 57 <<strcmp>> returns a number greater than zero. If the two 58 strings match, <<strcmp>> returns zero. If <<*<[a]>>> 59 sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a 60 number less than zero. 61 62 PORTABILITY 63 <<strcmp>> is ANSI C. 64 65 <<strcmp>> requires no supporting OS subroutines. 66 67 QUICKREF 68 strcmp ansi pure 69 */ 70 71 #include "_ansi.h" 72 #include <string.h> 73 #include <limits.h> 74 75 /* Nonzero if either X or Y is not aligned on a "long" boundary. */ 76 #define UNALIGNED(X, Y) \ 77 (((long)X & (sizeof(long) - 1)) | ((long)Y & (sizeof(long) - 1))) 78 79 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */ 80 #if LONG_MAX == 2147483647L 81 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080) 82 #else 83 #if LONG_MAX == 9223372036854775807L 84 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080) 85 #else 86 #error long int is not a 32bit or 64bit type. 87 #endif 88 #endif 89 90 #ifndef DETECTNULL 91 #error long int is not a 32bit or 64bit byte 92 #endif 93 94 int _DEFUN(strcmp, (s1, s2), _CONST char *s1 _AND _CONST char *s2) 95 { 96 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) 97 while (*s1 != '\0' && *s1 == *s2) { 98 s1++; 99 s2++; 100 } 101 102 return (*(unsigned char *)s1) - (*(unsigned char *)s2); 103 #else 104 unsigned long *a1; 105 unsigned long *a2; 106 107 /* If s1 or s2 are unaligned, then compare bytes. */ 108 if (!UNALIGNED(s1, s2)) { 109 /* 110 * If s1 and s2 are word-aligned, compare them a word at a time. 111 */ 112 a1 = (unsigned long *)s1; 113 a2 = (unsigned long *)s2; 114 while (*a1 == *a2) { 115 /* 116 * To get here, *a1 == *a2, thus if we find a null in 117 * *a1, then the strings must be equal, so return zero. 118 */ 119 if (DETECTNULL(*a1)) 120 return 0; 121 122 a1++; 123 a2++; 124 } 125 126 /* 127 * A difference was detected in last few bytes of s1, so search 128 * bytewise. 129 */ 130 s1 = (char *)a1; 131 s2 = (char *)a2; 132 } 133 134 while (*s1 != '\0' && *s1 == *s2) { 135 s1++; 136 s2++; 137 } 138 return (*(unsigned char *)s1) - (*(unsigned char *)s2); 139 #endif /* not PREFER_SIZE_OVER_SPEED */ 140 } 141