Subversion Repositories HelenOS

Rev

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

Rev 3386 Rev 4153
Line 1... Line 1...
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
-
 
3
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 * All rights reserved.
4
 *
5
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * are met:
Line 31... Line 32...
31
 */
32
 */
32
/** @file
33
/** @file
33
 */
34
 */
34
 
35
 
35
#include <string.h>
36
#include <string.h>
36
#include <unistd.h>
-
 
37
#include <ctype.h>
37
#include <stdlib.h>
38
#include <limits.h>
38
#include <limits.h>
39
#include <align.h>
-
 
40
#include <sys/types.h>
39
#include <ctype.h>
41
#include <malloc.h>
40
#include <malloc.h>
42
 
41
 
43
/* Dummy implementation of mem/ functions */
-
 
44
 
-
 
45
void *memset(void *s, int c, size_t n)
-
 
46
{
-
 
47
    char *os = s;
-
 
48
   
-
 
49
    while (n--)
-
 
50
        *(os++) = c;
-
 
51
   
-
 
52
    return s;
-
 
53
}
-
 
54
 
-
 
55
struct along {
-
 
56
    unsigned long n;
-
 
57
} __attribute__ ((packed));
-
 
58
 
-
 
59
static void *unaligned_memcpy(void *dst, const void *src, size_t n)
-
 
60
{
-
 
61
    int i, j;
-
 
62
    struct along *adst = dst;
-
 
63
    const struct along *asrc = src;
-
 
64
 
-
 
65
    for (i = 0; i < n / sizeof(unsigned long); i++)
-
 
66
        adst[i].n = asrc[i].n;
-
 
67
       
-
 
68
    for (j = 0; j < n % sizeof(unsigned long); j++)
-
 
69
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
-
 
70
            ((unsigned char *) (((unsigned long *) src) + i))[j];
-
 
71
       
-
 
72
    return (char *) dst;
-
 
73
}
-
 
74
 
-
 
75
void *memcpy(void *dst, const void *src, size_t n)
-
 
76
{
-
 
77
    int i, j;
-
 
78
 
-
 
79
    if (((long) dst & (sizeof(long) - 1)) ||
-
 
80
        ((long) src & (sizeof(long) - 1)))
-
 
81
        return unaligned_memcpy(dst, src, n);
-
 
82
 
-
 
83
    for (i = 0; i < n / sizeof(unsigned long); i++)
-
 
84
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
-
 
85
       
-
 
86
    for (j = 0; j < n % sizeof(unsigned long); j++)
-
 
87
        ((unsigned char *) (((unsigned long *) dst) + i))[j] =
-
 
88
            ((unsigned char *) (((unsigned long *) src) + i))[j];
-
 
89
       
-
 
90
    return (char *) dst;
-
 
91
}
-
 
92
 
-
 
93
void *memmove(void *dst, const void *src, size_t n)
-
 
94
{
-
 
95
    int i, j;
-
 
96
   
-
 
97
    if (src > dst)
-
 
98
        return memcpy(dst, src, n);
-
 
99
 
-
 
100
    for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--)
-
 
101
        ((unsigned char *) ((unsigned long *) dst))[j] =
-
 
102
            ((unsigned char *) ((unsigned long *) src))[j];
-
 
103
 
-
 
104
    for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--)
-
 
105
        ((unsigned long *) dst)[i] = ((unsigned long *) src)[i];
-
 
106
       
-
 
107
    return (char *) dst;
-
 
108
}
-
 
109
 
-
 
110
/** Compare two memory areas.
-
 
111
 *
-
 
112
 * @param s1        Pointer to the first area to compare.
-
 
113
 * @param s2        Pointer to the second area to compare.
-
 
114
 * @param len       Size of the first area in bytes. Both areas must have
-
 
115
 *          the same length.
-
 
116
 * @return      If len is 0, return zero. If the areas match, return
-
 
117
 *          zero. Otherwise return non-zero.
-
 
118
 */
-
 
119
int bcmp(const char *s1, const char *s2, size_t len)
-
 
120
{
-
 
121
    for (; len && *s1++ == *s2++; len--)
-
 
122
        ;
-
 
123
    return len;
-
 
124
}
-
 
125
 
-
 
126
/** Count the number of characters in the string, not including terminating 0.
42
/** Count the number of characters in the string, not including terminating 0.
127
 *
43
 *
128
 * @param str       String.
44
 * @param str       String.
129
 * @return      Number of characters in string.
45
 * @return      Number of characters in string.
130
 */
46
 */
Line 394... Line 310...
394
        return (char *) NULL;
310
        return (char *) NULL;
395
 
311
 
396
    return (char *) memcpy(ret, s1, len);
312
    return (char *) memcpy(ret, s1, len);
397
}
313
}
398
 
314
 
-
 
315
char *strtok(char *s, const char *delim)
-
 
316
{
-
 
317
    static char *next;
-
 
318
 
-
 
319
    return strtok_r(s, delim, &next);
-
 
320
}
-
 
321
 
-
 
322
char *strtok_r(char *s, const char *delim, char **next)
-
 
323
{
-
 
324
    char *start, *end;
-
 
325
 
-
 
326
    if (s == NULL)
-
 
327
        s = *next;
-
 
328
 
-
 
329
    /* Skip over leading delimiters. */
-
 
330
    while (*s && (strchr(delim, *s) != NULL)) ++s;
-
 
331
    start = s;
-
 
332
 
-
 
333
    /* Skip over token characters. */
-
 
334
    while (*s && (strchr(delim, *s) == NULL)) ++s;
-
 
335
    end = s;
-
 
336
    *next = (*s ? s + 1 : s);
-
 
337
 
-
 
338
    if (start == end) {
-
 
339
        return NULL;    /* No more tokens. */
-
 
340
    }
-
 
341
 
-
 
342
    /* Overwrite delimiter with NULL terminator. */
-
 
343
    *end = '\0';
-
 
344
    return start;
-
 
345
}
-
 
346
 
399
/** @}
347
/** @}
400
 */
348
 */