Subversion Repositories HelenOS

Rev

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

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