xref: /optee_os/lib/libutils/isoc/newlib/strncmp.c (revision bc420748bfc44a9e09000a3966fc59e9e0219df4)
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) - 0x01010101) & ~(X) & 0x80808080)
81 #else
82 #if LONG_MAX == 9223372036854775807L
83 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
84 #else
85 #error long int is not a 32bit or 64bit type.
86 #endif
87 #endif
88 
89 #ifndef DETECTNULL
90 #error long int is not a 32bit or 64bit byte
91 #endif
92 
93 int
94 _DEFUN (strncmp, (s1, s2, n),
95 	_CONST char *s1 _AND
96 	_CONST char *s2 _AND
97 	size_t n)
98 {
99 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
100   if (n == 0)
101     return 0;
102 
103   while (n-- != 0 && *s1 == *s2)
104     {
105       if (n == 0 || *s1 == '\0')
106 	break;
107       s1++;
108       s2++;
109     }
110 
111   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
112 #else
113   unsigned long *a1;
114   unsigned long *a2;
115 
116   if (n == 0)
117     return 0;
118 
119   /* If s1 or s2 are unaligned, then compare bytes. */
120   if (!UNALIGNED (s1, s2))
121     {
122       /* If s1 and s2 are word-aligned, compare them a word at a time. */
123       a1 = (unsigned long*)s1;
124       a2 = (unsigned long*)s2;
125       while (n >= sizeof (long) && *a1 == *a2)
126         {
127           n -= sizeof (long);
128 
129           /* If we've run out of bytes or hit a null, return zero
130 	     since we already know *a1 == *a2.  */
131           if (n == 0 || DETECTNULL (*a1))
132 	    return 0;
133 
134           a1++;
135           a2++;
136         }
137 
138       /* A difference was detected in last few bytes of s1, so search bytewise */
139       s1 = (char*)a1;
140       s2 = (char*)a2;
141     }
142 
143   while (n-- > 0 && *s1 == *s2)
144     {
145       /* If we've run out of bytes or hit a null, return zero
146 	 since we already know *s1 == *s2.  */
147       if (n == 0 || *s1 == '\0')
148 	return 0;
149       s1++;
150       s2++;
151     }
152   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
153 #endif /* not PREFER_SIZE_OVER_SPEED */
154 }
155