1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<strncmp>>---character string compare
20 
21 INDEX
22 	strncmp
23 
24 SYNOPSIS
25 	#include <string.h>
26 	int strncmp(const char *<[a]>, const char * <[b]>, size_t <[length]>);
27 
28 DESCRIPTION
29 	<<strncmp>> compares up to <[length]> characters
30 	from the string at <[a]> to the string at <[b]>.
31 
32 RETURNS
33 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
34 	<<strncmp>> returns a number greater than zero.  If the two
35 	strings are equivalent, <<strncmp>> returns zero.  If <<*<[a]>>>
36 	sorts lexicographically before <<*<[b]>>>, <<strncmp>> returns a
37 	number less than zero.
38 
39 PORTABILITY
40 <<strncmp>> is ANSI C.
41 
42 <<strncmp>> requires no supporting OS subroutines.
43 
44 QUICKREF
45 	strncmp ansi pure
46 */
47 
48 #include <string.h>
49 #include <limits.h>
50 #include <stdint.h>
51 
52 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
53 #define UNALIGNED(X, Y) \
54   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
55 
56 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
57 #if LONG_MAX == 2147483647L
58 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
59 #else
60 #if LONG_MAX == 9223372036854775807L
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66 
67 #ifndef DETECTNULL
68 #error long int is not a 32bit or 64bit byte
69 #endif
70 
71 int
strncmp(const char * s1,const char * s2,size_t n)72 strncmp (const char *s1,
73 	const char *s2,
74 	size_t n)
75 {
76 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
77   if (n == 0)
78     return 0;
79 
80   while (n-- != 0 && *s1 == *s2)
81     {
82       if (n == 0 || *s1 == '\0')
83 	break;
84       s1++;
85       s2++;
86     }
87 
88   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
89 #else
90   unsigned long *a1;
91   unsigned long *a2;
92 
93   if (n == 0)
94     return 0;
95 
96   /* If s1 or s2 are unaligned, then compare bytes. */
97   if (!UNALIGNED (s1, s2))
98     {
99       /* If s1 and s2 are word-aligned, compare them a word at a time. */
100       a1 = (unsigned long*)s1;
101       a2 = (unsigned long*)s2;
102       while (n >= sizeof (long) && *a1 == *a2)
103         {
104           n -= sizeof (long);
105 
106           /* If we've run out of bytes or hit a null, return zero
107 	     since we already know *a1 == *a2.  */
108           if (n == 0 || DETECTNULL (*a1))
109 	    return 0;
110 
111           a1++;
112           a2++;
113         }
114 
115       /* A difference was detected in last few bytes of s1, so search bytewise */
116       s1 = (char*)a1;
117       s2 = (char*)a2;
118     }
119 
120   while (n-- > 0 && *s1 == *s2)
121     {
122       /* If we've run out of bytes or hit a null, return zero
123 	 since we already know *s1 == *s2.  */
124       if (n == 0 || *s1 == '\0')
125 	return 0;
126       s1++;
127       s2++;
128     }
129   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
130 #endif /* not PREFER_SIZE_OVER_SPEED */
131 }
132