Subversion Repositories HelenOS

Rev

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

Rev 3730 Rev 3731
Line 31... Line 31...
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <string.h>
-
 
37
#include <unistd.h>
-
 
38
#include <ctype.h>
-
 
39
#include <limits.h>
36
#include <mem.h>
40
#include <align.h>
37
#include <stdlib.h>
41
#include <sys/types.h>
38
#include <sys/types.h>
42
#include <malloc.h>
-
 
43
 
39
 
44
/** Fill memory block with a constant value. */
40
/** Fill memory block with a constant value. */
45
void *memset(void *dest, int b, size_t n)
41
void *memset(void *dest, int b, size_t n)
46
{
42
{
47
    char *pb;
43
    char *pb;
Line 236... Line 232...
236
    for (; len && *s1++ == *s2++; len--)
232
    for (; len && *s1++ == *s2++; len--)
237
        ;
233
        ;
238
    return len;
234
    return len;
239
}
235
}
240
 
236
 
241
/** Count the number of characters in the string, not including terminating 0.
-
 
242
 *
-
 
243
 * @param str       String.
-
 
244
 * @return      Number of characters in string.
-
 
245
 */
-
 
246
size_t strlen(const char *str)
-
 
247
{
-
 
248
    size_t counter = 0;
-
 
249
 
-
 
250
    while (str[counter] != 0)
-
 
251
        counter++;
-
 
252
 
-
 
253
    return counter;
-
 
254
}
-
 
255
 
-
 
256
int strcmp(const char *a, const char *b)
-
 
257
{
-
 
258
    int c = 0;
-
 
259
   
-
 
260
    while (a[c] && b[c] && (!(a[c] - b[c])))
-
 
261
        c++;
-
 
262
   
-
 
263
    return (a[c] - b[c]);
-
 
264
}
-
 
265
 
-
 
266
int strncmp(const char *a, const char *b, size_t n)
-
 
267
{
-
 
268
    size_t c = 0;
-
 
269
 
-
 
270
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
-
 
271
        c++;
-
 
272
   
-
 
273
    return ( c < n ? a[c] - b[c] : 0);
-
 
274
   
-
 
275
}
-
 
276
 
-
 
277
int stricmp(const char *a, const char *b)
-
 
278
{
-
 
279
    int c = 0;
-
 
280
   
-
 
281
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
-
 
282
        c++;
-
 
283
   
-
 
284
    return (tolower(a[c]) - tolower(b[c]));
-
 
285
}
-
 
286
 
-
 
287
/** Return pointer to the first occurence of character c in string.
-
 
288
 *
-
 
289
 * @param str       Scanned string.
-
 
290
 * @param c     Searched character (taken as one byte).
-
 
291
 * @return      Pointer to the matched character or NULL if it is not
-
 
292
 *          found in given string.
-
 
293
 */
-
 
294
char *strchr(const char *str, int c)
-
 
295
{
-
 
296
    while (*str != '\0') {
-
 
297
        if (*str == (char) c)
-
 
298
            return (char *) str;
-
 
299
        str++;
-
 
300
    }
-
 
301
 
-
 
302
    return NULL;
-
 
303
}
-
 
304
 
-
 
305
/** Return pointer to the last occurence of character c in string.
-
 
306
 *
-
 
307
 * @param str       Scanned string.
-
 
308
 * @param c     Searched character (taken as one byte).
-
 
309
 * @return      Pointer to the matched character or NULL if it is not
-
 
310
 *          found in given string.
-
 
311
 */
-
 
312
char *strrchr(const char *str, int c)
-
 
313
{
-
 
314
    char *retval = NULL;
-
 
315
 
-
 
316
    while (*str != '\0') {
-
 
317
        if (*str == (char) c)
-
 
318
            retval = (char *) str;
-
 
319
        str++;
-
 
320
    }
-
 
321
 
-
 
322
    return (char *) retval;
-
 
323
}
-
 
324
 
-
 
325
/** Convert string to a number.
-
 
326
 * Core of strtol and strtoul functions.
-
 
327
 *
-
 
328
 * @param nptr      Pointer to string.
-
 
329
 * @param endptr    If not NULL, function stores here pointer to the first
-
 
330
 *          invalid character.
-
 
331
 * @param base      Zero or number between 2 and 36 inclusive.
-
 
332
 * @param sgn       It's set to 1 if minus found.
-
 
333
 * @return      Result of conversion.
-
 
334
 */
-
 
335
static unsigned long
-
 
336
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
-
 
337
{
-
 
338
    unsigned char c;
-
 
339
    unsigned long result = 0;
-
 
340
    unsigned long a, b;
-
 
341
    const char *str = nptr;
-
 
342
    const char *tmpptr;
-
 
343
   
-
 
344
    while (isspace(*str))
-
 
345
        str++;
-
 
346
   
-
 
347
    if (*str == '-') {
-
 
348
        *sgn = 1;
-
 
349
        ++str;
-
 
350
    } else if (*str == '+')
-
 
351
        ++str;
-
 
352
   
-
 
353
    if (base) {
-
 
354
        if ((base == 1) || (base > 36)) {
-
 
355
            /* FIXME: set errno to EINVAL */
-
 
356
            return 0;
-
 
357
        }
-
 
358
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
-
 
359
            (str[1] == 'X'))) {
-
 
360
            str += 2;
-
 
361
        }
-
 
362
    } else {
-
 
363
        base = 10;
-
 
364
       
-
 
365
        if (*str == '0') {
-
 
366
            base = 8;
-
 
367
            if ((str[1] == 'X') || (str[1] == 'x'))  {
-
 
368
                base = 16;
-
 
369
                str += 2;
-
 
370
            }
-
 
371
        }
-
 
372
    }
-
 
373
   
-
 
374
    tmpptr = str;
-
 
375
 
-
 
376
    while (*str) {
-
 
377
        c = *str;
-
 
378
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
-
 
379
            (c <= '9' ? c - '0' : 0xff)));
-
 
380
        if (c > base) {
-
 
381
            break;
-
 
382
        }
-
 
383
       
-
 
384
        a = (result & 0xff) * base + c;
-
 
385
        b = (result >> 8) * base + (a >> 8);
-
 
386
       
-
 
387
        if (b > (ULONG_MAX >> 8)) {
-
 
388
            /* overflow */
-
 
389
            /* FIXME: errno = ERANGE*/
-
 
390
            return ULONG_MAX;
-
 
391
        }
-
 
392
   
-
 
393
        result = (b << 8) + (a & 0xff);
-
 
394
        ++str;
-
 
395
    }
-
 
396
   
-
 
397
    if (str == tmpptr) {
-
 
398
        /*
-
 
399
         * No number was found => first invalid character is the first
-
 
400
         * character of the string.
-
 
401
         */
-
 
402
        /* FIXME: set errno to EINVAL */
-
 
403
        str = nptr;
-
 
404
        result = 0;
-
 
405
    }
-
 
406
   
-
 
407
    if (endptr)
-
 
408
        *endptr = (char *) str;
-
 
409
 
-
 
410
    if (nptr == str) {
-
 
411
        /*FIXME: errno = EINVAL*/
-
 
412
        return 0;
-
 
413
    }
-
 
414
 
-
 
415
    return result;
-
 
416
}
-
 
417
 
-
 
418
/** Convert initial part of string to long int according to given base.
-
 
419
 * The number may begin with an arbitrary number of whitespaces followed by
-
 
420
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
-
 
421
 * inserted and the number will be taken as hexadecimal one. If the base is 0
-
 
422
 * and the number begin with a zero, number will be taken as octal one (as with
-
 
423
 * base 8). Otherwise the base 0 is taken as decimal.
-
 
424
 *
-
 
425
 * @param nptr      Pointer to string.
-
 
426
 * @param endptr    If not NULL, function stores here pointer to the first
-
 
427
 *          invalid character.
-
 
428
 * @param base      Zero or number between 2 and 36 inclusive.
-
 
429
 * @return      Result of conversion.
-
 
430
 */
-
 
431
long int strtol(const char *nptr, char **endptr, int base)
-
 
432
{
-
 
433
    char sgn = 0;
-
 
434
    unsigned long number = 0;
-
 
435
   
-
 
436
    number = _strtoul(nptr, endptr, base, &sgn);
-
 
437
 
-
 
438
    if (number > LONG_MAX) {
-
 
439
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
-
 
440
            /* FIXME: set 0 to errno */
-
 
441
            return number;     
-
 
442
        }
-
 
443
        /* FIXME: set ERANGE to errno */
-
 
444
        return (sgn ? LONG_MIN : LONG_MAX);
-
 
445
    }
-
 
446
   
-
 
447
    return (sgn ? -number : number);
-
 
448
}
-
 
449
 
-
 
450
 
-
 
451
/** Convert initial part of string to unsigned long according to given base.
-
 
452
 * The number may begin with an arbitrary number of whitespaces followed by
-
 
453
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
-
 
454
 * inserted and the number will be taken as hexadecimal one. If the base is 0
-
 
455
 * and the number begin with a zero, number will be taken as octal one (as with
-
 
456
 * base 8). Otherwise the base 0 is taken as decimal.
-
 
457
 *
-
 
458
 * @param nptr      Pointer to string.
-
 
459
 * @param endptr    If not NULL, function stores here pointer to the first
-
 
460
 *          invalid character
-
 
461
 * @param base      Zero or number between 2 and 36 inclusive.
-
 
462
 * @return      Result of conversion.
-
 
463
 */
-
 
464
unsigned long strtoul(const char *nptr, char **endptr, int base)
-
 
465
{
-
 
466
    char sgn = 0;
-
 
467
    unsigned long number = 0;
-
 
468
   
-
 
469
    number = _strtoul(nptr, endptr, base, &sgn);
-
 
470
 
-
 
471
    return (sgn ? -number : number);
-
 
472
}
-
 
473
 
-
 
474
char *strcpy(char *dest, const char *src)
-
 
475
{
-
 
476
    char *orig = dest;
-
 
477
   
-
 
478
    while ((*(dest++) = *(src++)))
-
 
479
        ;
-
 
480
    return orig;
-
 
481
}
-
 
482
 
-
 
483
char *strncpy(char *dest, const char *src, size_t n)
-
 
484
{
-
 
485
    char *orig = dest;
-
 
486
   
-
 
487
    while ((*(dest++) = *(src++)) && --n)
-
 
488
        ;
-
 
489
    return orig;
-
 
490
}
-
 
491
 
-
 
492
char *strcat(char *dest, const char *src)
-
 
493
{
-
 
494
    char *orig = dest;
-
 
495
    while (*dest++)
-
 
496
        ;
-
 
497
    --dest;
-
 
498
    while ((*dest++ = *src++))
-
 
499
        ;
-
 
500
    return orig;
-
 
501
}
-
 
502
 
-
 
503
char * strdup(const char *s1)
-
 
504
{
-
 
505
    size_t len = strlen(s1) + 1;
-
 
506
    void *ret = malloc(len);
-
 
507
 
-
 
508
    if (ret == NULL)
-
 
509
        return (char *) NULL;
-
 
510
 
-
 
511
    return (char *) memcpy(ret, s1, len);
-
 
512
}
-
 
513
 
-
 
514
char *strtok(char *s, const char *delim)
-
 
515
{
-
 
516
    static char *next;
-
 
517
 
-
 
518
    return strtok_r(s, delim, &next);
-
 
519
}
-
 
520
 
-
 
521
char *strtok_r(char *s, const char *delim, char **next)
-
 
522
{
-
 
523
    char *start, *end;
-
 
524
 
-
 
525
    if (s == NULL)
-
 
526
        s = *next;
-
 
527
 
-
 
528
    /* Skip over leading delimiters. */
-
 
529
    while (*s && (strchr(delim, *s) != NULL)) ++s;
-
 
530
    start = s;
-
 
531
 
-
 
532
    /* Skip over token characters. */
-
 
533
    while (*s && (strchr(delim, *s) == NULL)) ++s;
-
 
534
    end = s;
-
 
535
    *next = (*s ? s + 1 : s);
-
 
536
 
-
 
537
    if (start == end) {
-
 
538
        return NULL;    /* No more tokens. */
-
 
539
    }
-
 
540
 
-
 
541
    /* Overwrite delimiter with NULL terminator. */
-
 
542
    *end = '\0';
-
 
543
    return start;
-
 
544
}
-
 
545
 
-
 
546
/** @}
237
/** @}
547
 */
238
 */