Subversion Repositories HelenOS

Rev

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

Rev 4207 Rev 4208
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 <errno.h>
43
#include <console/kconsole.h>
44
#include <console/kconsole.h>
44
 
45
 
45
char invalch = '?';
46
char invalch = '?';
46
 
47
 
47
/** Byte mask consisting of lowest @n bits (out of eight). */
48
/** Byte mask consisting of lowest @n bits (out of eight). */
48
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
49
#define LO_MASK_8(n) ((uint8_t)((1 << (n)) - 1))
49
 
50
 
50
/** Byte mask consisting of lowest @n bits (out of 32). */
51
/** Byte mask consisting of lowest @n bits (out of 32). */
51
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
52
#define LO_MASK_32(n) ((uint32_t)((1 << (n)) - 1))
52
 
53
 
53
/** Byte mask consisting of highest @n bits (out of eight). */
54
/** Byte mask consisting of highest @n bits (out of eight). */
54
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
55
#define HI_MASK_8(n) (~LO_MASK_8(8 - (n)))
55
 
56
 
56
/** Number of data bits in a UTF-8 continuation byte. */
57
/** Number of data bits in a UTF-8 continuation byte. */
57
#define CONT_BITS 6
58
#define CONT_BITS 6
58
 
59
 
59
/** Decode a single character from a substring.
60
/** Decode a single character from a substring.
60
 *
61
 *
61
 * Decode a single character from a substring of size @a sz. Decoding starts
62
 * Decode a single character from a substring of size @a sz. Decoding starts
62
 * at @a offset and this offset is moved to the beginning of the next
63
 * at @a offset and this offset is moved to the beginning of the next
63
 * character. In case of decoding error, offset generally advances at least
64
 * character. In case of decoding error, offset generally advances at least
64
 * by one. However, offset is never moved beyond (str + sz).
65
 * by one. However, offset is never moved beyond (str + sz).
65
 *
66
 *
66
 * @param str   String (not necessarily NULL-terminated).
67
 * @param str   String (not necessarily NULL-terminated).
67
 * @param index Index (counted in plain characters) where to start
68
 * @param index Index (counted in plain characters) where to start
68
 *              the decoding.
69
 *              the decoding.
69
 * @param limit Size of the substring.
70
 * @param limit Size of the substring.
70
 *
71
 *
71
 * @return  Value of decoded character or '?' on decoding error.
72
 * @return  Value of decoded character or '?' on decoding error.
72
 */
73
 */
73
wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
74
wchar_t chr_decode(const char *str, size_t *offset, size_t sz)
74
{
75
{
75
    uint8_t b0, b;          /* Bytes read from str. */
76
    uint8_t b0, b;          /* Bytes read from str. */
76
    wchar_t ch;
77
    wchar_t ch;
77
 
78
 
78
    int b0_bits;        /* Data bits in first byte. */
79
    int b0_bits;        /* Data bits in first byte. */
79
    int cbytes;     /* Number of continuation bytes. */
80
    int cbytes;     /* Number of continuation bytes. */
80
 
81
 
81
    if (*offset + 1 > sz)
82
    if (*offset + 1 > sz)
82
        return invalch;
83
        return invalch;
83
 
84
 
84
    b0 = (uint8_t) str[(*offset)++];
85
    b0 = (uint8_t) str[(*offset)++];
85
 
86
 
86
    /* Determine code length. */
87
    /* Determine code length. */
87
 
88
 
88
    if ((b0 & 0x80) == 0) {
89
    if ((b0 & 0x80) == 0) {
89
        /* 0xxxxxxx (Plain ASCII) */
90
        /* 0xxxxxxx (Plain ASCII) */
90
        b0_bits = 7;
91
        b0_bits = 7;
91
        cbytes = 0;
92
        cbytes = 0;
92
    } else if ((b0 & 0xe0) == 0xc0) {
93
    } else if ((b0 & 0xe0) == 0xc0) {
93
        /* 110xxxxx 10xxxxxx */
94
        /* 110xxxxx 10xxxxxx */
94
        b0_bits = 5;
95
        b0_bits = 5;
95
        cbytes = 1;
96
        cbytes = 1;
96
    } else if ((b0 & 0xf0) == 0xe0) {
97
    } else if ((b0 & 0xf0) == 0xe0) {
97
        /* 1110xxxx 10xxxxxx 10xxxxxx */
98
        /* 1110xxxx 10xxxxxx 10xxxxxx */
98
        b0_bits = 4;
99
        b0_bits = 4;
99
        cbytes = 2;
100
        cbytes = 2;
100
    } else if ((b0 & 0xf8) == 0xf0) {
101
    } else if ((b0 & 0xf8) == 0xf0) {
101
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
102
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
102
        b0_bits = 3;
103
        b0_bits = 3;
103
        cbytes = 3;
104
        cbytes = 3;
104
    } else {
105
    } else {
105
        /* 10xxxxxx -- unexpected continuation byte. */
106
        /* 10xxxxxx -- unexpected continuation byte. */
106
        return invalch;
107
        return invalch;
107
    }
108
    }
108
 
109
 
109
    if (*offset + cbytes > sz) {
110
    if (*offset + cbytes > sz) {
110
        return invalch;
111
        return invalch;
111
    }
112
    }
112
 
113
 
113
    ch = b0 & LO_MASK_8(b0_bits);
114
    ch = b0 & LO_MASK_8(b0_bits);
114
 
115
 
115
    /* Decode continuation bytes. */
116
    /* Decode continuation bytes. */
116
    while (cbytes > 0) {
117
    while (cbytes > 0) {
117
        b = (uint8_t) str[(*offset)++];
118
        b = (uint8_t) str[(*offset)++];
118
 
119
 
119
        /* Must be 10xxxxxx. */
120
        /* Must be 10xxxxxx. */
120
        if ((b & 0xc0) != 0x80) {
121
        if ((b & 0xc0) != 0x80) {
121
            return invalch;
122
            return invalch;
122
        }
123
        }
123
 
124
 
124
        /* Shift data bits to ch. */
125
        /* Shift data bits to ch. */
125
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
126
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
126
        --cbytes;
127
        --cbytes;
127
    }
128
    }
128
 
129
 
129
    return ch;
130
    return ch;
130
}
131
}
131
 
132
 
132
/** Encode a single character to string representation.
133
/** Encode a single character to string representation.
133
 *
134
 *
134
 * Encode a single character to string representation (i.e. UTF-8) and store
135
 * Encode a single character to string representation (i.e. UTF-8) and store
135
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
136
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
136
 * is moved to the position where the next character can be written to.
137
 * is moved to the position where the next character can be written to.
137
 *
138
 *
138
 * @param ch        Input character.
139
 * @param ch        Input character.
139
 * @param str       Output buffer.
140
 * @param str       Output buffer.
140
 * @param offset    Offset (in bytes) where to start writing.
141
 * @param offset    Offset (in bytes) where to start writing.
141
 * @param sz        Size of the output buffer.
142
 * @param sz        Size of the output buffer.
142
 *
143
 *
143
 * @return True if the character was encoded successfully or false if there
144
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
144
 *     was not enough space in the output buffer or the character code
145
 *     was not enough space in the output buffer or EINVAL if the character
145
 *     was invalid.
146
 *     code was invalid.
146
 */
147
 */
147
bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
148
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz)
148
{
149
{
149
    uint32_t cc;        /* Unsigned version of ch. */
150
    uint32_t cc;        /* Unsigned version of ch. */
150
 
151
 
151
    int cbytes;     /* Number of continuation bytes. */
152
    int cbytes;     /* Number of continuation bytes. */
152
    int b0_bits;        /* Number of data bits in first byte. */
153
    int b0_bits;        /* Number of data bits in first byte. */
153
    int i;
154
    int i;
154
 
155
 
155
    if (*offset >= sz)
156
    if (*offset >= sz)
156
        return false;
157
        return EOVERFLOW;
157
 
158
 
158
    if (ch < 0)
159
    if (ch < 0)
159
        return false;
160
        return EINVAL;
160
 
161
 
161
    /* Bit operations should only be done on unsigned numbers. */
162
    /* Bit operations should only be done on unsigned numbers. */
162
    cc = (uint32_t) ch;
163
    cc = (uint32_t) ch;
163
 
164
 
164
    /* Determine how many continuation bytes are needed. */
165
    /* Determine how many continuation bytes are needed. */
165
    if ((cc & ~LO_MASK_32(7)) == 0) {
166
    if ((cc & ~LO_MASK_32(7)) == 0) {
166
        b0_bits = 7;
167
        b0_bits = 7;
167
        cbytes = 0;
168
        cbytes = 0;
168
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
169
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
169
        b0_bits = 5;
170
        b0_bits = 5;
170
        cbytes = 1;
171
        cbytes = 1;
171
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
172
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
172
        b0_bits = 4;
173
        b0_bits = 4;
173
        cbytes = 2;
174
        cbytes = 2;
174
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
175
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
175
        b0_bits = 3;
176
        b0_bits = 3;
176
        cbytes = 3;
177
        cbytes = 3;
177
    } else {
178
    } else {
178
        /* Codes longer than 21 bits are not supported. */
179
        /* Codes longer than 21 bits are not supported. */
179
        return false;
180
        return EINVAL;
180
    }
181
    }
181
 
182
 
182
    /* Check for available space in buffer. */
183
    /* Check for available space in buffer. */
183
    if (*offset + cbytes >= sz)
184
    if (*offset + cbytes >= sz)
184
        return false;
185
        return EOVERFLOW;
185
 
186
 
186
    /* Encode continuation bytes. */
187
    /* Encode continuation bytes. */
187
    for (i = cbytes; i > 0; --i) {
188
    for (i = cbytes; i > 0; --i) {
188
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
189
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
189
        cc = cc >> CONT_BITS;
190
        cc = cc >> CONT_BITS;
190
    }
191
    }
191
 
192
 
192
    /* Encode first byte. */
193
    /* Encode first byte. */
193
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
194
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
194
 
195
 
195
    /* Advance offset. */
196
    /* Advance offset. */
196
    *offset += (1 + cbytes);
197
    *offset += (1 + cbytes);
197
   
198
   
198
    return true;
199
    return EOK;
199
}
200
}
200
 
201
 
201
/** Get size of string, with length limit.
202
/** Get size of string, with length limit.
202
 *
203
 *
203
 * Get the number of bytes which are used by up to @a max_len first
204
 * Get the number of bytes which are used by up to @a max_len first
204
 * characters in the string @a str. If @a max_len is greater than
205
 * characters in the string @a str. If @a max_len is greater than
205
 * the length of @a str, the entire string is measured.
206
 * the length of @a str, the entire string is measured.
206
 *
207
 *
207
 * @param str   String to consider.
208
 * @param str   String to consider.
208
 * @param count Maximum number of characters to measure.
209
 * @param count Maximum number of characters to measure.
209
 *
210
 *
210
 * @return Number of bytes used by the characters.
211
 * @return Number of bytes used by the characters.
211
 */
212
 */
212
size_t str_lsize(const char *str, count_t max_len)
213
size_t str_lsize(const char *str, count_t max_len)
213
{
214
{
214
    count_t len = 0;
215
    count_t len = 0;
215
    size_t cur = 0;
216
    size_t cur = 0;
216
    size_t prev;
217
    size_t prev;
217
    wchar_t ch;
218
    wchar_t ch;
218
 
219
 
219
    while (true) {
220
    while (true) {
220
        prev = cur;
221
        prev = cur;
221
        if (len >= max_len)
222
        if (len >= max_len)
222
            break;
223
            break;
223
        ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
224
        ch = chr_decode(str, &cur, UTF8_NO_LIMIT);
224
        if (ch == '\0') break;
225
        if (ch == '\0') break;
225
 
226
 
226
        len++;
227
        len++;
227
    }
228
    }
228
 
229
 
229
    return prev;
230
    return prev;
230
}
231
}
231
 
232
 
232
/** Check whether character is plain ASCII.
233
/** Check whether character is plain ASCII.
233
 *
234
 *
234
 * @return True if character is plain ASCII.
235
 * @return True if character is plain ASCII.
235
 *
236
 *
236
 */
237
 */
237
bool ascii_check(const wchar_t ch)
238
bool ascii_check(const wchar_t ch)
238
{
239
{
239
    if ((ch >= 0) && (ch <= 127))
240
    if ((ch >= 0) && (ch <= 127))
240
        return true;
241
        return true;
241
   
242
   
242
    return false;
243
    return false;
243
}
244
}
244
 
245
 
245
/** Check whether character is Unicode.
246
/** Check whether character is Unicode.
246
 *
247
 *
247
 * @return True if character is valid Unicode code point.
248
 * @return True if character is valid Unicode code point.
248
 */
249
 */
249
bool unicode_check(const wchar_t ch)
250
bool unicode_check(const wchar_t ch)
250
{
251
{
251
    if ((ch >= 0) && (ch <= 1114111))
252
    if ((ch >= 0) && (ch <= 1114111))
252
        return true;
253
        return true;
253
   
254
   
254
    return false;
255
    return false;
255
}
256
}
256
 
257
 
257
/** Return number of bytes the string occupies.
258
/** Return number of bytes the string occupies.
258
 *
259
 *
259
 * @param str A string.
260
 * @param str A string.
260
 * @return Number of bytes in @a str excluding the null terminator.
261
 * @return Number of bytes in @a str excluding the null terminator.
261
 */
262
 */
262
size_t str_size(const char *str)
263
size_t str_size(const char *str)
263
{
264
{
264
    size_t size;
265
    size_t size;
265
 
266
 
266
    size = 0;
267
    size = 0;
267
    while (*str++ != '\0')
268
    while (*str++ != '\0')
268
        ++size;
269
        ++size;
269
 
270
 
270
    return size;
271
    return size;
271
}
272
}
272
 
273
 
273
/** Return number of characters in a string.
274
/** Return number of characters in a string.
274
 *
275
 *
275
 * @param str NULL-terminated string.
276
 * @param str NULL-terminated string.
276
 * @return Number of characters in string.
277
 * @return Number of characters in string.
277
 */
278
 */
278
count_t str_length(const char *str)
279
count_t str_length(const char *str)
279
{
280
{
280
    count_t len = 0;
281
    count_t len = 0;
281
    size_t offset = 0;
282
    size_t offset = 0;
282
 
283
 
283
    while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
284
    while (chr_decode(str, &offset, UTF8_NO_LIMIT) != 0) {
284
        len++;
285
        len++;
285
    }
286
    }
286
 
287
 
287
    return len;
288
    return len;
288
}
289
}
289
 
290
 
290
/** Return number of characters in a wide string.
291
/** Return number of characters in a wide string.
291
 *
292
 *
292
 * @param str NULL-terminated wide string.
293
 * @param str NULL-terminated wide string.
293
 * @return Number of characters in @a str.
294
 * @return Number of characters in @a str.
294
 */
295
 */
295
count_t wstr_length(const wchar_t *wstr)
296
count_t wstr_length(const wchar_t *wstr)
296
{
297
{
297
    count_t len;
298
    count_t len;
298
 
299
 
299
    len = 0;
300
    len = 0;
300
    while (*wstr++ != '\0')
301
    while (*wstr++ != '\0')
301
        ++len;
302
        ++len;
302
 
303
 
303
    return len;
304
    return len;
304
}
305
}
305
 
306
 
306
/** Compare two NULL terminated strings
307
/** Compare two NULL terminated strings
307
 *
308
 *
308
 * Do a char-by-char comparison of two NULL terminated strings.
309
 * Do a char-by-char comparison of two NULL terminated strings.
309
 * The strings are considered equal iff they consist of the same
310
 * The strings are considered equal iff they consist of the same
310
 * characters on the minimum of their lengths.
311
 * characters on the minimum of their lengths.
311
 *
312
 *
312
 * @param src First string to compare.
313
 * @param src First string to compare.
313
 * @param dst Second string to compare.
314
 * @param dst Second string to compare.
314
 *
315
 *
315
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
316
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
316
 *
317
 *
317
 */
318
 */
318
int strcmp(const char *src, const char *dst)
319
int strcmp(const char *src, const char *dst)
319
{
320
{
320
    for (; *src && *dst; src++, dst++) {
321
    for (; *src && *dst; src++, dst++) {
321
        if (*src < *dst)
322
        if (*src < *dst)
322
            return -1;
323
            return -1;
323
        if (*src > *dst)
324
        if (*src > *dst)
324
            return 1;
325
            return 1;
325
    }
326
    }
326
    if (*src == *dst)
327
    if (*src == *dst)
327
        return 0;
328
        return 0;
328
   
329
   
329
    if (!*src)
330
    if (!*src)
330
        return -1;
331
        return -1;
331
   
332
   
332
    return 1;
333
    return 1;
333
}
334
}
334
 
335
 
335
 
336
 
336
/** Compare two NULL terminated strings
337
/** Compare two NULL terminated strings
337
 *
338
 *
338
 * Do a char-by-char comparison of two NULL terminated strings.
339
 * Do a char-by-char comparison of two NULL terminated strings.
339
 * The strings are considered equal iff they consist of the same
340
 * The strings are considered equal iff they consist of the same
340
 * characters on the minimum of their lengths and specified maximal
341
 * characters on the minimum of their lengths and specified maximal
341
 * length.
342
 * length.
342
 *
343
 *
343
 * @param src First string to compare.
344
 * @param src First string to compare.
344
 * @param dst Second string to compare.
345
 * @param dst Second string to compare.
345
 * @param len Maximal length for comparison.
346
 * @param len Maximal length for comparison.
346
 *
347
 *
347
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
348
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
348
 */
349
 */
349
int strncmp(const char *src, const char *dst, size_t len)
350
int strncmp(const char *src, const char *dst, size_t len)
350
{
351
{
351
    unsigned int i;
352
    unsigned int i;
352
   
353
   
353
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
354
    for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
354
        if (*src < *dst)
355
        if (*src < *dst)
355
            return -1;
356
            return -1;
356
       
357
       
357
        if (*src > *dst)
358
        if (*src > *dst)
358
            return 1;
359
            return 1;
359
    }
360
    }
360
   
361
   
361
    if (i == len || *src == *dst)
362
    if (i == len || *src == *dst)
362
        return 0;
363
        return 0;
363
   
364
   
364
    if (!*src)
365
    if (!*src)
365
        return -1;
366
        return -1;
366
   
367
   
367
    return 1;
368
    return 1;
368
}
369
}
369
 
370
 
370
 
371
 
371
 
372
 
372
/** Copy NULL terminated string.
373
/** Copy NULL terminated string.
373
 *
374
 *
374
 * Copy at most 'len' characters from string 'src' to 'dest'.
375
 * Copy at most 'len' characters from string 'src' to 'dest'.
375
 * If 'src' is shorter than 'len', '\0' is inserted behind the
376
 * If 'src' is shorter than 'len', '\0' is inserted behind the
376
 * last copied character.
377
 * last copied character.
377
 *
378
 *
378
 * @param src  Source string.
379
 * @param src  Source string.
379
 * @param dest Destination buffer.
380
 * @param dest Destination buffer.
380
 * @param len  Size of destination buffer.
381
 * @param len  Size of destination buffer.
381
 */
382
 */
382
void strncpy(char *dest, const char *src, size_t len)
383
void strncpy(char *dest, const char *src, size_t len)
383
{
384
{
384
    unsigned int i;
385
    unsigned int i;
385
   
386
   
386
    for (i = 0; i < len; i++) {
387
    for (i = 0; i < len; i++) {
387
        if (!(dest[i] = src[i]))
388
        if (!(dest[i] = src[i]))
388
            return;
389
            return;
389
    }
390
    }
390
   
391
   
391
    dest[i - 1] = '\0';
392
    dest[i - 1] = '\0';
392
}
393
}
393
 
394
 
394
/** Find first occurence of character in string.
395
/** Find first occurence of character in string.
395
 *
396
 *
396
 * @param s String to search.
397
 * @param s String to search.
397
 * @param i Character to look for.
398
 * @param i Character to look for.
398
 *
399
 *
399
 * @return Pointer to character in @a s or NULL if not found.
400
 * @return Pointer to character in @a s or NULL if not found.
400
 */
401
 */
401
extern char *strchr(const char *s, int i)
402
extern char *strchr(const char *s, int i)
402
{
403
{
403
    while (*s != '\0') {
404
    while (*s != '\0') {
404
        if (*s == i)
405
        if (*s == i)
405
            return (char *) s;
406
            return (char *) s;
406
        ++s;
407
        ++s;
407
    }
408
    }
408
   
409
   
409
    return NULL;
410
    return NULL;
410
}
411
}
411
 
412
 
412
/** @}
413
/** @}
413
 */
414
 */
414
 
415