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