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