Subversion Repositories HelenOS-historic

Rev

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

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