1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com>
7 * Copyright (c) 2004 Marc Lehmann <pcg@goof.com>
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.
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.
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 *----------------------------------------------------------------------*/
24 #include "../config.h" /* NECESSARY */
25 #include "rxvt.h" /* NECESSARY */
28 /*----------------------------------------------------------------------*/
30 * a replacement for strcasecmp () to avoid linking an entire library.
31 * Mark Olesen added this in 2.15 but for which OS & library? - Geoff Wing
34 strcasecmp (const char *s1, const char *s2)
36 for ( ; tolower (*s1) == tolower (*s2); s1++, s2++)
39 return (int) (tolower (*s1) - tolower (*s2));
43 strncasecmp (const char *s1, const char *s2, size_t n)
45 for ( ; n-- && (tolower (*s1) == tolower (*s2)); s1++, s2++)
50 return (int) (tolower (*s1) - tolower (*s2));
54 strcpy (char *d, const char *s)
58 for ( ; (*r++ = *s++) != '\0'; ) ;
63 strncpy (char *d, const char *s, size_t len)
69 if ((*r++ = *s++) == '\0')
79 strcmp (const char *s1, const char *s2)
81 for ( ; (*s1 == *s2++); )
84 return (int) ((unsigned char) *s1 - (unsigned char) *--s2);
88 strncmp (const char *s1, const char *s2, size_t len)
92 for ( ; len-- && (*s1++ == *s2++); ) ;
94 return (int) ((unsigned char) *--s1 - (unsigned char) *--s2);
100 strcat (char *s1, const char *s2)
105 for ( ; *++r != '\0'; ) ;
106 for ( ; (*r++ = *s2++) != '\0'; ) ;
112 strncat (char *s1, const char *s2, size_t len)
117 for ( ; *++r != '\0'; ) ;
118 for ( ; len-- && ((*r++ = *s2++) != '\0'); ) ;
125 strlen (const char *s)
129 for ( ; *s++ != '\0'; len++) ;
134 strdup (const char *s)
136 size_t len = strlen (s) + 1;
139 if ((c = malloc (len)) != NULL)
145 index (const char *s, int c)
147 return strchr (s, c);
151 strchr (const char *s, int c)
170 rindex (const char *s, int c)
172 return strrchr (s, c);
176 strrchr (const char *s, int c)
191 memcpy (void *s1, const void *s2, size_t len)
193 /* has extra stack and time but less code space */
194 return memmove (s1, s2, len);
197 /*--------------------------------------------------------------------------*
198 * Possibly faster memmove () by Geoff Wing <mason@primenet.com.au>
199 *--------------------------------------------------------------------------*/
201 memmove (void *d, const void *s, size_t len)
204 unsigned char *dst = (unsigned char *)d;
205 const unsigned char *src = (const unsigned char *)s;
209 if ((u_intp_t)d < (u_intp_t)s)
212 i = (- (u_intp_t)dst) & (SIZEOF_INT_P - 1);
213 if (len >= 16 && i == ((- (u_intp_t)src) & (SIZEOF_INT_P - 1)))
215 /* speed up since src & dst are offset correctly */
219 for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; )
220 * ((u_intp_t *)dst)++ = * ((const u_intp_t *)src)++;
221 len &= (SIZEOF_INT_P - 1);
231 i = ((u_intp_t)dst) & (SIZEOF_INT_P - 1);
232 if (len >= 16 && i == (((u_intp_t)src) & (SIZEOF_INT_P - 1)))
234 /* speed up since src & dst are offset correctly */
238 for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; )
239 *-- ((u_intp_t *)dst) = *-- ((const u_intp_t *)src);
240 len &= (SIZEOF_INT_P - 1);
249 /*--------------------------------------------------------------------------*
250 * Possibly faster memset () by Geoff Wing <mason@primenet.com.au>
252 * 1) intp_t write the best
253 * 2) SIZEOF_INT_P == power of 2
254 *--------------------------------------------------------------------------*/
257 bzero (void *b, size_t len)
263 memset (void *p, int c1, size_t len)
266 unsigned char c = (unsigned char) c1;
267 unsigned char *lp = (unsigned char *) p;
272 { /* < 16 probably not worth all the calculations */
273 /* write out preceding characters so we align on an integer boundary */
274 if ((i = ((- (u_intp_t)p) & (SIZEOF_INT_P - 1))))
281 /* do the fast writing */
283 #if SIZEOF_INT_P >= 4
286 #if SIZEOF_INT_P >= 8
289 #if SIZEOF_INT_P == 16
292 for (i = (u_intp_t) (len / SIZEOF_INT_P); i--;)
293 * ((u_intp_t *)lp)++ = val;
294 len &= (SIZEOF_INT_P - 1);
296 /* write trailing characters */
303 /*----------------------- end-of-file (C source) -----------------------*/