Subversion Repositories HelenOS

Rev

Rev 2927 | Rev 4337 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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