Subversion Repositories HelenOS

Rev

Rev 3405 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3405 Rev 3888
1
/*
1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
2
 * Copyright (c) 2001-2004 Jakub Jermar
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 generic
29
/** @addtogroup generic
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
#include <string.h>
33
#include <string.h>
34
 
34
 
35
/**
35
/**
36
 * @file
36
 * @file
37
 * @brief   String manipulation functions.
37
 * @brief   String manipulation functions.
38
 */
38
 */
39
 
39
 
40
/** Return number of characters in a string.
40
/** Return number of characters in a string.
41
 *
41
 *
42
 * @param str       NULL terminated string.
42
 * @param str       NULL terminated string.
43
 *
43
 *
44
 * @return      Number of characters in str.
44
 * @return      Number of characters in str.
45
 */
45
 */
46
size_t strlen(const char *str)
46
size_t strlen(const char *str)
47
{
47
{
48
    int i;
48
    int i;
49
   
49
   
50
    for (i = 0; str[i]; i++)
50
    for (i = 0; str[i]; i++)
51
        ;
51
        ;
52
   
52
   
53
    return i;
53
    return i;
54
}
54
}
55
 
55
 
56
/** Compare two NULL terminated strings.
56
/** Compare two NULL terminated strings.
57
 *
57
 *
58
 * Do a char-by-char comparison of two NULL terminated strings.
58
 * Do a char-by-char comparison of two NULL terminated strings.
59
 * The strings are considered equal iff they consist of the same
59
 * The strings are considered equal iff they consist of the same
60
 * characters on the minimum of their lengths.
60
 * characters on the minimum of their lengths.
61
 *
61
 *
62
 * @param src       First string to compare.
62
 * @param src       First string to compare.
63
 * @param dst       Second string to compare.
63
 * @param dst       Second string to compare.
64
 *
64
 *
65
 * @return      0 if the strings are equal, -1 if first is smaller,
65
 * @return      0 if the strings are equal, -1 if first is smaller,
66
 *          1 if second smaller.
66
 *          1 if second smaller.
67
 *
67
 *
68
 */
68
 */
69
int strcmp(const char *src, const char *dst)
69
int strcmp(const char *src, const char *dst)
70
{
70
{
71
    for (; *src && *dst; src++, dst++) {
71
    for (; *src && *dst; src++, dst++) {
72
        if (*src < *dst)
72
        if (*src < *dst)
73
            return -1;
73
            return -1;
74
        if (*src > *dst)
74
        if (*src > *dst)
75
            return 1;
75
            return 1;
76
    }
76
    }
77
    if (*src == *dst)
77
    if (*src == *dst)
78
        return 0;
78
        return 0;
79
    if (!*src)
79
    if (!*src)
80
        return -1;
80
        return -1;
81
    return 1;
81
    return 1;
82
}
82
}
83
 
83
 
84
 
84
 
85
/** Compare two NULL terminated strings.
85
/** Compare two NULL terminated strings.
86
 *
86
 *
87
 * Do a char-by-char comparison of two NULL terminated strings.
87
 * Do a char-by-char comparison of two NULL terminated strings.
88
 * The strings are considered equal iff they consist of the same
88
 * The strings are considered equal iff they consist of the same
89
 * characters on the minimum of their lengths and specified maximal
89
 * characters on the minimum of their lengths and specified maximal
90
 * length.
90
 * length.
91
 *
91
 *
92
 * @param src       First string to compare.
92
 * @param src       First string to compare.
93
 * @param dst       Second string to compare.
93
 * @param dst       Second string to compare.
94
 * @param len       Maximal length for comparison.
94
 * @param len       Maximal length for comparison.
95
 *
95
 *
96
 * @return      0 if the strings are equal, -1 if first is smaller,
96
 * @return      0 if the strings are equal, -1 if first is smaller,
97
 *          1 if second smaller.
97
 *          1 if second smaller.
98
 *
98
 *
99
 */
99
 */
100
int strncmp(const char *src, const char *dst, size_t len)
100
int strncmp(const char *src, const char *dst, size_t len)
101
{
101
{
102
    int i;
102
    int i;
103
   
103
   
104
    for (i = 0; *src && *dst && i < len; src++, dst++, i++) {
104
    for (i = 0; *src && *dst && i < len; src++, dst++, i++) {
105
        if (*src < *dst)
105
        if (*src < *dst)
106
            return -1;
106
            return -1;
107
        if (*src > *dst)
107
        if (*src > *dst)
108
            return 1;
108
            return 1;
109
    }
109
    }
110
    if (i == len || *src == *dst)
110
    if (i == len || *src == *dst)
111
        return 0;
111
        return 0;
112
    if (!*src)
112
    if (!*src)
113
        return -1;
113
        return -1;
114
    return 1;
114
    return 1;
115
}
115
}
116
 
116
 
117
/** Copy NULL terminated string.
117
/** Copy NULL terminated string.
118
 *
118
 *
119
 * Copy at most 'len' characters from string 'src' to 'dest'.
119
 * Copy at most 'len' characters from string 'src' to 'dest'.
120
 * If 'src' is shorter than 'len', '\0' is inserted behind the
120
 * If 'src' is shorter than 'len', '\0' is inserted behind the
121
 * last copied character.
121
 * last copied character.
122
 *
122
 *
123
 * @param src       Source string.
123
 * @param src       Source string.
124
 * @param dest      Destination buffer.
124
 * @param dest      Destination buffer.
125
 * @param len       Size of destination buffer.
125
 * @param len       Size of destination buffer.
126
 */
126
 */
127
void strncpy(char *dest, const char *src, size_t len)
127
void strncpy(char *dest, const char *src, size_t len)
128
{
128
{
129
    int i;
129
    int i;
130
    for (i = 0; i < len; i++) {
130
    for (i = 0; i < len; i++) {
131
        if (!(dest[i] = src[i]))
131
        if (!(dest[i] = src[i]))
132
            return;
132
            return;
133
    }
133
    }
134
    dest[i-1] = '\0';
134
    dest[i-1] = '\0';
135
}
135
}
136
 
136
 
137
/** Convert ascii representation to unative_t.
137
/** Convert ascii representation to unative_t.
138
 *
138
 *
139
 * Supports 0x for hexa & 0 for octal notation.
139
 * Supports 0x for hexa & 0 for octal notation.
140
 * Does not check for overflows, does not support negative numbers
140
 * Does not check for overflows, does not support negative numbers
141
 *
141
 *
142
 * @param text      Textual representation of number.
142
 * @param text      Textual representation of number.
143
 * @return      Converted number or 0 if no valid number found.
143
 * @return      Converted number or 0 if no valid number found.
144
 */
144
 */
145
unative_t atoi(const char *text)
145
unative_t atoi(const char *text)
146
{
146
{
147
    int base = 10;
147
    int base = 10;
148
    unative_t result = 0;
148
    unative_t result = 0;
149
 
149
 
150
    if (text[0] == '0' && text[1] == 'x') {
150
    if (text[0] == '0' && text[1] == 'x') {
151
        base = 16;
151
        base = 16;
152
        text += 2;
152
        text += 2;
153
    } else if (text[0] == '0')
153
    } else if (text[0] == '0')
154
        base = 8;
154
        base = 8;
155
 
155
 
156
    while (*text) {
156
    while (*text) {
157
        if (base != 16 &&
157
        if (base != 16 &&
158
            ((*text >= 'A' && *text <= 'F') ||
158
            ((*text >= 'A' && *text <= 'F') ||
159
            (*text >='a' && *text <='f')))
159
            (*text >='a' && *text <='f')))
160
            break;
160
            break;
161
        if (base == 8 && *text >='8')
161
        if (base == 8 && *text >='8')
162
            break;
162
            break;
163
 
163
 
164
        if (*text >= '0' && *text <= '9') {
164
        if (*text >= '0' && *text <= '9') {
165
            result *= base;
165
            result *= base;
166
            result += *text - '0';
166
            result += *text - '0';
167
        } else if (*text >= 'A' && *text <= 'F') {
167
        } else if (*text >= 'A' && *text <= 'F') {
168
            result *= base;
168
            result *= base;
169
            result += *text - 'A' + 10;
169
            result += *text - 'A' + 10;
170
        } else if (*text >= 'a' && *text <= 'f') {
170
        } else if (*text >= 'a' && *text <= 'f') {
171
            result *= base;
171
            result *= base;
172
            result += *text - 'a' + 10;
172
            result += *text - 'a' + 10;
173
        } else
173
        } else
174
            break;
174
            break;
175
        text++;
175
        text++;
176
    }
176
    }
177
 
177
 
178
    return result;
178
    return result;
179
}
179
}
180
 
180
 
181
/** Move piece of memory to another, possibly overlapping, location.
181
/** Move piece of memory to another, possibly overlapping, location.
182
 *
182
 *
183
 * @param dst       Destination address.
183
 * @param dst       Destination address.
184
 * @param src       Source address.
184
 * @param src       Source address.
185
 * @param len       Number of bytes to move.
185
 * @param len       Number of bytes to move.
186
 *
186
 *
187
 * @return      Destination address.
187
 * @return      Destination address.
188
 */
188
 */
189
void *memmove(void *dst, const void *src, size_t len)
189
void *memmove(void *dst, const void *src, size_t len)
190
{
190
{
191
    char *d = dst;
191
    char *d = dst;
192
    const char *s = src;
192
    const char *s = src;
193
    if (s < d) {
193
    if (s < d) {
194
        while (len--)
194
        while (len--)
195
            *(d + len) = *(s + len);
195
            *(d + len) = *(s + len);
196
    } else {
196
    } else {
197
        while (len--)
197
        while (len--)
198
            *d++ = *s++;
198
            *d++ = *s++;
199
    }
199
    }
200
   
200
   
201
    return dst;
201
    return dst;
202
}
202
}
203
 
203
 
204
/** @}
204
/** @}
205
 */
205
 */
206
 
206