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 /* 33 FUNCTION 34 <<strncmp>>---character string compare 35 36 INDEX 37 strncmp 38 39 ANSI_SYNOPSIS 40 #include <string.h> 41 int strncmp(const char *<[a]>, const char * <[b]>, size_t <[length]>); 42 43 TRAD_SYNOPSIS 44 #include <string.h> 45 int strncmp(<[a]>, <[b]>, <[length]>) 46 char *<[a]>; 47 char *<[b]>; 48 size_t <[length]> 49 50 DESCRIPTION 51 <<strncmp>> compares up to <[length]> characters 52 from the string at <[a]> to the string at <[b]>. 53 54 RETURNS 55 If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>, 56 <<strncmp>> returns a number greater than zero. If the two 57 strings are equivalent, <<strncmp>> returns zero. If <<*<[a]>>> 58 sorts lexicographically before <<*<[b]>>>, <<strncmp>> returns a 59 number less than zero. 60 61 PORTABILITY 62 <<strncmp>> is ANSI C. 63 64 <<strncmp>> requires no supporting OS subroutines. 65 66 QUICKREF 67 strncmp ansi pure 68 */ 69 70 #include "_ansi.h" 71 #include <string.h> 72 #include <limits.h> 73 74 /* Nonzero if either X or Y is not aligned on a "long" boundary. */ 75 #define UNALIGNED(X, Y) \ 76 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1))) 77 78 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */ 79 #if LONG_MAX == 2147483647L 80 #define DETECTNULL(X) (((X) - 0x01010101L) & ~(X) & 0x80808080UL) 81 #else 82 #if LONG_MAX == 9223372036854775807L 83 #define DETECTNULL(X) (((X) - 0x0101010101010101L) & ~(X) & \ 84 0x8080808080808080UL) 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 95 _DEFUN (strncmp, (s1, s2, n), 96 _CONST char *s1 _AND 97 _CONST char *s2 _AND 98 size_t n) 99 { 100 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) 101 if (n == 0) 102 return 0; 103 104 while (n-- != 0 && *s1 == *s2) 105 { 106 if (n == 0 || *s1 == '\0') 107 break; 108 s1++; 109 s2++; 110 } 111 112 return (*(unsigned char *) s1) - (*(unsigned char *) s2); 113 #else 114 unsigned long *a1; 115 unsigned long *a2; 116 117 if (n == 0) 118 return 0; 119 120 /* If s1 or s2 are unaligned, then compare bytes. */ 121 if (!UNALIGNED (s1, s2)) 122 { 123 /* If s1 and s2 are word-aligned, compare them a word at a time. */ 124 a1 = (unsigned long*)s1; 125 a2 = (unsigned long*)s2; 126 while (n >= sizeof (long) && *a1 == *a2) 127 { 128 n -= sizeof (long); 129 130 /* If we've run out of bytes or hit a null, return zero 131 since we already know *a1 == *a2. */ 132 if (n == 0 || DETECTNULL (*a1)) 133 return 0; 134 135 a1++; 136 a2++; 137 } 138 139 /* A difference was detected in last few bytes of s1, so search bytewise */ 140 s1 = (char*)a1; 141 s2 = (char*)a2; 142 } 143 144 while (n-- > 0 && *s1 == *s2) 145 { 146 /* If we've run out of bytes or hit a null, return zero 147 since we already know *s1 == *s2. */ 148 if (n == 0 || *s1 == '\0') 149 return 0; 150 s1++; 151 s2++; 152 } 153 return (*(unsigned char *) s1) - (*(unsigned char *) s2); 154 #endif /* not PREFER_SIZE_OVER_SPEED */ 155 } 156