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