Subversion Repositories HelenOS

Rev

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

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