Subversion Repositories HelenOS

Rev

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

Rev 3261 Rev 3271
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] =
70
            ((unsigned char *) (((unsigned long *) src) + i))[j];
70
            ((unsigned char *) (((unsigned long *) src) + i))[j];
71
       
71
       
72
    return (char *) src;
72
    return (char *) src;
73
}
73
}
74
 
74
 
75
void *memcpy(void *dst, const void *src, size_t n)
75
void *memcpy(void *dst, const void *src, size_t n)
76
{
76
{
77
    int i, j;
77
    int i, j;
78
 
78
 
79
    if (((long) dst & (sizeof(long) - 1)) ||
79
    if (((long) dst & (sizeof(long) - 1)) ||
80
        ((long) src & (sizeof(long) - 1)))
80
        ((long) src & (sizeof(long) - 1)))
81
        return unaligned_memcpy(dst, src, n);
81
        return unaligned_memcpy(dst, src, n);
82
 
82
 
83
    for (i = 0; i < n / sizeof(unsigned long); i++)
83
    for (i = 0; i < n / sizeof(unsigned long); i++)
84
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
84
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
85
       
85
       
86
    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] =
87
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
88
            ((unsigned char *) (((unsigned long *) src) + i))[j];
88
            ((unsigned char *) (((unsigned long *) src) + i))[j];
89
       
89
       
90
    return (char *) src;
90
    return (char *) src;
91
}
91
}
92
 
92
 
93
void *memmove(void *dst, const void *src, size_t n)
93
void *memmove(void *dst, const void *src, size_t n)
94
{
94
{
95
    int i, j;
95
    int i, j;
96
   
96
   
97
    if (src > dst)
97
    if (src > dst)
98
        return memcpy(dst, src, n);
98
        return memcpy(dst, src, n);
99
 
99
 
100
    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] =
101
        ((unsigned char *) ((unsigned long *) dst))[j] =
102
            ((unsigned char *) ((unsigned long *) src))[j];
102
            ((unsigned char *) ((unsigned long *) src))[j];
103
 
103
 
104
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
104
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
105
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
105
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
106
       
106
       
107
    return (char *) src;
107
    return (char *) src;
108
}
108
}
109
 
109
 
110
/** Compare two memory areas.
110
/** Compare two memory areas.
111
 *
111
 *
112
 * @param s1        Pointer to the first area to compare.
112
 * @param s1        Pointer to the first area to compare.
113
 * @param s2        Pointer to the second area to compare.
113
 * @param s2        Pointer to the second area to compare.
114
 * @param len       Size of the first area in bytes. Both areas must have
114
 * @param len       Size of the first area in bytes. Both areas must have
115
 *          the same length.
115
 *          the same length.
116
 * @return      If len is 0, return zero. If the areas match, return
116
 * @return      If len is 0, return zero. If the areas match, return
117
 *          zero. Otherwise return non-zero.
117
 *          zero. Otherwise return non-zero.
118
 */
118
 */
119
int bcmp(const char *s1, const char *s2, size_t len)
119
int bcmp(const char *s1, const char *s2, size_t len)
120
{
120
{
121
    for (; len && *s1++ == *s2++; len--)
121
    for (; len && *s1++ == *s2++; len--)
122
        ;
122
        ;
123
    return len;
123
    return len;
124
}
124
}
125
 
125
 
126
/** 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
 *
127
 *
128
 * @param str       String.
128
 * @param str       String.
129
 * @return      Number of characters in string.
129
 * @return      Number of characters in string.
130
 */
130
 */
131
size_t strlen(const char *str)
131
size_t strlen(const char *str)
132
{
132
{
133
    size_t counter = 0;
133
    size_t counter = 0;
134
 
134
 
135
    while (str[counter] != 0)
135
    while (str[counter] != 0)
136
        counter++;
136
        counter++;
137
 
137
 
138
    return counter;
138
    return counter;
139
}
139
}
140
 
140
 
141
int strcmp(const char *a, const char *b)
141
int strcmp(const char *a, const char *b)
142
{
142
{
143
    int c = 0;
143
    int c = 0;
144
   
144
   
145
    while (a[c] && b[c] && (!(a[c] - b[c])))
145
    while (a[c] && b[c] && (!(a[c] - b[c])))
146
        c++;
146
        c++;
147
   
147
   
148
    return (a[c] - b[c]);
148
    return (a[c] - b[c]);
149
   
-
 
150
}
149
}
151
 
150
 
152
int strncmp(const char *a, const char *b, size_t n)
151
int strncmp(const char *a, const char *b, size_t n)
153
{
152
{
154
    size_t c = 0;
153
    size_t c = 0;
155
 
154
 
156
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
155
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
157
        c++;
156
        c++;
158
   
157
   
159
    return ( c < n ? a[c] - b[c] : 0);
158
    return ( c < n ? a[c] - b[c] : 0);
160
   
159
   
161
}
160
}
-
 
161
 
-
 
162
int stricmp(const char *a, const char *b)
-
 
163
{
-
 
164
    int c = 0;
-
 
165
   
-
 
166
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
-
 
167
        c++;
-
 
168
   
-
 
169
    return (tolower(a[c]) - tolower(b[c]));
-
 
170
}
162
 
171
 
163
/** Return pointer to the first occurence of character c in string.
172
/** Return pointer to the first occurence of character c in string.
164
 *
173
 *
165
 * @param str       Scanned string.
174
 * @param str       Scanned string.
166
 * @param c     Searched character (taken as one byte).
175
 * @param c     Searched character (taken as one byte).
167
 * @return      Pointer to the matched character or NULL if it is not
176
 * @return      Pointer to the matched character or NULL if it is not
168
 *          found in given string.
177
 *          found in given string.
169
 */
178
 */
170
char *strchr(const char *str, int c)
179
char *strchr(const char *str, int c)
171
{
180
{
172
    while (*str != '\0') {
181
    while (*str != '\0') {
173
        if (*str == (char) c)
182
        if (*str == (char) c)
174
            return (char *) str;
183
            return (char *) str;
175
        str++;
184
        str++;
176
    }
185
    }
177
 
186
 
178
    return NULL;
187
    return NULL;
179
}
188
}
180
 
189
 
181
/** Return pointer to the last occurence of character c in string.
190
/** Return pointer to the last occurence of character c in string.
182
 *
191
 *
183
 * @param str       Scanned string.
192
 * @param str       Scanned string.
184
 * @param c     Searched character (taken as one byte).
193
 * @param c     Searched character (taken as one byte).
185
 * @return      Pointer to the matched character or NULL if it is not
194
 * @return      Pointer to the matched character or NULL if it is not
186
 *          found in given string.
195
 *          found in given string.
187
 */
196
 */
188
char *strrchr(const char *str, int c)
197
char *strrchr(const char *str, int c)
189
{
198
{
190
    char *retval = NULL;
199
    char *retval = NULL;
191
 
200
 
192
    while (*str != '\0') {
201
    while (*str != '\0') {
193
        if (*str == (char) c)
202
        if (*str == (char) c)
194
            retval = (char *) str;
203
            retval = (char *) str;
195
        str++;
204
        str++;
196
    }
205
    }
197
 
206
 
198
    return (char *) retval;
207
    return (char *) retval;
199
}
208
}
200
 
209
 
201
/** Convert string to a number.
210
/** Convert string to a number.
202
 * Core of strtol and strtoul functions.
211
 * Core of strtol and strtoul functions.
203
 *
212
 *
204
 * @param nptr      Pointer to string.
213
 * @param nptr      Pointer to string.
205
 * @param endptr    If not NULL, function stores here pointer to the first
214
 * @param endptr    If not NULL, function stores here pointer to the first
206
 *          invalid character.
215
 *          invalid character.
207
 * @param base      Zero or number between 2 and 36 inclusive.
216
 * @param base      Zero or number between 2 and 36 inclusive.
208
 * @param sgn       It's set to 1 if minus found.
217
 * @param sgn       It's set to 1 if minus found.
209
 * @return      Result of conversion.
218
 * @return      Result of conversion.
210
 */
219
 */
211
static unsigned long
220
static unsigned long
212
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
221
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
213
{
222
{
214
    unsigned char c;
223
    unsigned char c;
215
    unsigned long result = 0;
224
    unsigned long result = 0;
216
    unsigned long a, b;
225
    unsigned long a, b;
217
    const char *str = nptr;
226
    const char *str = nptr;
218
    const char *tmpptr;
227
    const char *tmpptr;
219
   
228
   
220
    while (isspace(*str))
229
    while (isspace(*str))
221
        str++;
230
        str++;
222
   
231
   
223
    if (*str == '-') {
232
    if (*str == '-') {
224
        *sgn = 1;
233
        *sgn = 1;
225
        ++str;
234
        ++str;
226
    } else if (*str == '+')
235
    } else if (*str == '+')
227
        ++str;
236
        ++str;
228
   
237
   
229
    if (base) {
238
    if (base) {
230
        if ((base == 1) || (base > 36)) {
239
        if ((base == 1) || (base > 36)) {
231
            /* FIXME: set errno to EINVAL */
240
            /* FIXME: set errno to EINVAL */
232
            return 0;
241
            return 0;
233
        }
242
        }
234
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
243
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
235
            (str[1] == 'X'))) {
244
            (str[1] == 'X'))) {
236
            str += 2;
245
            str += 2;
237
        }
246
        }
238
    } else {
247
    } else {
239
        base = 10;
248
        base = 10;
240
       
249
       
241
        if (*str == '0') {
250
        if (*str == '0') {
242
            base = 8;
251
            base = 8;
243
            if ((str[1] == 'X') || (str[1] == 'x'))  {
252
            if ((str[1] == 'X') || (str[1] == 'x'))  {
244
                base = 16;
253
                base = 16;
245
                str += 2;
254
                str += 2;
246
            }
255
            }
247
        }
256
        }
248
    }
257
    }
249
   
258
   
250
    tmpptr = str;
259
    tmpptr = str;
251
 
260
 
252
    while (*str) {
261
    while (*str) {
253
        c = *str;
262
        c = *str;
254
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
263
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
255
            (c <= '9' ? c - '0' : 0xff)));
264
            (c <= '9' ? c - '0' : 0xff)));
256
        if (c > base) {
265
        if (c > base) {
257
            break;
266
            break;
258
        }
267
        }
259
       
268
       
260
        a = (result & 0xff) * base + c;
269
        a = (result & 0xff) * base + c;
261
        b = (result >> 8) * base + (a >> 8);
270
        b = (result >> 8) * base + (a >> 8);
262
       
271
       
263
        if (b > (ULONG_MAX >> 8)) {
272
        if (b > (ULONG_MAX >> 8)) {
264
            /* overflow */
273
            /* overflow */
265
            /* FIXME: errno = ERANGE*/
274
            /* FIXME: errno = ERANGE*/
266
            return ULONG_MAX;
275
            return ULONG_MAX;
267
        }
276
        }
268
   
277
   
269
        result = (b << 8) + (a & 0xff);
278
        result = (b << 8) + (a & 0xff);
270
        ++str;
279
        ++str;
271
    }
280
    }
272
   
281
   
273
    if (str == tmpptr) {
282
    if (str == tmpptr) {
274
        /*
283
        /*
275
         * No number was found => first invalid character is the first
284
         * No number was found => first invalid character is the first
276
         * character of the string.
285
         * character of the string.
277
         */
286
         */
278
        /* FIXME: set errno to EINVAL */
287
        /* FIXME: set errno to EINVAL */
279
        str = nptr;
288
        str = nptr;
280
        result = 0;
289
        result = 0;
281
    }
290
    }
282
   
291
   
283
    if (endptr)
292
    if (endptr)
284
        *endptr = (char *) str;
293
        *endptr = (char *) str;
285
 
294
 
286
    if (nptr == str) {
295
    if (nptr == str) {
287
        /*FIXME: errno = EINVAL*/
296
        /*FIXME: errno = EINVAL*/
288
        return 0;
297
        return 0;
289
    }
298
    }
290
 
299
 
291
    return result;
300
    return result;
292
}
301
}
293
 
302
 
294
/** Convert initial part of string to long int according to given base.
303
/** Convert initial part of string to long int according to given base.
295
 * The number may begin with an arbitrary number of whitespaces followed by
304
 * 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
305
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
297
 * inserted and the number will be taken as hexadecimal one. If the base is 0
306
 * inserted and the number will be taken as hexadecimal one. If the base is 0
298
 * and the number begin with a zero, number will be taken as octal one (as with
307
 * and the number begin with a zero, number will be taken as octal one (as with
299
 * base 8). Otherwise the base 0 is taken as decimal.
308
 * base 8). Otherwise the base 0 is taken as decimal.
300
 *
309
 *
301
 * @param nptr      Pointer to string.
310
 * @param nptr      Pointer to string.
302
 * @param endptr    If not NULL, function stores here pointer to the first
311
 * @param endptr    If not NULL, function stores here pointer to the first
303
 *          invalid character.
312
 *          invalid character.
304
 * @param base      Zero or number between 2 and 36 inclusive.
313
 * @param base      Zero or number between 2 and 36 inclusive.
305
 * @return      Result of conversion.
314
 * @return      Result of conversion.
306
 */
315
 */
307
long int strtol(const char *nptr, char **endptr, int base)
316
long int strtol(const char *nptr, char **endptr, int base)
308
{
317
{
309
    char sgn = 0;
318
    char sgn = 0;
310
    unsigned long number = 0;
319
    unsigned long number = 0;
311
   
320
   
312
    number = _strtoul(nptr, endptr, base, &sgn);
321
    number = _strtoul(nptr, endptr, base, &sgn);
313
 
322
 
314
    if (number > LONG_MAX) {
323
    if (number > LONG_MAX) {
315
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
324
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
316
            /* FIXME: set 0 to errno */
325
            /* FIXME: set 0 to errno */
317
            return number;     
326
            return number;     
318
        }
327
        }
319
        /* FIXME: set ERANGE to errno */
328
        /* FIXME: set ERANGE to errno */
320
        return (sgn ? LONG_MIN : LONG_MAX);
329
        return (sgn ? LONG_MIN : LONG_MAX);
321
    }
330
    }
322
   
331
   
323
    return (sgn ? -number : number);
332
    return (sgn ? -number : number);
324
}
333
}
325
 
334
 
326
 
335
 
327
/** Convert initial part of string to unsigned long according to given base.
336
/** Convert initial part of string to unsigned long according to given base.
328
 * The number may begin with an arbitrary number of whitespaces followed by
337
 * 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
338
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
330
 * inserted and the number will be taken as hexadecimal one. If the base is 0
339
 * inserted and the number will be taken as hexadecimal one. If the base is 0
331
 * and the number begin with a zero, number will be taken as octal one (as with
340
 * and the number begin with a zero, number will be taken as octal one (as with
332
 * base 8). Otherwise the base 0 is taken as decimal.
341
 * base 8). Otherwise the base 0 is taken as decimal.
333
 *
342
 *
334
 * @param nptr      Pointer to string.
343
 * @param nptr      Pointer to string.
335
 * @param endptr    If not NULL, function stores here pointer to the first
344
 * @param endptr    If not NULL, function stores here pointer to the first
336
 *          invalid character
345
 *          invalid character
337
 * @param base      Zero or number between 2 and 36 inclusive.
346
 * @param base      Zero or number between 2 and 36 inclusive.
338
 * @return      Result of conversion.
347
 * @return      Result of conversion.
339
 */
348
 */
340
unsigned long strtoul(const char *nptr, char **endptr, int base)
349
unsigned long strtoul(const char *nptr, char **endptr, int base)
341
{
350
{
342
    char sgn = 0;
351
    char sgn = 0;
343
    unsigned long number = 0;
352
    unsigned long number = 0;
344
   
353
   
345
    number = _strtoul(nptr, endptr, base, &sgn);
354
    number = _strtoul(nptr, endptr, base, &sgn);
346
 
355
 
347
    return (sgn ? -number : number);
356
    return (sgn ? -number : number);
348
}
357
}
349
 
358
 
350
char *strcpy(char *dest, const char *src)
359
char *strcpy(char *dest, const char *src)
351
{
360
{
352
    char *orig = dest;
361
    char *orig = dest;
353
   
362
   
354
    while ((*(dest++) = *(src++)))
363
    while ((*(dest++) = *(src++)))
355
        ;
364
        ;
356
    return orig;
365
    return orig;
357
}
366
}
358
 
367
 
359
char *strncpy(char *dest, const char *src, size_t n)
368
char *strncpy(char *dest, const char *src, size_t n)
360
{
369
{
361
    char *orig = dest;
370
    char *orig = dest;
362
   
371
   
363
    while ((*(dest++) = *(src++)) && --n)
372
    while ((*(dest++) = *(src++)) && --n)
364
        ;
373
        ;
365
    return orig;
374
    return orig;
366
}
375
}
367
 
376
 
368
char *strcat(char *dest, const char *src)
377
char *strcat(char *dest, const char *src)
369
{
378
{
370
    char *orig = dest;
379
    char *orig = dest;
371
    while (*dest++)
380
    while (*dest++)
372
        ;
381
        ;
373
    --dest;
382
    --dest;
374
    while ((*dest++ = *src++))
383
    while ((*dest++ = *src++))
375
        ;
384
        ;
376
    return orig;
385
    return orig;
377
}
386
}
378
 
387
 
379
char * strdup(const char *s1)
388
char * strdup(const char *s1)
380
{
389
{
381
    size_t len = strlen(s1) + 1;
390
    size_t len = strlen(s1) + 1;
382
    void *ret = malloc(len);
391
    void *ret = malloc(len);
383
 
392
 
384
    if (ret == NULL)
393
    if (ret == NULL)
385
        return (char *) NULL;
394
        return (char *) NULL;
386
 
395
 
387
    return (char *) memcpy(ret, s1, len);
396
    return (char *) memcpy(ret, s1, len);
388
}
397
}
389
 
398
 
390
/** @}
399
/** @}
391
 */
400
 */
392
 
401