File: strings.c

package info (click to toggle)
rxvt 1%3A2.7.10-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,068 kB
  • ctags: 4,415
  • sloc: ansic: 23,997; sh: 6,606; makefile: 649; perl: 33
file content (308 lines) | stat: -rw-r--r-- 7,059 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*--------------------------------*-C-*---------------------------------*
 * File:	strings.c
 *----------------------------------------------------------------------*
 * $Id: strings.c,v 1.14 2001/07/27 06:38:37 gcw Exp $
 *
 * All portions of code are copyright by their respective author/s.
 * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------*/

#include "../config.h"		/* NECESSARY */
#include "rxvt.h"		/* NECESSARY */
#include "strings.intpro"	/* PROTOS for internal routines */

#ifndef NO_STDLIB
/*----------------------------------------------------------------------*/
/*
 * a replacement for strcasecmp() to avoid linking an entire library.
 * Mark Olesen added this in 2.15 but for which OS & library? - Geoff Wing
 */
/* EXTPROTO */
int
strcasecmp(const char *s1, const char *s2)
{
    for ( ; tolower(*s1) == tolower(*s2); s1++, s2++)
	if (!*s1)
	    return 0;
    return (int)(tolower(*s1) - tolower(*s2));
}

/* EXTPROTO */
int
strncasecmp(const char *s1, const char *s2, size_t n)
{
    for ( ; n-- && (tolower(*s1) == tolower(*s2)); s1++, s2++)
	if (!*s1)
	    return 0;
    if (n == 0)
	return 0;
    return (int)(tolower(*s1) - tolower(*s2));
}

/* EXTPROTO */
char           *
strcpy(char *d, const char *s)
{
    char          *r = d;

    for ( ; (*r++ = *s++) != '\0'; ) ;
    return d;
}

/* EXTPROTO */
char           *
strncpy(char *d, const char *s, size_t len)
{
    char          *r = d;

    if (len)
	for ( ; len; len--)
	    if ((*r++ = *s++) == '\0') {
		for ( ; --len; )
		    *r++ = '\0';
		break;
	    }
    return d;
}

/* EXTPROTO */
int
strcmp(const char *s1, const char *s2)
{
    for ( ; (*s1 == *s2++); )
	if (*s1++ == '\0')
	    return 0;
    return (int) ((unsigned char) *s1 - (unsigned char) *--s2);
}

/* EXTPROTO */
int
strncmp(const char *s1, const char *s2, size_t len)
{
    if (len) {
	for ( ; len-- && (*s1++ == *s2++); ) ;
	if (++len)
	    return (int) ((unsigned char) *--s1 - (unsigned char) *--s2);
    }
    return 0;
}

/* EXTPROTO */
char           *
strcat(char *s1, const char *s2)
{
    char           *r = s1;

    if (*r != '\0')
	for ( ; *++r != '\0'; ) ;
    for ( ; (*r++ = *s2++) != '\0'; ) ;

    return s1;
}

/* EXTPROTO */
char           *
strncat(char *s1, const char *s2, size_t len)
{
    char           *r = s1;

    if (*r != '\0')
	for ( ; *++r != '\0'; ) ;
    for ( ; len-- && ((*r++ = *s2++) != '\0'); ) ;
    *r = '\0';

    return s1;
}

/* EXTPROTO */
size_t
strlen(const char *s)
{
    size_t         len = 0;

    for ( ; *s++ != '\0'; len++) ;
    return len;
}

/* EXTPROTO */
char           *
strdup(const char *s)
{
    size_t         len = STRLEN(s) + 1;
    char          *c;

    if ((c = malloc(len)) != NULL)
	MEMCPY(c, s, len);
    return c;
}

/* EXTPROTO */
char           *
index(const char *s, int c)
{
    return STRCHR(s, c);
}

/* EXTPROTO */
char           *
strchr(const char *s, int c)
{
    char          *p = NULL;

    for (;;) {
	if (*s == (char)c) {
	    p = (char *)s;
	    break;
	}
	if (*s++ == '\0')
	    break;
    }
    return p;

}

/* EXTPROTO */
char           *
rindex(const char *s, int c)
{
    return STRRCHR(s, c);
}

/* EXTPROTO */
char           *
strrchr(const char *s, int c)
{
    char          *p = NULL;

    for (;;) {
	if (*s == (char)c)
	    p = (char *)s;
	if (*s++ == '\0')
	    break;
    }
    return p;
}

/* EXTPROTO */
void           *
memcpy(void *s1, const void *s2, size_t len)
{
    /* has extra stack and time but less code space */
    return MEMMOVE(s1, s2, len);
}

/*--------------------------------------------------------------------------*
 * Possibly faster memmove() by Geoff Wing <mason@primenet.com.au>
 *--------------------------------------------------------------------------*/
/* EXTPROTO */
void           *
memmove(void *d, const void *s, size_t len)
{
    u_intp_t        i;
    unsigned char  *dst = (unsigned char *)d;
    const unsigned char *src = (const unsigned char *)s;

    if (len && d != s) {
	if ((u_intp_t)d < (u_intp_t)s) {
	/* forwards */
	    i = (-(u_intp_t)dst) & (SIZEOF_INT_P - 1);
	    if (len >= 16 && i == ((-(u_intp_t)src) & (SIZEOF_INT_P - 1))) {
	    /* speed up since src & dst are offset correctly */
		len -= (size_t)i;
		for ( ; i--; )
		    *dst++ = *src++;
		for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; )
		    *((u_intp_t *)dst)++ = *((const u_intp_t *)src)++;
		len &= (SIZEOF_INT_P - 1);
	    }
	    for ( ; len--; )
		*dst++ = *src++;
	} else {
	/* backwards */
	    dst += len;
	    src += len;
	    i = ((u_intp_t)dst) & (SIZEOF_INT_P - 1);
	    if (len >= 16 && i == (((u_intp_t)src) & (SIZEOF_INT_P - 1))) {
	    /* speed up since src & dst are offset correctly */
		len -= (size_t)i;
		for ( ; i--; )
		    *--dst = *--src;
		for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; )
		    *--((u_intp_t *)dst) = *--((const u_intp_t *)src);
		len &= (SIZEOF_INT_P - 1);
	    }
	    for ( ; len--; )
		*--dst = *--src;
	}
    }
    return d;
}

/*--------------------------------------------------------------------------*
 * Possibly faster memset() by Geoff Wing <mason@primenet.com.au>
 * presumptions:
 *   1) intp_t write the best
 *   2) SIZEOF_INT_P == power of 2
 *--------------------------------------------------------------------------*/

/* EXTPROTO */
void
bzero(void *b, size_t len)
{
    MEMSET(b, 0, len);
}

/* EXTPROTO */
void           *
memset(void *p, int c1, size_t len)
{
    u_intp_t        i, val;
    unsigned char   c = (unsigned char) c1;
    unsigned char  *lp = (unsigned char *) p;

    if (len) {
	if (len >= 16) { /* < 16 probably not worth all the calculations */
/* write out preceding characters so we align on an integer boundary */
	    if ((i = ((-(u_intp_t)p) & (SIZEOF_INT_P - 1)))) {
		len -= (size_t)i;
		for (; i--;)
		    *lp++ = c;
	    }

/* do the fast writing */
	    val = (c << 8) + c;
#if SIZEOF_INT_P >= 4
	    val |= (val << 16);
#endif
#if SIZEOF_INT_P >= 8
	    val |= (val << 32);
#endif
#if SIZEOF_INT_P == 16
	    val |= (val << 64);
#endif
	    for (i = (u_intp_t)(len / SIZEOF_INT_P); i--;)
		*((u_intp_t *)lp)++ = val;
	    len &= (SIZEOF_INT_P - 1);
	}
/* write trailing characters */
	for (; len--;)
	    *lp++ = c;
    }
    return p;
}
#endif
/*----------------------- end-of-file (C source) -----------------------*/