Subversion Repositories HelenOS

Rev

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

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