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 	<<strcmp>>---character string compare
20 
21 INDEX
22 	strcmp
23 
24 SYNOPSIS
25 	#include <string.h>
26 	int strcmp(const char *<[a]>, const char *<[b]>);
27 
28 DESCRIPTION
29 	<<strcmp>> compares the string at <[a]> to
30 	the string at <[b]>.
31 
32 RETURNS
33 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
34 	<<strcmp>> returns a number greater than zero.  If the two
35 	strings match, <<strcmp>> returns zero.  If <<*<[a]>>>
36 	sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a
37 	number less than zero.
38 
39 PORTABILITY
40 <<strcmp>> is ANSI C.
41 
42 <<strcmp>> requires no supporting OS subroutines.
43 
44 QUICKREF
45 	strcmp 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
strcmp(const char * s1,const char * s2)72 strcmp (const char *s1,
73 	const char *s2)
74 {
75 #if (defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)) && !defined(FAST_STRCMP)
76   while (*s1 != '\0' && *s1 == *s2)
77     {
78       s1++;
79       s2++;
80     }
81 
82   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
83 #else
84   unsigned long *a1;
85   unsigned long *a2;
86 
87   /* If s1 or s2 are unaligned, then compare bytes. */
88   if (!UNALIGNED (s1, s2))
89     {
90       /* If s1 and s2 are word-aligned, compare them a word at a time. */
91       a1 = (unsigned long*)s1;
92       a2 = (unsigned long*)s2;
93       while (*a1 == *a2)
94         {
95           /* To get here, *a1 == *a2, thus if we find a null in *a1,
96 	     then the strings must be equal, so return zero.  */
97           if (DETECTNULL (*a1))
98 	    return 0;
99 
100           a1++;
101           a2++;
102         }
103 
104       /* A difference was detected in last few bytes of s1, so search bytewise */
105       s1 = (char*)a1;
106       s2 = (char*)a2;
107     }
108 
109   while (*s1 != '\0' && *s1 == *s2)
110     {
111       s1++;
112       s2++;
113     }
114   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
115 #endif /* not PREFER_SIZE_OVER_SPEED */
116 }
117