Subversion Repositories HelenOS-historic

Rev

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

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