Subversion Repositories HelenOS

Rev

Rev 3425 | Rev 4377 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3425 Rev 3437
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
-
 
3
 * Copyright (C) 1998 by Wes Peters <wes@softweyr.com>
-
 
4
 * Copyright (c) 1988, 1993 The Regents of the University of California.
3
 * All rights reserved.
5
 * All rights reserved.
4
 *
6
 *
5
 * Redistribution and use in source and binary forms, with or without
7
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
8
 * modification, are permitted provided that the following conditions
7
 * are met:
9
 * are met:
8
 *
10
 *
9
 * - Redistributions of source code must retain the above copyright
11
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
12
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
13
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
14
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
15
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
16
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
17
 *   derived from this software without specific prior written permission.
16
 *
18
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
29
 */
28
 
30
 
29
/** @addtogroup libc
31
/** @addtogroup libc
30
 * @{
32
 * @{
31
 */
33
 */
32
/** @file
34
/** @file
33
 */
35
 */
34
 
36
 
35
#include <string.h>
37
#include <string.h>
36
#include <unistd.h>
38
#include <unistd.h>
37
#include <ctype.h>
39
#include <ctype.h>
38
#include <limits.h>
40
#include <limits.h>
39
#include <align.h>
41
#include <align.h>
40
#include <sys/types.h>
42
#include <sys/types.h>
41
#include <malloc.h>
43
#include <malloc.h>
42
 
44
 
43
/* Dummy implementation of mem/ functions */
45
/* Dummy implementation of mem/ functions */
44
 
46
 
45
void *memset(void *s, int c, size_t n)
47
void *memset(void *s, int c, size_t n)
46
{
48
{
47
    char *os = s;
49
    char *os = s;
48
   
50
   
49
    while (n--)
51
    while (n--)
50
        *(os++) = c;
52
        *(os++) = c;
51
   
53
   
52
    return s;
54
    return s;
53
}
55
}
54
 
56
 
55
struct along {
57
struct along {
56
    unsigned long n;
58
    unsigned long n;
57
} __attribute__ ((packed));
59
} __attribute__ ((packed));
58
 
60
 
59
static void *unaligned_memcpy(void *dst, const void *src, size_t n)
61
static void *unaligned_memcpy(void *dst, const void *src, size_t n)
60
{
62
{
61
    int i, j;
63
    int i, j;
62
    struct along *adst = dst;
64
    struct along *adst = dst;
63
    const struct along *asrc = src;
65
    const struct along *asrc = src;
64
 
66
 
65
    for (i = 0; i < n / sizeof(unsigned long); i++)
67
    for (i = 0; i < n / sizeof(unsigned long); i++)
66
        adst[i].n = asrc[i].n;
68
        adst[i].n = asrc[i].n;
67
       
69
       
68
    for (j = 0; j < n % sizeof(unsigned long); j++)
70
    for (j = 0; j < n % sizeof(unsigned long); j++)
69
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
71
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
70
            ((unsigned char *) (((unsigned long *) src) + i))[j];
72
            ((unsigned char *) (((unsigned long *) src) + i))[j];
71
       
73
       
72
    return (char *) dst;
74
    return (char *) dst;
73
}
75
}
74
 
76
 
75
void *memcpy(void *dst, const void *src, size_t n)
77
void *memcpy(void *dst, const void *src, size_t n)
76
{
78
{
77
    int i, j;
79
    int i, j;
78
 
80
 
79
    if (((long) dst & (sizeof(long) - 1)) ||
81
    if (((long) dst & (sizeof(long) - 1)) ||
80
        ((long) src & (sizeof(long) - 1)))
82
        ((long) src & (sizeof(long) - 1)))
81
        return unaligned_memcpy(dst, src, n);
83
        return unaligned_memcpy(dst, src, n);
82
 
84
 
83
    for (i = 0; i < n / sizeof(unsigned long); i++)
85
    for (i = 0; i < n / sizeof(unsigned long); i++)
84
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
86
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
85
       
87
       
86
    for (j = 0; j < n % sizeof(unsigned long); j++)
88
    for (j = 0; j < n % sizeof(unsigned long); j++)
87
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
89
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
88
            ((unsigned char *) (((unsigned long *) src) + i))[j];
90
            ((unsigned char *) (((unsigned long *) src) + i))[j];
89
       
91
       
90
    return (char *) dst;
92
    return (char *) dst;
91
}
93
}
92
 
94
 
93
void *memmove(void *dst, const void *src, size_t n)
95
void *memmove(void *dst, const void *src, size_t n)
94
{
96
{
95
    int i, j;
97
    int i, j;
96
   
98
   
97
    if (src > dst)
99
    if (src > dst)
98
        return memcpy(dst, src, n);
100
        return memcpy(dst, src, n);
99
 
101
 
100
    for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
102
    for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
101
        ((unsigned char *) ((unsigned long *) dst))[j] =
103
        ((unsigned char *) ((unsigned long *) dst))[j] =
102
            ((unsigned char *) ((unsigned long *) src))[j];
104
            ((unsigned char *) ((unsigned long *) src))[j];
103
 
105
 
104
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
106
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
105
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
107
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
106
       
108
       
107
    return (char *) dst;
109
    return (char *) dst;
108
}
110
}
109
 
111
 
110
/** Compare two memory areas.
112
/** Compare two memory areas.
111
 *
113
 *
112
 * @param s1        Pointer to the first area to compare.
114
 * @param s1        Pointer to the first area to compare.
113
 * @param s2        Pointer to the second area to compare.
115
 * @param s2        Pointer to the second area to compare.
114
 * @param len       Size of the first area in bytes. Both areas must have
116
 * @param len       Size of the first area in bytes. Both areas must have
115
 *          the same length.
117
 *          the same length.
116
 * @return      If len is 0, return zero. If the areas match, return
118
 * @return      If len is 0, return zero. If the areas match, return
117
 *          zero. Otherwise return non-zero.
119
 *          zero. Otherwise return non-zero.
118
 */
120
 */
119
int bcmp(const char *s1, const char *s2, size_t len)
121
int bcmp(const char *s1, const char *s2, size_t len)
120
{
122
{
121
    for (; len && *s1++ == *s2++; len--)
123
    for (; len && *s1++ == *s2++; len--)
122
        ;
124
        ;
123
    return len;
125
    return len;
124
}
126
}
125
 
127
 
126
/** Count the number of characters in the string, not including terminating 0.
128
/** Count the number of characters in the string, not including terminating 0.
127
 *
129
 *
128
 * @param str       String.
130
 * @param str       String.
129
 * @return      Number of characters in string.
131
 * @return      Number of characters in string.
130
 */
132
 */
131
size_t strlen(const char *str)
133
size_t strlen(const char *str)
132
{
134
{
133
    size_t counter = 0;
135
    size_t counter = 0;
134
 
136
 
135
    while (str[counter] != 0)
137
    while (str[counter] != 0)
136
        counter++;
138
        counter++;
137
 
139
 
138
    return counter;
140
    return counter;
139
}
141
}
140
 
142
 
141
int strcmp(const char *a, const char *b)
143
int strcmp(const char *a, const char *b)
142
{
144
{
143
    int c = 0;
145
    int c = 0;
144
   
146
   
145
    while (a[c] && b[c] && (!(a[c] - b[c])))
147
    while (a[c] && b[c] && (!(a[c] - b[c])))
146
        c++;
148
        c++;
147
   
149
   
148
    return (a[c] - b[c]);
150
    return (a[c] - b[c]);
149
}
151
}
150
 
152
 
151
int strncmp(const char *a, const char *b, size_t n)
153
int strncmp(const char *a, const char *b, size_t n)
152
{
154
{
153
    size_t c = 0;
155
    size_t c = 0;
154
 
156
 
155
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
157
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
156
        c++;
158
        c++;
157
   
159
   
158
    return ( c < n ? a[c] - b[c] : 0);
160
    return ( c < n ? a[c] - b[c] : 0);
159
   
161
   
160
}
162
}
161
 
163
 
162
int stricmp(const char *a, const char *b)
164
int stricmp(const char *a, const char *b)
163
{
165
{
164
    int c = 0;
166
    int c = 0;
165
   
167
   
166
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
168
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
167
        c++;
169
        c++;
168
   
170
   
169
    return (tolower(a[c]) - tolower(b[c]));
171
    return (tolower(a[c]) - tolower(b[c]));
170
}
172
}
171
 
173
 
172
/** Return pointer to the first occurence of character c in string.
174
/** Return pointer to the first occurence of character c in string.
173
 *
175
 *
174
 * @param str       Scanned string.
176
 * @param str       Scanned string.
175
 * @param c     Searched character (taken as one byte).
177
 * @param c     Searched character (taken as one byte).
176
 * @return      Pointer to the matched character or NULL if it is not
178
 * @return      Pointer to the matched character or NULL if it is not
177
 *          found in given string.
179
 *          found in given string.
178
 */
180
 */
179
char *strchr(const char *str, int c)
181
char *strchr(const char *str, int c)
180
{
182
{
181
    while (*str != '\0') {
183
    while (*str != '\0') {
182
        if (*str == (char) c)
184
        if (*str == (char) c)
183
            return (char *) str;
185
            return (char *) str;
184
        str++;
186
        str++;
185
    }
187
    }
186
 
188
 
187
    return NULL;
189
    return NULL;
188
}
190
}
189
 
191
 
190
/** Return pointer to the last occurence of character c in string.
192
/** Return pointer to the last occurence of character c in string.
191
 *
193
 *
192
 * @param str       Scanned string.
194
 * @param str       Scanned string.
193
 * @param c     Searched character (taken as one byte).
195
 * @param c     Searched character (taken as one byte).
194
 * @return      Pointer to the matched character or NULL if it is not
196
 * @return      Pointer to the matched character or NULL if it is not
195
 *          found in given string.
197
 *          found in given string.
196
 */
198
 */
197
char *strrchr(const char *str, int c)
199
char *strrchr(const char *str, int c)
198
{
200
{
199
    char *retval = NULL;
201
    char *retval = NULL;
200
 
202
 
201
    while (*str != '\0') {
203
    while (*str != '\0') {
202
        if (*str == (char) c)
204
        if (*str == (char) c)
203
            retval = (char *) str;
205
            retval = (char *) str;
204
        str++;
206
        str++;
205
    }
207
    }
206
 
208
 
207
    return (char *) retval;
209
    return (char *) retval;
208
}
210
}
209
 
211
 
210
/** Convert string to a number.
212
/** Convert string to a number.
211
 * Core of strtol and strtoul functions.
213
 * Core of strtol and strtoul functions.
212
 *
214
 *
213
 * @param nptr      Pointer to string.
215
 * @param nptr      Pointer to string.
214
 * @param endptr    If not NULL, function stores here pointer to the first
216
 * @param endptr    If not NULL, function stores here pointer to the first
215
 *          invalid character.
217
 *          invalid character.
216
 * @param base      Zero or number between 2 and 36 inclusive.
218
 * @param base      Zero or number between 2 and 36 inclusive.
217
 * @param sgn       It's set to 1 if minus found.
219
 * @param sgn       It's set to 1 if minus found.
218
 * @return      Result of conversion.
220
 * @return      Result of conversion.
219
 */
221
 */
220
static unsigned long
222
static unsigned long
221
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
223
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
222
{
224
{
223
    unsigned char c;
225
    unsigned char c;
224
    unsigned long result = 0;
226
    unsigned long result = 0;
225
    unsigned long a, b;
227
    unsigned long a, b;
226
    const char *str = nptr;
228
    const char *str = nptr;
227
    const char *tmpptr;
229
    const char *tmpptr;
228
   
230
   
229
    while (isspace(*str))
231
    while (isspace(*str))
230
        str++;
232
        str++;
231
   
233
   
232
    if (*str == '-') {
234
    if (*str == '-') {
233
        *sgn = 1;
235
        *sgn = 1;
234
        ++str;
236
        ++str;
235
    } else if (*str == '+')
237
    } else if (*str == '+')
236
        ++str;
238
        ++str;
237
   
239
   
238
    if (base) {
240
    if (base) {
239
        if ((base == 1) || (base > 36)) {
241
        if ((base == 1) || (base > 36)) {
240
            /* FIXME: set errno to EINVAL */
242
            /* FIXME: set errno to EINVAL */
241
            return 0;
243
            return 0;
242
        }
244
        }
243
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
245
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
244
            (str[1] == 'X'))) {
246
            (str[1] == 'X'))) {
245
            str += 2;
247
            str += 2;
246
        }
248
        }
247
    } else {
249
    } else {
248
        base = 10;
250
        base = 10;
249
       
251
       
250
        if (*str == '0') {
252
        if (*str == '0') {
251
            base = 8;
253
            base = 8;
252
            if ((str[1] == 'X') || (str[1] == 'x'))  {
254
            if ((str[1] == 'X') || (str[1] == 'x'))  {
253
                base = 16;
255
                base = 16;
254
                str += 2;
256
                str += 2;
255
            }
257
            }
256
        }
258
        }
257
    }
259
    }
258
   
260
   
259
    tmpptr = str;
261
    tmpptr = str;
260
 
262
 
261
    while (*str) {
263
    while (*str) {
262
        c = *str;
264
        c = *str;
263
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
265
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
264
            (c <= '9' ? c - '0' : 0xff)));
266
            (c <= '9' ? c - '0' : 0xff)));
265
        if (c > base) {
267
        if (c > base) {
266
            break;
268
            break;
267
        }
269
        }
268
       
270
       
269
        a = (result & 0xff) * base + c;
271
        a = (result & 0xff) * base + c;
270
        b = (result >> 8) * base + (a >> 8);
272
        b = (result >> 8) * base + (a >> 8);
271
       
273
       
272
        if (b > (ULONG_MAX >> 8)) {
274
        if (b > (ULONG_MAX >> 8)) {
273
            /* overflow */
275
            /* overflow */
274
            /* FIXME: errno = ERANGE*/
276
            /* FIXME: errno = ERANGE*/
275
            return ULONG_MAX;
277
            return ULONG_MAX;
276
        }
278
        }
277
   
279
   
278
        result = (b << 8) + (a & 0xff);
280
        result = (b << 8) + (a & 0xff);
279
        ++str;
281
        ++str;
280
    }
282
    }
281
   
283
   
282
    if (str == tmpptr) {
284
    if (str == tmpptr) {
283
        /*
285
        /*
284
         * No number was found => first invalid character is the first
286
         * No number was found => first invalid character is the first
285
         * character of the string.
287
         * character of the string.
286
         */
288
         */
287
        /* FIXME: set errno to EINVAL */
289
        /* FIXME: set errno to EINVAL */
288
        str = nptr;
290
        str = nptr;
289
        result = 0;
291
        result = 0;
290
    }
292
    }
291
   
293
   
292
    if (endptr)
294
    if (endptr)
293
        *endptr = (char *) str;
295
        *endptr = (char *) str;
294
 
296
 
295
    if (nptr == str) {
297
    if (nptr == str) {
296
        /*FIXME: errno = EINVAL*/
298
        /*FIXME: errno = EINVAL*/
297
        return 0;
299
        return 0;
298
    }
300
    }
299
 
301
 
300
    return result;
302
    return result;
301
}
303
}
302
 
304
 
303
/** Convert initial part of string to long int according to given base.
305
/** Convert initial part of string to long int according to given base.
304
 * The number may begin with an arbitrary number of whitespaces followed by
306
 * The number may begin with an arbitrary number of whitespaces followed by
305
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
307
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
306
 * inserted and the number will be taken as hexadecimal one. If the base is 0
308
 * inserted and the number will be taken as hexadecimal one. If the base is 0
307
 * and the number begin with a zero, number will be taken as octal one (as with
309
 * and the number begin with a zero, number will be taken as octal one (as with
308
 * base 8). Otherwise the base 0 is taken as decimal.
310
 * base 8). Otherwise the base 0 is taken as decimal.
309
 *
311
 *
310
 * @param nptr      Pointer to string.
312
 * @param nptr      Pointer to string.
311
 * @param endptr    If not NULL, function stores here pointer to the first
313
 * @param endptr    If not NULL, function stores here pointer to the first
312
 *          invalid character.
314
 *          invalid character.
313
 * @param base      Zero or number between 2 and 36 inclusive.
315
 * @param base      Zero or number between 2 and 36 inclusive.
314
 * @return      Result of conversion.
316
 * @return      Result of conversion.
315
 */
317
 */
316
long int strtol(const char *nptr, char **endptr, int base)
318
long int strtol(const char *nptr, char **endptr, int base)
317
{
319
{
318
    char sgn = 0;
320
    char sgn = 0;
319
    unsigned long number = 0;
321
    unsigned long number = 0;
320
   
322
   
321
    number = _strtoul(nptr, endptr, base, &sgn);
323
    number = _strtoul(nptr, endptr, base, &sgn);
322
 
324
 
323
    if (number > LONG_MAX) {
325
    if (number > LONG_MAX) {
324
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
326
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
325
            /* FIXME: set 0 to errno */
327
            /* FIXME: set 0 to errno */
326
            return number;     
328
            return number;     
327
        }
329
        }
328
        /* FIXME: set ERANGE to errno */
330
        /* FIXME: set ERANGE to errno */
329
        return (sgn ? LONG_MIN : LONG_MAX);
331
        return (sgn ? LONG_MIN : LONG_MAX);
330
    }
332
    }
331
   
333
   
332
    return (sgn ? -number : number);
334
    return (sgn ? -number : number);
333
}
335
}
334
 
336
 
335
 
337
 
336
/** Convert initial part of string to unsigned long according to given base.
338
/** Convert initial part of string to unsigned long according to given base.
337
 * The number may begin with an arbitrary number of whitespaces followed by
339
 * The number may begin with an arbitrary number of whitespaces followed by
338
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
340
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
339
 * inserted and the number will be taken as hexadecimal one. If the base is 0
341
 * inserted and the number will be taken as hexadecimal one. If the base is 0
340
 * and the number begin with a zero, number will be taken as octal one (as with
342
 * and the number begin with a zero, number will be taken as octal one (as with
341
 * base 8). Otherwise the base 0 is taken as decimal.
343
 * base 8). Otherwise the base 0 is taken as decimal.
342
 *
344
 *
343
 * @param nptr      Pointer to string.
345
 * @param nptr      Pointer to string.
344
 * @param endptr    If not NULL, function stores here pointer to the first
346
 * @param endptr    If not NULL, function stores here pointer to the first
345
 *          invalid character
347
 *          invalid character
346
 * @param base      Zero or number between 2 and 36 inclusive.
348
 * @param base      Zero or number between 2 and 36 inclusive.
347
 * @return      Result of conversion.
349
 * @return      Result of conversion.
348
 */
350
 */
349
unsigned long strtoul(const char *nptr, char **endptr, int base)
351
unsigned long strtoul(const char *nptr, char **endptr, int base)
350
{
352
{
351
    char sgn = 0;
353
    char sgn = 0;
352
    unsigned long number = 0;
354
    unsigned long number = 0;
353
   
355
   
354
    number = _strtoul(nptr, endptr, base, &sgn);
356
    number = _strtoul(nptr, endptr, base, &sgn);
355
 
357
 
356
    return (sgn ? -number : number);
358
    return (sgn ? -number : number);
357
}
359
}
358
 
360
 
359
char *strcpy(char *dest, const char *src)
361
char *strcpy(char *dest, const char *src)
360
{
362
{
361
    char *orig = dest;
363
    char *orig = dest;
362
   
364
   
363
    while ((*(dest++) = *(src++)))
365
    while ((*(dest++) = *(src++)))
364
        ;
366
        ;
365
    return orig;
367
    return orig;
366
}
368
}
367
 
369
 
368
char *strncpy(char *dest, const char *src, size_t n)
370
char *strncpy(char *dest, const char *src, size_t n)
369
{
371
{
370
    char *orig = dest;
372
    char *orig = dest;
371
   
373
   
372
    while ((*(dest++) = *(src++)) && --n)
374
    while ((*(dest++) = *(src++)) && --n)
373
        ;
375
        ;
374
    return orig;
376
    return orig;
375
}
377
}
376
 
378
 
377
char *strcat(char *dest, const char *src)
379
char *strcat(char *dest, const char *src)
378
{
380
{
379
    char *orig = dest;
381
    char *orig = dest;
380
    while (*dest++)
382
    while (*dest++)
381
        ;
383
        ;
382
    --dest;
384
    --dest;
383
    while ((*dest++ = *src++))
385
    while ((*dest++ = *src++))
384
        ;
386
        ;
385
    return orig;
387
    return orig;
386
}
388
}
387
 
389
 
388
char * strdup(const char *s1)
390
char * strdup(const char *s1)
389
{
391
{
390
    size_t len = strlen(s1) + 1;
392
    size_t len = strlen(s1) + 1;
391
    void *ret = malloc(len);
393
    void *ret = malloc(len);
392
 
394
 
393
    if (ret == NULL)
395
    if (ret == NULL)
394
        return (char *) NULL;
396
        return (char *) NULL;
395
 
397
 
396
    return (char *) memcpy(ret, s1, len);
398
    return (char *) memcpy(ret, s1, len);
397
}
399
}
-
 
400
 
-
 
401
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
-
 
402
char * strtok_r(char *s, const char *delim, char **last)
-
 
403
{
-
 
404
    char *spanp, *tok;
-
 
405
    int c, sc;
-
 
406
 
-
 
407
    if (s == NULL && (s = *last) == NULL)
-
 
408
        return (NULL);
-
 
409
 
-
 
410
cont:
-
 
411
    c = *s++;
-
 
412
    for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
-
 
413
        if (c == sc)
-
 
414
            goto cont;
-
 
415
    }
-
 
416
 
-
 
417
    if (c == 0) {       /* no non-delimiter characters */
-
 
418
        *last = NULL;
-
 
419
        return (NULL);
-
 
420
    }
-
 
421
 
-
 
422
    tok = s - 1;
-
 
423
 
-
 
424
    for (;;) {
-
 
425
        c = *s++;
-
 
426
        spanp = (char *)delim;
-
 
427
        do {
-
 
428
            if ((sc = *spanp++) == c) {
-
 
429
                if (c == 0)
-
 
430
                    s = NULL;
-
 
431
                else
-
 
432
                    s[-1] = '\0';
-
 
433
                *last = s;
-
 
434
                return (tok);
-
 
435
            }
-
 
436
        } while (sc != 0);
-
 
437
    }
-
 
438
}
-
 
439
 
-
 
440
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
-
 
441
char * strtok(char *s, const char *delim)
-
 
442
{
-
 
443
    static char *last;
-
 
444
 
-
 
445
    return (strtok_r(s, delim, &last));
-
 
446
}
398
 
447
 
399
/** @}
448
/** @}
400
 */
449
 */
401
 
450