1 |
/*--------------------------------*-C-*---------------------------------* |
2 |
* File: strings.c |
3 |
*----------------------------------------------------------------------* |
4 |
* $Id: strings.C,v 1.4 2004/02/01 01:34:41 pcg Exp $ |
5 |
* |
6 |
* All portions of code are copyright by their respective author/s. |
7 |
* Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com> |
8 |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
11 |
* the Free Software Foundation; either version 2 of the License, or |
12 |
* (at your option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the GNU General Public License |
20 |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 |
*----------------------------------------------------------------------*/ |
23 |
|
24 |
#include "../config.h" /* NECESSARY */ |
25 |
#include "rxvt.h" /* NECESSARY */ |
26 |
#include "strings.intpro" /* PROTOS for internal routines */ |
27 |
|
28 |
#ifndef NO_STRINGS |
29 |
/*----------------------------------------------------------------------*/ |
30 |
/* |
31 |
* a replacement for strcasecmp () to avoid linking an entire library. |
32 |
* Mark Olesen added this in 2.15 but for which OS & library? - Geoff Wing |
33 |
*/ |
34 |
int |
35 |
strcasecmp (const char *s1, const char *s2) |
36 |
{ |
37 |
for ( ; tolower (*s1) == tolower (*s2); s1++, s2++) |
38 |
if (!*s1) |
39 |
return 0; |
40 |
return (int) (tolower (*s1) - tolower (*s2)); |
41 |
} |
42 |
|
43 |
int |
44 |
strncasecmp (const char *s1, const char *s2, size_t n) |
45 |
{ |
46 |
for ( ; n-- && (tolower (*s1) == tolower (*s2)); s1++, s2++) |
47 |
if (!*s1) |
48 |
return 0; |
49 |
if (n == 0) |
50 |
return 0; |
51 |
return (int) (tolower (*s1) - tolower (*s2)); |
52 |
} |
53 |
|
54 |
char * |
55 |
strcpy (char *d, const char *s) |
56 |
{ |
57 |
char *r = d; |
58 |
|
59 |
for ( ; (*r++ = *s++) != '\0'; ) ; |
60 |
return d; |
61 |
} |
62 |
|
63 |
char * |
64 |
strncpy (char *d, const char *s, size_t len) |
65 |
{ |
66 |
char *r = d; |
67 |
|
68 |
if (len) |
69 |
for ( ; len; len--) |
70 |
if ((*r++ = *s++) == '\0') |
71 |
{ |
72 |
for ( ; --len; ) |
73 |
*r++ = '\0'; |
74 |
break; |
75 |
} |
76 |
return d; |
77 |
} |
78 |
|
79 |
int |
80 |
strcmp (const char *s1, const char *s2) |
81 |
{ |
82 |
for ( ; (*s1 == *s2++); ) |
83 |
if (*s1++ == '\0') |
84 |
return 0; |
85 |
return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
86 |
} |
87 |
|
88 |
int |
89 |
strncmp (const char *s1, const char *s2, size_t len) |
90 |
{ |
91 |
if (len) |
92 |
{ |
93 |
for ( ; len-- && (*s1++ == *s2++); ) ; |
94 |
if (++len) |
95 |
return (int) ((unsigned char) *--s1 - (unsigned char) *--s2); |
96 |
} |
97 |
return 0; |
98 |
} |
99 |
|
100 |
char * |
101 |
strcat (char *s1, const char *s2) |
102 |
{ |
103 |
char *r = s1; |
104 |
|
105 |
if (*r != '\0') |
106 |
for ( ; *++r != '\0'; ) ; |
107 |
for ( ; (*r++ = *s2++) != '\0'; ) ; |
108 |
|
109 |
return s1; |
110 |
} |
111 |
|
112 |
char * |
113 |
strncat (char *s1, const char *s2, size_t len) |
114 |
{ |
115 |
char *r = s1; |
116 |
|
117 |
if (*r != '\0') |
118 |
for ( ; *++r != '\0'; ) ; |
119 |
for ( ; len-- && ((*r++ = *s2++) != '\0'); ) ; |
120 |
*r = '\0'; |
121 |
|
122 |
return s1; |
123 |
} |
124 |
|
125 |
size_t |
126 |
strlen (const char *s) |
127 |
{ |
128 |
size_t len = 0; |
129 |
|
130 |
for ( ; *s++ != '\0'; len++) ; |
131 |
return len; |
132 |
} |
133 |
|
134 |
char * |
135 |
strdup (const char *s) |
136 |
{ |
137 |
size_t len = STRLEN (s) + 1; |
138 |
char *c; |
139 |
|
140 |
if ((c = malloc (len)) != NULL) |
141 |
MEMCPY (c, s, len); |
142 |
return c; |
143 |
} |
144 |
|
145 |
char * |
146 |
index (const char *s, int c) |
147 |
{ |
148 |
return STRCHR (s, c); |
149 |
} |
150 |
|
151 |
char * |
152 |
strchr (const char *s, int c) |
153 |
{ |
154 |
char *p = NULL; |
155 |
|
156 |
for (;;) |
157 |
{ |
158 |
if (*s == (char)c) |
159 |
{ |
160 |
p = (char *)s; |
161 |
break; |
162 |
} |
163 |
if (*s++ == '\0') |
164 |
break; |
165 |
} |
166 |
return p; |
167 |
|
168 |
} |
169 |
|
170 |
char * |
171 |
rindex (const char *s, int c) |
172 |
{ |
173 |
return STRRCHR (s, c); |
174 |
} |
175 |
|
176 |
char * |
177 |
strrchr (const char *s, int c) |
178 |
{ |
179 |
char *p = NULL; |
180 |
|
181 |
for (;;) |
182 |
{ |
183 |
if (*s == (char)c) |
184 |
p = (char *)s; |
185 |
if (*s++ == '\0') |
186 |
break; |
187 |
} |
188 |
return p; |
189 |
} |
190 |
|
191 |
void * |
192 |
memcpy (void *s1, const void *s2, size_t len) |
193 |
{ |
194 |
/* has extra stack and time but less code space */ |
195 |
return MEMMOVE (s1, s2, len); |
196 |
} |
197 |
|
198 |
/*--------------------------------------------------------------------------* |
199 |
* Possibly faster memmove () by Geoff Wing <mason@primenet.com.au> |
200 |
*--------------------------------------------------------------------------*/ |
201 |
void * |
202 |
memmove (void *d, const void *s, size_t len) |
203 |
{ |
204 |
u_intp_t i; |
205 |
unsigned char *dst = (unsigned char *)d; |
206 |
const unsigned char *src = (const unsigned char *)s; |
207 |
|
208 |
if (len && d != s) |
209 |
{ |
210 |
if ((u_intp_t)d < (u_intp_t)s) |
211 |
{ |
212 |
/* forwards */ |
213 |
i = (- (u_intp_t)dst) & (SIZEOF_INT_P - 1); |
214 |
if (len >= 16 && i == ((- (u_intp_t)src) & (SIZEOF_INT_P - 1))) |
215 |
{ |
216 |
/* speed up since src & dst are offset correctly */ |
217 |
len -= (size_t)i; |
218 |
for ( ; i--; ) |
219 |
*dst++ = *src++; |
220 |
for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; ) |
221 |
* ((u_intp_t *)dst)++ = * ((const u_intp_t *)src)++; |
222 |
len &= (SIZEOF_INT_P - 1); |
223 |
} |
224 |
for ( ; len--; ) |
225 |
*dst++ = *src++; |
226 |
} |
227 |
else |
228 |
{ |
229 |
/* backwards */ |
230 |
dst += len; |
231 |
src += len; |
232 |
i = ((u_intp_t)dst) & (SIZEOF_INT_P - 1); |
233 |
if (len >= 16 && i == (((u_intp_t)src) & (SIZEOF_INT_P - 1))) |
234 |
{ |
235 |
/* speed up since src & dst are offset correctly */ |
236 |
len -= (size_t)i; |
237 |
for ( ; i--; ) |
238 |
*--dst = *--src; |
239 |
for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; ) |
240 |
*-- ((u_intp_t *)dst) = *-- ((const u_intp_t *)src); |
241 |
len &= (SIZEOF_INT_P - 1); |
242 |
} |
243 |
for ( ; len--; ) |
244 |
*--dst = *--src; |
245 |
} |
246 |
} |
247 |
return d; |
248 |
} |
249 |
|
250 |
/*--------------------------------------------------------------------------* |
251 |
* Possibly faster memset () by Geoff Wing <mason@primenet.com.au> |
252 |
* presumptions: |
253 |
* 1) intp_t write the best |
254 |
* 2) SIZEOF_INT_P == power of 2 |
255 |
*--------------------------------------------------------------------------*/ |
256 |
|
257 |
void |
258 |
bzero (void *b, size_t len) |
259 |
{ |
260 |
MEMSET (b, 0, len); |
261 |
} |
262 |
|
263 |
void * |
264 |
memset (void *p, int c1, size_t len) |
265 |
{ |
266 |
u_intp_t i, val; |
267 |
unsigned char c = (unsigned char) c1; |
268 |
unsigned char *lp = (unsigned char *) p; |
269 |
|
270 |
if (len) |
271 |
{ |
272 |
if (len >= 16) |
273 |
{ /* < 16 probably not worth all the calculations */ |
274 |
/* write out preceding characters so we align on an integer boundary */ |
275 |
if ((i = ((- (u_intp_t)p) & (SIZEOF_INT_P - 1)))) |
276 |
{ |
277 |
len -= (size_t)i; |
278 |
for (; i--;) |
279 |
*lp++ = c; |
280 |
} |
281 |
|
282 |
/* do the fast writing */ |
283 |
val = (c << 8) + c; |
284 |
#if SIZEOF_INT_P >= 4 |
285 |
val |= (val << 16); |
286 |
#endif |
287 |
#if SIZEOF_INT_P >= 8 |
288 |
val |= (val << 32); |
289 |
#endif |
290 |
#if SIZEOF_INT_P == 16 |
291 |
val |= (val << 64); |
292 |
#endif |
293 |
for (i = (u_intp_t) (len / SIZEOF_INT_P); i--;) |
294 |
* ((u_intp_t *)lp)++ = val; |
295 |
len &= (SIZEOF_INT_P - 1); |
296 |
} |
297 |
/* write trailing characters */ |
298 |
for (; len--;) |
299 |
*lp++ = c; |
300 |
} |
301 |
return p; |
302 |
} |
303 |
#endif |
304 |
/*----------------------- end-of-file (C source) -----------------------*/ |