comparison src/strcmp.c @ 428:3ecd8885ac67 r21-2-22

Import from CVS: tag r21-2-22
author cvs
date Mon, 13 Aug 2007 11:28:15 +0200
parents
children abe6d1db359e
comparison
equal deleted inserted replaced
427:0a0253eac470 428:3ecd8885ac67
1 /* This file is part of XEmacs.
2
3 XEmacs is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 XEmacs is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with XEmacs; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA. */
17
18 /* Synched up with: Not in FSF. */
19
20 /* In SunOS 4.1.1 the strcmp and strncmp functions reference memory
21 past the last byte of the string! This will core dump if the memory
22 following the last byte is not mapped.
23
24 Here are correct versions by hbs@lucid.com.
25 */
26
27 # include <config.h>
28 # ifndef REGISTER /* Strictly enforced in 20.3 */
29 # define REGISTER
30 # endif
31
32 #include <string.h>
33 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
34
35 #define MAGIC 0x7efefeff
36 #define HIGH_BIT_P(c) ((c) & hi_bit)
37 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
38
39 /* CONST IS LOSING, but const is part of the interface of strcmp */
40 int
41 strcmp (const char *x, const char *y)
42 {
43 if (x == y)
44 return 0;
45 else if (ALIGNED (x) && ALIGNED (y))
46 {
47 CONST unsigned long *x1 = (CONST unsigned long *) x;
48 CONST unsigned long *y1 = (CONST unsigned long *) y;
49 unsigned long c;
50 unsigned long magic = MAGIC;
51 unsigned long not_magic = ~magic;
52 unsigned long hi_bit = 0x80000000;
53
54 while ((c = *x1) == *y1)
55 {
56 if (HAS_ZERO(c))
57 {
58 if (!HIGH_BIT_P (c))
59 return 0;
60 else
61 {
62 x = (CONST char *) x1;
63 y = (CONST char *) y1;
64 goto slow_loop;
65 }
66 }
67
68 x1++;
69 y1++;
70 }
71
72 x = (CONST char *) x1;
73 y = (CONST char *) y1;
74 goto slow_loop;
75 }
76 else
77 {
78 char c;
79
80 slow_loop:
81
82 while ((c = *x) == *y)
83 {
84 if (c == (char) 0) return 0;
85 x++;
86 y++;
87 }
88 return (*x - *y);
89 }
90 }
91
92
93 int
94 strncmp (CONST char *x, CONST char *y, size_t n)
95 {
96 if ((x == y) || (n <= 0))
97 return 0;
98 else if (ALIGNED (x) && ALIGNED (y))
99 {
100 CONST unsigned long *x1 = (CONST unsigned long *) x;
101 CONST unsigned long *y1 = (CONST unsigned long *) y;
102 unsigned long c;
103 unsigned long magic = MAGIC;
104 unsigned long not_magic = ~magic;
105 unsigned long hi_bit = 0x80000000;
106
107 while ((c = *x1) == *y1)
108 {
109 n -= sizeof (unsigned long);
110 if (n <= 0)
111 return 0;
112
113 if (HAS_ZERO(c))
114 {
115 if (!HIGH_BIT_P (c))
116 return 0;
117 else
118 {
119 x = (CONST char *) x1;
120 y = (CONST char *) y1;
121 goto slow_loop;
122 }
123 }
124
125 x1++;
126 y1++;
127 }
128
129 x = (CONST char *) x1;
130 y = (CONST char *) y1;
131 goto slow_loop;
132 }
133 else
134 {
135 char c;
136
137 slow_loop:
138
139 while ((c = *x) == *y)
140 {
141 n--;
142 if (n <= 0)
143 return 0;
144 if (c == (char) 0)
145 return 0;
146 x++;
147 y++;
148 }
149 return (*x - *y);
150 }
151 }