0
|
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 <string.h>
|
|
28 #define ALIGNED(x) (!(((unsigned long) (x)) & (sizeof (unsigned long) - 1)))
|
|
29
|
|
30 #define MAGIC 0x7efefeff
|
|
31 #define HIGH_BIT_P(c) ((c) & hi_bit)
|
|
32 #define HAS_ZERO(c) (((((c) + magic) ^ (c)) & not_magic) != not_magic)
|
|
33
|
|
34 int
|
|
35 strcmp (const char *x, const char *y)
|
|
36 {
|
|
37 if (x == y)
|
|
38 return 0;
|
|
39 else if (ALIGNED (x) && ALIGNED (y))
|
|
40 {
|
|
41 const unsigned long *x1 = (const unsigned long *) x;
|
|
42 const unsigned long *y1 = (const unsigned long *) y;
|
|
43 unsigned long c;
|
|
44 unsigned long magic = MAGIC;
|
|
45 unsigned long not_magic = ~magic;
|
|
46 unsigned long hi_bit = 0x80000000;
|
|
47
|
|
48 while ((c = *x1) == *y1)
|
|
49 {
|
|
50 if (HAS_ZERO(c))
|
|
51 {
|
|
52 if (!HIGH_BIT_P (c))
|
|
53 return 0;
|
|
54 else
|
|
55 {
|
|
56 x = (const char *) x1;
|
|
57 y = (const char *) y1;
|
|
58 goto slow_loop;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 x1++;
|
|
63 y1++;
|
|
64 }
|
|
65
|
|
66 x = (const char *) x1;
|
|
67 y = (const char *) y1;
|
|
68 goto slow_loop;
|
|
69 }
|
|
70 else
|
|
71 {
|
|
72 char c;
|
|
73
|
|
74 slow_loop:
|
|
75
|
|
76 while ((c = *x) == *y)
|
|
77 {
|
|
78 if (c == (char) 0) return 0;
|
|
79 x++;
|
|
80 y++;
|
|
81 }
|
|
82 return (*x - *y);
|
|
83 }
|
|
84 }
|
|
85
|
|
86
|
|
87 int
|
|
88 strncmp (const char *x, const char *y, size_t n)
|
|
89 {
|
|
90 if ((x == y) || (n <= 0))
|
|
91 return 0;
|
|
92 else if (ALIGNED (x) && ALIGNED (y))
|
|
93 {
|
|
94 const unsigned long *x1 = (const unsigned long *) x;
|
|
95 const unsigned long *y1 = (const unsigned long *) y;
|
|
96 unsigned long c;
|
|
97 unsigned long magic = MAGIC;
|
|
98 unsigned long not_magic = ~magic;
|
|
99 unsigned long hi_bit = 0x80000000;
|
|
100
|
|
101 while ((c = *x1) == *y1)
|
|
102 {
|
|
103 n -= sizeof (unsigned long);
|
|
104 if (n <= 0)
|
|
105 return 0;
|
|
106
|
|
107 if (HAS_ZERO(c))
|
|
108 {
|
|
109 if (!HIGH_BIT_P (c))
|
|
110 return 0;
|
|
111 else
|
|
112 {
|
|
113 x = (const char *) x1;
|
|
114 y = (const char *) y1;
|
|
115 goto slow_loop;
|
|
116 }
|
|
117 }
|
|
118
|
|
119 x1++;
|
|
120 y1++;
|
|
121 }
|
|
122
|
|
123 x = (const char *) x1;
|
|
124 y = (const char *) y1;
|
|
125 goto slow_loop;
|
|
126 }
|
|
127 else
|
|
128 {
|
|
129 char c;
|
|
130
|
|
131 slow_loop:
|
|
132
|
|
133 while ((c = *x) == *y)
|
|
134 {
|
|
135 n--;
|
|
136 if (n <= 0)
|
|
137 return 0;
|
|
138 if (c == (char) 0)
|
|
139 return 0;
|
|
140 x++;
|
|
141 y++;
|
|
142 }
|
|
143 return (*x - *y);
|
|
144 }
|
|
145 }
|