Subversion Repositories HelenOS

Rev

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

Rev 2479 Rev 2541
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
 
41
 
41
 
42
 
42
/* Dummy implementation of mem/ functions */
43
/* Dummy implementation of mem/ functions */
43
 
44
 
44
void *memset(void *s, int c, size_t n)
45
void *memset(void *s, int c, size_t n)
45
{
46
{
46
    char *os = s;
47
    char *os = s;
47
   
48
   
48
    while (n--)
49
    while (n--)
49
        *(os++) = c;
50
        *(os++) = c;
50
   
51
   
51
    return s;
52
    return s;
52
}
53
}
53
 
54
 
54
struct along {
55
struct along {
55
    unsigned long n;
56
    unsigned long n;
56
} __attribute__ ((packed));
57
} __attribute__ ((packed));
57
 
58
 
58
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)
59
{
60
{
60
    int i, j;
61
    int i, j;
61
    struct along *adst = dst;
62
    struct along *adst = dst;
62
    const struct along *asrc = src;
63
    const struct along *asrc = src;
63
 
64
 
64
    for (i = 0; i < n / sizeof(unsigned long); i++)
65
    for (i = 0; i < n / sizeof(unsigned long); i++)
65
        adst[i].n = asrc[i].n;
66
        adst[i].n = asrc[i].n;
66
       
67
       
67
    for (j = 0; j < n % sizeof(unsigned long); j++)
68
    for (j = 0; j < n % sizeof(unsigned long); j++)
68
        ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j];
69
        ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j];
69
       
70
       
70
    return (char *) src;
71
    return (char *) src;
71
}
72
}
72
 
73
 
73
void *memcpy(void *dst, const void *src, size_t n)
74
void *memcpy(void *dst, const void *src, size_t n)
74
{
75
{
75
    int i, j;
76
    int i, j;
76
 
77
 
77
    if (((long) dst & (sizeof(long) - 1)) || ((long) src & (sizeof(long) - 1)))
78
    if (((long) dst & (sizeof(long) - 1)) || ((long) src & (sizeof(long) - 1)))
78
        return unaligned_memcpy(dst, src, n);
79
        return unaligned_memcpy(dst, src, n);
79
 
80
 
80
    for (i = 0; i < n / sizeof(unsigned long); i++)
81
    for (i = 0; i < n / sizeof(unsigned long); i++)
81
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
82
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
82
       
83
       
83
    for (j = 0; j < n % sizeof(unsigned long); j++)
84
    for (j = 0; j < n % sizeof(unsigned long); j++)
84
        ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j];
85
        ((unsigned char *) (((unsigned long *) dst) + i))[j] = ((unsigned char *) (((unsigned long *) src) + i))[j];
85
       
86
       
86
    return (char *) src;
87
    return (char *) src;
87
}
88
}
88
 
89
 
89
void *memmove(void *dst, const void *src, size_t n)
90
void *memmove(void *dst, const void *src, size_t n)
90
{
91
{
91
    int i, j;
92
    int i, j;
92
   
93
   
93
    if (src > dst)
94
    if (src > dst)
94
        return memcpy(dst, src, n);
95
        return memcpy(dst, src, n);
95
 
96
 
96
    for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
97
    for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
97
        ((unsigned char *) ((unsigned long *) dst))[j] = ((unsigned char *) ((unsigned long *) src))[j];
98
        ((unsigned char *) ((unsigned long *) dst))[j] = ((unsigned char *) ((unsigned long *) src))[j];
98
 
99
 
99
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
100
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
100
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
101
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
101
       
102
       
102
    return (char *) src;
103
    return (char *) src;
103
}
104
}
104
 
105
 
105
/** Compare two memory areas.
106
/** Compare two memory areas.
106
 *
107
 *
107
 * @param s1    Pointer to the first area to compare.
108
 * @param s1    Pointer to the first area to compare.
108
 * @param s2    Pointer to the second area to compare.
109
 * @param s2    Pointer to the second area to compare.
109
 * @param len   Size of the first area in bytes. Both areas must have the same
110
 * @param len   Size of the first area in bytes. Both areas must have the same
110
 *      length.
111
 *      length.
111
 * @return  If len is 0, return zero. If the areas match, return zero.
112
 * @return  If len is 0, return zero. If the areas match, return zero.
112
 *      Otherwise return non-zero.
113
 *      Otherwise return non-zero.
113
 */
114
 */
114
int bcmp(const char *s1, const char *s2, size_t len)
115
int bcmp(const char *s1, const char *s2, size_t len)
115
{
116
{
116
    for (; len && *s1++ == *s2++; len--)
117
    for (; len && *s1++ == *s2++; len--)
117
        ;
118
        ;
118
    return len;
119
    return len;
119
}
120
}
120
 
121
 
121
/** Count the number of characters in the string, not including terminating 0.
122
/** Count the number of characters in the string, not including terminating 0.
122
 * @param str string
123
 * @param str string
123
 * @return number of characters in string.
124
 * @return number of characters in string.
124
 */
125
 */
125
size_t strlen(const char *str)
126
size_t strlen(const char *str)
126
{
127
{
127
    size_t counter = 0;
128
    size_t counter = 0;
128
 
129
 
129
    while (str[counter] != 0)
130
    while (str[counter] != 0)
130
        counter++;
131
        counter++;
131
 
132
 
132
    return counter;
133
    return counter;
133
}
134
}
134
 
135
 
135
int strcmp(const char *a, const char *b)
136
int strcmp(const char *a, const char *b)
136
{
137
{
137
    int c = 0;
138
    int c = 0;
138
   
139
   
139
    while (a[c] && b[c] && (!(a[c] - b[c])))
140
    while (a[c] && b[c] && (!(a[c] - b[c])))
140
        c++;
141
        c++;
141
   
142
   
142
    return (a[c] - b[c]);
143
    return (a[c] - b[c]);
143
   
144
   
144
}
145
}
145
 
146
 
146
 
147
 
147
/** Return pointer to the first occurence of character c in string
148
/** Return pointer to the first occurence of character c in string
148
 * @param str scanned string
149
 * @param str scanned string
149
 * @param c searched character (taken as one byte)
150
 * @param c searched character (taken as one byte)
150
 * @return pointer to the matched character or NULL if it is not found in given string.
151
 * @return pointer to the matched character or NULL if it is not found in given string.
151
 */
152
 */
152
char *strchr(const char *str, int c)
153
char *strchr(const char *str, int c)
153
{
154
{
154
    while (*str != '\0') {
155
    while (*str != '\0') {
155
        if (*str == (char) c)
156
        if (*str == (char) c)
156
            return (char *) str;
157
            return (char *) str;
157
        str++;
158
        str++;
158
    }
159
    }
159
 
160
 
160
    return NULL;
161
    return NULL;
161
}
162
}
162
 
163
 
163
/** Return pointer to the last occurence of character c in string
164
/** Return pointer to the last occurence of character c in string
164
 * @param str scanned string
165
 * @param str scanned string
165
 * @param c searched character (taken as one byte)
166
 * @param c searched character (taken as one byte)
166
 * @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 found in given string.
167
 */
168
 */
168
char *strrchr(const char *str, int c)
169
char *strrchr(const char *str, int c)
169
{
170
{
170
    char *retval = NULL;
171
    char *retval = NULL;
171
 
172
 
172
    while (*str != '\0') {
173
    while (*str != '\0') {
173
        if (*str == (char) c)
174
        if (*str == (char) c)
174
            retval = (char *) str;
175
            retval = (char *) str;
175
        str++;
176
        str++;
176
    }
177
    }
177
 
178
 
178
    return (char *) retval;
179
    return (char *) retval;
179
}
180
}
180
 
181
 
181
/** Convert string to a number.
182
/** Convert string to a number.
182
 * Core of strtol and strtoul functions.
183
 * Core of strtol and strtoul functions.
183
 * @param nptr pointer to string
184
 * @param nptr pointer to string
184
 * @param endptr if not NULL, function stores here pointer to the first invalid character
185
 * @param endptr if not NULL, function stores here pointer to the first invalid character
185
 * @param base zero or number between 2 and 36 inclusive
186
 * @param base zero or number between 2 and 36 inclusive
186
 * @param sgn its set to 1 if minus found
187
 * @param sgn its set to 1 if minus found
187
 * @return result of conversion.
188
 * @return result of conversion.
188
 */
189
 */
189
static unsigned long _strtoul(const char *nptr, char **endptr, int base, char *sgn)
190
static unsigned long _strtoul(const char *nptr, char **endptr, int base, char *sgn)
190
{
191
{
191
    unsigned char c;
192
    unsigned char c;
192
    unsigned long result = 0;
193
    unsigned long result = 0;
193
    unsigned long a, b;
194
    unsigned long a, b;
194
    const char *str = nptr;
195
    const char *str = nptr;
195
    const char *tmpptr;
196
    const char *tmpptr;
196
   
197
   
197
    while (isspace(*str))
198
    while (isspace(*str))
198
        str++;
199
        str++;
199
   
200
   
200
    if (*str == '-') {
201
    if (*str == '-') {
201
        *sgn = 1;
202
        *sgn = 1;
202
        ++str;
203
        ++str;
203
    } else if (*str == '+')
204
    } else if (*str == '+')
204
        ++str;
205
        ++str;
205
   
206
   
206
    if (base) {
207
    if (base) {
207
        if ((base == 1) || (base > 36)) {
208
        if ((base == 1) || (base > 36)) {
208
            /* FIXME: set errno to EINVAL */
209
            /* FIXME: set errno to EINVAL */
209
            return 0;
210
            return 0;
210
        }
211
        }
211
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') || (str[1] == 'X'))) {
212
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') || (str[1] == 'X'))) {
212
            str += 2;
213
            str += 2;
213
        }
214
        }
214
    } else {
215
    } else {
215
        base = 10;
216
        base = 10;
216
       
217
       
217
        if (*str == '0') {
218
        if (*str == '0') {
218
            base = 8;
219
            base = 8;
219
            if ((str[1] == 'X') || (str[1] == 'x'))  {
220
            if ((str[1] == 'X') || (str[1] == 'x'))  {
220
                base = 16;
221
                base = 16;
221
                str += 2;
222
                str += 2;
222
            }
223
            }
223
        }
224
        }
224
    }
225
    }
225
   
226
   
226
    tmpptr = str;
227
    tmpptr = str;
227
 
228
 
228
    while (*str) {
229
    while (*str) {
229
        c = *str;
230
        c = *str;
230
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : (c <= '9' ? c - '0' : 0xff)));
231
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : (c <= '9' ? c - '0' : 0xff)));
231
        if (c > base) {
232
        if (c > base) {
232
            break;
233
            break;
233
        }
234
        }
234
       
235
       
235
        a = (result & 0xff) * base + c;
236
        a = (result & 0xff) * base + c;
236
        b = (result >> 8) * base + (a >> 8);
237
        b = (result >> 8) * base + (a >> 8);
237
       
238
       
238
        if (b > (ULONG_MAX >> 8)) {
239
        if (b > (ULONG_MAX >> 8)) {
239
            /* overflow */
240
            /* overflow */
240
            /* FIXME: errno = ERANGE*/
241
            /* FIXME: errno = ERANGE*/
241
            return ULONG_MAX;
242
            return ULONG_MAX;
242
        }
243
        }
243
   
244
   
244
        result = (b << 8) + (a & 0xff);
245
        result = (b << 8) + (a & 0xff);
245
        ++str;
246
        ++str;
246
    }
247
    }
247
   
248
   
248
    if (str == tmpptr) {
249
    if (str == tmpptr) {
249
        /* no number was found => first invalid character is the first character of the string */
250
        /* no number was found => first invalid character is the first character of the string */
250
        /* FIXME: set errno to EINVAL */
251
        /* FIXME: set errno to EINVAL */
251
        str = nptr;
252
        str = nptr;
252
        result = 0;
253
        result = 0;
253
    }
254
    }
254
   
255
   
255
    if (endptr)
256
    if (endptr)
256
        *endptr = (char *) str;
257
        *endptr = (char *) str;
257
 
258
 
258
    if (nptr == str) {
259
    if (nptr == str) {
259
        /*FIXME: errno = EINVAL*/
260
        /*FIXME: errno = EINVAL*/
260
        return 0;
261
        return 0;
261
    }
262
    }
262
 
263
 
263
    return result;
264
    return result;
264
}
265
}
265
 
266
 
266
/** Convert initial part of string to long int according to given base.
267
/** Convert initial part of string to long int according to given base.
267
 * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-').
268
 * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-').
268
 * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one.
269
 * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one.
269
 * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8).
270
 * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8).
270
 * Otherwise the base 0 is taken as decimal.
271
 * Otherwise the base 0 is taken as decimal.
271
 * @param nptr pointer to string
272
 * @param nptr pointer to string
272
 * @param endptr if not NULL, function stores here pointer to the first invalid character
273
 * @param endptr if not NULL, function stores here pointer to the first invalid character
273
 * @param base zero or number between 2 and 36 inclusive
274
 * @param base zero or number between 2 and 36 inclusive
274
 * @return result of conversion.
275
 * @return result of conversion.
275
 */
276
 */
276
long int strtol(const char *nptr, char **endptr, int base)
277
long int strtol(const char *nptr, char **endptr, int base)
277
{
278
{
278
    char sgn = 0;
279
    char sgn = 0;
279
    unsigned long number = 0;
280
    unsigned long number = 0;
280
   
281
   
281
    number = _strtoul(nptr, endptr, base, &sgn);
282
    number = _strtoul(nptr, endptr, base, &sgn);
282
 
283
 
283
    if (number > LONG_MAX) {
284
    if (number > LONG_MAX) {
284
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
285
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
285
            /* FIXME: set 0 to errno */
286
            /* FIXME: set 0 to errno */
286
            return number;     
287
            return number;     
287
        }
288
        }
288
        /* FIXME: set ERANGE to errno */
289
        /* FIXME: set ERANGE to errno */
289
        return (sgn ? LONG_MIN : LONG_MAX);
290
        return (sgn ? LONG_MIN : LONG_MAX);
290
    }
291
    }
291
   
292
   
292
    return (sgn ? -number : number);
293
    return (sgn ? -number : number);
293
}
294
}
294
 
295
 
295
 
296
 
296
/** Convert initial part of string to unsigned long according to given base.
297
/** Convert initial part of string to unsigned long according to given base.
297
 * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-').
298
 * The number may begin with an arbitrary number of whitespaces followed by optional sign (`+' or `-').
298
 * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one.
299
 * If the base is 0 or 16, the prefix `0x' may be inserted and the number will be taken as hexadecimal one.
299
 * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8).
300
 * If the base is 0 and the number begin with a zero, number will be taken as octal one (as with base 8).
300
 * Otherwise the base 0 is taken as decimal.
301
 * Otherwise the base 0 is taken as decimal.
301
 * @param nptr pointer to string
302
 * @param nptr pointer to string
302
 * @param endptr if not NULL, function stores here pointer to the first invalid character
303
 * @param endptr if not NULL, function stores here pointer to the first invalid character
303
 * @param base zero or number between 2 and 36 inclusive
304
 * @param base zero or number between 2 and 36 inclusive
304
 * @return result of conversion.
305
 * @return result of conversion.
305
 */
306
 */
306
unsigned long strtoul(const char *nptr, char **endptr, int base)
307
unsigned long strtoul(const char *nptr, char **endptr, int base)
307
{
308
{
308
    char sgn = 0;
309
    char sgn = 0;
309
    unsigned long number = 0;
310
    unsigned long number = 0;
310
   
311
   
311
    number = _strtoul(nptr, endptr, base, &sgn);
312
    number = _strtoul(nptr, endptr, base, &sgn);
312
 
313
 
313
    return (sgn ? -number : number);
314
    return (sgn ? -number : number);
314
}
315
}
315
 
316
 
316
char *strcpy(char *dest, const char *src)
317
char *strcpy(char *dest, const char *src)
317
{
318
{
318
    char *orig = dest;
319
    char *orig = dest;
319
   
320
   
320
    while ((*(dest++) = *(src++)));
321
    while ((*(dest++) = *(src++)));
321
    return orig;
322
    return orig;
322
}
323
}
323
 
324
 
324
char *strncpy(char *dest, const char *src, size_t n)
325
char *strncpy(char *dest, const char *src, size_t n)
325
{
326
{
326
    char *orig = dest;
327
    char *orig = dest;
327
   
328
   
328
    while ((*(dest++) = *(src++)) && --n);
329
    while ((*(dest++) = *(src++)) && --n);
329
    return orig;
330
    return orig;
330
}
331
}
331
 
332
 
332
/** @}
333
/** @}
333
 */
334
 */
334
 
335