Subversion Repositories HelenOS

Rev

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

Rev 4011 Rev 4012
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
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Miscellaneous functions.
35
 * @brief   Miscellaneous functions.
36
 */
36
 */
37
 
37
 
38
#include <string.h>
38
#include <string.h>
39
#include <print.h>
39
#include <print.h>
40
#include <cpu.h>
40
#include <cpu.h>
41
#include <arch/asm.h>
41
#include <arch/asm.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <console/kconsole.h>
43
#include <console/kconsole.h>
44
 
44
 
45
/** Return number of characters in a string.
45
/** Return number of characters in a string.
46
 *
46
 *
47
 * @param str NULL terminated string.
47
 * @param str NULL terminated string.
48
 *
48
 *
49
 * @return Number of characters in str.
49
 * @return Number of characters in str.
50
 */
50
 */
51
size_t strlen(const char *str)
51
size_t strlen(const char *str)
52
{
52
{
53
    int i;
53
    int i;
54
   
54
   
55
    for (i = 0; str[i]; i++)
55
    for (i = 0; str[i]; i++)
56
        ;
56
        ;
57
   
57
   
58
    return i;
58
    return i;
59
}
59
}
60
 
60
 
61
/** Compare two NULL terminated strings
61
/** Compare two NULL terminated strings
62
 *
62
 *
63
 * Do a char-by-char comparison of two NULL terminated strings.
63
 * Do a char-by-char comparison of two NULL terminated strings.
64
 * The strings are considered equal iff they consist of the same
64
 * The strings are considered equal iff they consist of the same
65
 * characters on the minimum of their lengths.
65
 * characters on the minimum of their lengths.
66
 *
66
 *
67
 * @param src First string to compare.
67
 * @param src First string to compare.
68
 * @param dst Second string to compare.
68
 * @param dst Second string to compare.
69
 *
69
 *
70
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
70
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
71
 *
71
 *
72
 */
72
 */
73
int strcmp(const char *src, const char *dst)
73
int strcmp(const char *src, const char *dst)
74
{
74
{
75
    for (; *src && *dst; src++, dst++) {
75
    for (; *src && *dst; src++, dst++) {
76
        if (*src < *dst)
76
        if (*src < *dst)
77
            return -1;
77
            return -1;
78
        if (*src > *dst)
78
        if (*src > *dst)
79
            return 1;
79
            return 1;
80
    }
80
    }
81
    if (*src == *dst)
81
    if (*src == *dst)
82
        return 0;
82
        return 0;
83
    if (!*src)
83
    if (!*src)
84
        return -1;
84
        return -1;
85
    return 1;
85
    return 1;
86
}
86
}
87
 
87
 
88
 
88
 
89
/** Compare two NULL terminated strings
89
/** Compare two NULL terminated strings
90
 *
90
 *
91
 * Do a char-by-char comparison of two NULL terminated strings.
91
 * Do a char-by-char comparison of two NULL terminated strings.
92
 * The strings are considered equal iff they consist of the same
92
 * The strings are considered equal iff they consist of the same
93
 * characters on the minimum of their lengths and specified maximal
93
 * characters on the minimum of their lengths and specified maximal
94
 * length.
94
 * length.
95
 *
95
 *
96
 * @param src First string to compare.
96
 * @param src First string to compare.
97
 * @param dst Second string to compare.
97
 * @param dst Second string to compare.
98
 * @param len Maximal length for comparison.
98
 * @param len Maximal length for comparison.
99
 *
99
 *
100
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
100
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
101
 *
101
 *
102
 */
102
 */
103
int strncmp(const char *src, const char *dst, size_t len)
103
int strncmp(const char *src, const char *dst, size_t len)
104
{
104
{
105
    unsigned int i;
105
    unsigned int i;
106
   
106
   
107
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
107
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
108
        if (*src < *dst)
108
        if (*src < *dst)
109
            return -1;
109
            return -1;
110
        if (*src > *dst)
110
        if (*src > *dst)
111
            return 1;
111
            return 1;
112
    }
112
    }
113
    if (i == len || *src == *dst)
113
    if (i == len || *src == *dst)
114
        return 0;
114
        return 0;
115
    if (!*src)
115
    if (!*src)
116
        return -1;
116
        return -1;
117
    return 1;
117
    return 1;
118
}
118
}
119
 
119
 
120
 
120
 
121
 
121
 
122
/** Copy NULL terminated string.
122
/** Copy NULL terminated string.
123
 *
123
 *
124
 * Copy at most 'len' characters from string 'src' to 'dest'.
124
 * Copy at most 'len' characters from string 'src' to 'dest'.
125
 * If 'src' is shorter than 'len', '\0' is inserted behind the
125
 * If 'src' is shorter than 'len', '\0' is inserted behind the
126
 * last copied character.
126
 * last copied character.
127
 *
127
 *
128
 * @param src Source string.
128
 * @param src Source string.
129
 * @param dest Destination buffer.
129
 * @param dest Destination buffer.
130
 * @param len Size of destination buffer.
130
 * @param len Size of destination buffer.
131
 */
131
 */
132
void strncpy(char *dest, const char *src, size_t len)
132
void strncpy(char *dest, const char *src, size_t len)
133
{
133
{
134
    unsigned int i;
134
    unsigned int i;
135
 
135
 
136
    for (i = 0; i < len; i++) {
136
    for (i = 0; i < len; i++) {
137
        if (!(dest[i] = src[i]))
137
        if (!(dest[i] = src[i]))
138
            return;
138
            return;
139
    }
139
    }
140
 
140
 
141
    dest[i - 1] = '\0';
141
    dest[i - 1] = '\0';
142
}
142
}
143
 
143
 
144
/** Copy string.
144
/** Copy string.
145
 *
145
 *
146
 * Copy string from src address to dst address.  The copying is done
146
 * Copy string from src address to dst address.  The copying is done
147
 * char-by-char until the null character. The source and destination memory
147
 * char-by-char until the null character. The source and destination memory
148
 * areas cannot overlap.
148
 * areas cannot overlap.
149
 *
149
 *
150
 * @param src       Source string to copy from.
150
 * @param src       Source string to copy from.
151
 * @param dst       Destination string to copy to.
151
 * @param dst       Destination string to copy to.
152
 *
152
 *
153
 * @return      Address of the destination string.
153
 * @return      Address of the destination string.
154
 */
154
 */
155
char *strcpy(char *dest, const char *src)
155
char *strcpy(char *dest, const char *src)
156
{
156
{
157
    char *orig = dest;
157
    char *orig = dest;
158
   
158
   
159
    while ((*dest++ = *src++) != '\0');
159
    while ((*dest++ = *src++) != '\0');
160
 
160
 
161
    return orig;
161
    return orig;
162
}
162
}
163
 
163
 
-
 
164
/** Find first occurence of character in string.
-
 
165
 *
-
 
166
 * @param s String to search.
-
 
167
 * @param i Character to look for.
-
 
168
 *
-
 
169
 * @return  Pointer to character in @a s or NULL if not found.
-
 
170
 */
-
 
171
extern char *strchr(const char *s, int i)
-
 
172
{
-
 
173
    while (*s != '\0') {
-
 
174
        if (*s == i) return (char *) s;
-
 
175
        ++s;
-
 
176
    }
-
 
177
 
-
 
178
    return NULL;
-
 
179
}
-
 
180
 
-
 
181
/** Find last occurence of character in string.
-
 
182
 *
-
 
183
 * @param s String to search.
-
 
184
 * @param i Character to look for.
-
 
185
 *
-
 
186
 * @return  Pointer to character in @a s or NULL if not found.
-
 
187
 */
-
 
188
extern char *strrchr(const char *s, int i)
-
 
189
{
-
 
190
    const char *start;
-
 
191
 
-
 
192
    start = s;
-
 
193
    if (*s == '\0') return NULL;
-
 
194
 
-
 
195
    while (*s != '\0') ++s;
-
 
196
 
-
 
197
    while (s != start) {
-
 
198
        --s;
-
 
199
        if (*s == i) return (char *) s;
-
 
200
    }
-
 
201
 
-
 
202
    return NULL;
-
 
203
}
-
 
204
 
164
/** @}
205
/** @}
165
 */
206
 */
166
 
207