Subversion Repositories HelenOS

Rev

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

Rev 4234 Rev 4264
1
/*
1
/*
2
 * Copyright (c) 2005 Martin Decky
2
 * Copyright (c) 2005 Martin Decky
3
 * Copyright (c) 2008 Jiri Svoboda
3
 * Copyright (c) 2008 Jiri Svoboda
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libc
30
/** @addtogroup libc
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <string.h>
36
#include <string.h>
37
#include <stdlib.h>
37
#include <stdlib.h>
38
#include <limits.h>
38
#include <limits.h>
39
#include <ctype.h>
39
#include <ctype.h>
40
#include <malloc.h>
40
#include <malloc.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <align.h>
42
#include <align.h>
43
#include <string.h>
43
#include <string.h>
44
 
44
 
45
/** Byte mask consisting of lowest @n bits (out of 8) */
45
/** Byte mask consisting of lowest @n bits (out of 8) */
46
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
46
#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
47
 
47
 
48
/** Byte mask consisting of lowest @n bits (out of 32) */
48
/** Byte mask consisting of lowest @n bits (out of 32) */
49
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
49
#define LO_MASK_32(n)  ((uint32_t) ((1 << (n)) - 1))
50
 
50
 
51
/** Byte mask consisting of highest @n bits (out of 8) */
51
/** Byte mask consisting of highest @n bits (out of 8) */
52
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
52
#define HI_MASK_8(n)  (~LO_MASK_8(8 - (n)))
53
 
53
 
54
/** Number of data bits in a UTF-8 continuation byte */
54
/** Number of data bits in a UTF-8 continuation byte */
55
#define CONT_BITS  6
55
#define CONT_BITS  6
56
 
56
 
57
/** Decode a single character from a string.
57
/** Decode a single character from a string.
58
 *
58
 *
59
 * Decode a single character from a string of size @a size. Decoding starts
59
 * Decode a single character from a string of size @a size. Decoding starts
60
 * at @a offset and this offset is moved to the beginning of the next
60
 * at @a offset and this offset is moved to the beginning of the next
61
 * character. In case of decoding error, offset generally advances at least
61
 * character. In case of decoding error, offset generally advances at least
62
 * by one. However, offset is never moved beyond size.
62
 * by one. However, offset is never moved beyond size.
63
 *
63
 *
64
 * @param str    String (not necessarily NULL-terminated).
64
 * @param str    String (not necessarily NULL-terminated).
65
 * @param offset Byte offset in string where to start decoding.
65
 * @param offset Byte offset in string where to start decoding.
66
 * @param size   Size of the string (in bytes).
66
 * @param size   Size of the string (in bytes).
67
 *
67
 *
68
 * @return Value of decoded character, U_SPECIAL on decoding error or
68
 * @return Value of decoded character, U_SPECIAL on decoding error or
69
 *         NULL if attempt to decode beyond @a size.
69
 *         NULL if attempt to decode beyond @a size.
70
 *
70
 *
71
 */
71
 */
72
wchar_t str_decode(const char *str, size_t *offset, size_t size)
72
wchar_t str_decode(const char *str, size_t *offset, size_t size)
73
{
73
{
74
    if (*offset + 1 > size)
74
    if (*offset + 1 > size)
75
        return 0;
75
        return 0;
76
   
76
   
77
    /* First byte read from string */
77
    /* First byte read from string */
78
    uint8_t b0 = (uint8_t) str[(*offset)++];
78
    uint8_t b0 = (uint8_t) str[(*offset)++];
79
   
79
   
80
    /* Determine code length */
80
    /* Determine code length */
81
   
81
   
82
    unsigned int b0_bits;  /* Data bits in first byte */
82
    unsigned int b0_bits;  /* Data bits in first byte */
83
    unsigned int cbytes;   /* Number of continuation bytes */
83
    unsigned int cbytes;   /* Number of continuation bytes */
84
   
84
   
85
    if ((b0 & 0x80) == 0) {
85
    if ((b0 & 0x80) == 0) {
86
        /* 0xxxxxxx (Plain ASCII) */
86
        /* 0xxxxxxx (Plain ASCII) */
87
        b0_bits = 7;
87
        b0_bits = 7;
88
        cbytes = 0;
88
        cbytes = 0;
89
    } else if ((b0 & 0xe0) == 0xc0) {
89
    } else if ((b0 & 0xe0) == 0xc0) {
90
        /* 110xxxxx 10xxxxxx */
90
        /* 110xxxxx 10xxxxxx */
91
        b0_bits = 5;
91
        b0_bits = 5;
92
        cbytes = 1;
92
        cbytes = 1;
93
    } else if ((b0 & 0xf0) == 0xe0) {
93
    } else if ((b0 & 0xf0) == 0xe0) {
94
        /* 1110xxxx 10xxxxxx 10xxxxxx */
94
        /* 1110xxxx 10xxxxxx 10xxxxxx */
95
        b0_bits = 4;
95
        b0_bits = 4;
96
        cbytes = 2;
96
        cbytes = 2;
97
    } else if ((b0 & 0xf8) == 0xf0) {
97
    } else if ((b0 & 0xf8) == 0xf0) {
98
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
98
        /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
99
        b0_bits = 3;
99
        b0_bits = 3;
100
        cbytes = 3;
100
        cbytes = 3;
101
    } else {
101
    } else {
102
        /* 10xxxxxx -- unexpected continuation byte */
102
        /* 10xxxxxx -- unexpected continuation byte */
103
        return U_SPECIAL;
103
        return U_SPECIAL;
104
    }
104
    }
105
   
105
   
106
    if (*offset + cbytes > size)
106
    if (*offset + cbytes > size)
107
        return U_SPECIAL;
107
        return U_SPECIAL;
108
   
108
   
109
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
109
    wchar_t ch = b0 & LO_MASK_8(b0_bits);
110
   
110
   
111
    /* Decode continuation bytes */
111
    /* Decode continuation bytes */
112
    while (cbytes > 0) {
112
    while (cbytes > 0) {
113
        uint8_t b = (uint8_t) str[(*offset)++];
113
        uint8_t b = (uint8_t) str[(*offset)++];
114
       
114
       
115
        /* Must be 10xxxxxx */
115
        /* Must be 10xxxxxx */
116
        if ((b & 0xc0) != 0x80)
116
        if ((b & 0xc0) != 0x80)
117
            return U_SPECIAL;
117
            return U_SPECIAL;
118
       
118
       
119
        /* Shift data bits to ch */
119
        /* Shift data bits to ch */
120
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
120
        ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
121
        cbytes--;
121
        cbytes--;
122
    }
122
    }
123
   
123
   
124
    return ch;
124
    return ch;
125
}
125
}
126
 
126
 
127
/** Encode a single character to string representation.
127
/** Encode a single character to string representation.
128
 *
128
 *
129
 * Encode a single character to string representation (i.e. UTF-8) and store
129
 * Encode a single character to string representation (i.e. UTF-8) and store
130
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
130
 * it into a buffer at @a offset. Encoding starts at @a offset and this offset
131
 * is moved to the position where the next character can be written to.
131
 * is moved to the position where the next character can be written to.
132
 *
132
 *
133
 * @param ch     Input character.
133
 * @param ch     Input character.
134
 * @param str    Output buffer.
134
 * @param str    Output buffer.
135
 * @param offset Byte offset where to start writing.
135
 * @param offset Byte offset where to start writing.
136
 * @param size   Size of the output buffer (in bytes).
136
 * @param size   Size of the output buffer (in bytes).
137
 *
137
 *
138
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
138
 * @return EOK if the character was encoded successfully, EOVERFLOW if there
139
 *     was not enough space in the output buffer or EINVAL if the character
139
 *     was not enough space in the output buffer or EINVAL if the character
140
 *     code was invalid.
140
 *     code was invalid.
141
 */
141
 */
142
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
142
int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
143
{
143
{
144
    if (*offset >= size)
144
    if (*offset >= size)
145
        return EOVERFLOW;
145
        return EOVERFLOW;
146
   
146
   
147
    if (!chr_check(ch))
147
    if (!chr_check(ch))
148
        return EINVAL;
148
        return EINVAL;
149
   
149
   
150
    /* Unsigned version of ch (bit operations should only be done
150
    /* Unsigned version of ch (bit operations should only be done
151
       on unsigned types). */
151
       on unsigned types). */
152
    uint32_t cc = (uint32_t) ch;
152
    uint32_t cc = (uint32_t) ch;
153
   
153
   
154
    /* Determine how many continuation bytes are needed */
154
    /* Determine how many continuation bytes are needed */
155
   
155
   
156
    unsigned int b0_bits;  /* Data bits in first byte */
156
    unsigned int b0_bits;  /* Data bits in first byte */
157
    unsigned int cbytes;   /* Number of continuation bytes */
157
    unsigned int cbytes;   /* Number of continuation bytes */
158
   
158
   
159
    if ((cc & ~LO_MASK_32(7)) == 0) {
159
    if ((cc & ~LO_MASK_32(7)) == 0) {
160
        b0_bits = 7;
160
        b0_bits = 7;
161
        cbytes = 0;
161
        cbytes = 0;
162
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
162
    } else if ((cc & ~LO_MASK_32(11)) == 0) {
163
        b0_bits = 5;
163
        b0_bits = 5;
164
        cbytes = 1;
164
        cbytes = 1;
165
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
165
    } else if ((cc & ~LO_MASK_32(16)) == 0) {
166
        b0_bits = 4;
166
        b0_bits = 4;
167
        cbytes = 2;
167
        cbytes = 2;
168
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
168
    } else if ((cc & ~LO_MASK_32(21)) == 0) {
169
        b0_bits = 3;
169
        b0_bits = 3;
170
        cbytes = 3;
170
        cbytes = 3;
171
    } else {
171
    } else {
172
        /* Codes longer than 21 bits are not supported */
172
        /* Codes longer than 21 bits are not supported */
173
        return EINVAL;
173
        return EINVAL;
174
    }
174
    }
175
   
175
   
176
    /* Check for available space in buffer */
176
    /* Check for available space in buffer */
177
    if (*offset + cbytes >= size)
177
    if (*offset + cbytes >= size)
178
        return EOVERFLOW;
178
        return EOVERFLOW;
179
   
179
   
180
    /* Encode continuation bytes */
180
    /* Encode continuation bytes */
181
    unsigned int i;
181
    unsigned int i;
182
    for (i = cbytes; i > 0; i--) {
182
    for (i = cbytes; i > 0; i--) {
183
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
183
        str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS));
184
        cc = cc >> CONT_BITS;
184
        cc = cc >> CONT_BITS;
185
    }
185
    }
186
   
186
   
187
    /* Encode first byte */
187
    /* Encode first byte */
188
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
188
    str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1);
189
   
189
   
190
    /* Advance offset */
190
    /* Advance offset */
191
    *offset += cbytes + 1;
191
    *offset += cbytes + 1;
192
   
192
   
193
    return EOK;
193
    return EOK;
194
}
194
}
195
 
195
 
196
/** Get size of string.
196
/** Get size of string.
197
 *
197
 *
198
 * Get the number of bytes which are used by the string @a str (excluding the
198
 * Get the number of bytes which are used by the string @a str (excluding the
199
 * NULL-terminator).
199
 * NULL-terminator).
200
 *
200
 *
201
 * @param str String to consider.
201
 * @param str String to consider.
202
 *
202
 *
203
 * @return Number of bytes used by the string
203
 * @return Number of bytes used by the string
204
 *
204
 *
205
 */
205
 */
206
size_t str_size(const char *str)
206
size_t str_size(const char *str)
207
{
207
{
208
    size_t size = 0;
208
    size_t size = 0;
209
   
209
   
210
    while (*str++ != 0)
210
    while (*str++ != 0)
211
        size++;
211
        size++;
212
   
212
   
213
    return size;
213
    return size;
214
}
214
}
215
 
215
 
216
/** Get size of wide string.
216
/** Get size of wide string.
217
 *
217
 *
218
 * Get the number of bytes which are used by the wide string @a str (excluding the
218
 * Get the number of bytes which are used by the wide string @a str (excluding the
219
 * NULL-terminator).
219
 * NULL-terminator).
220
 *
220
 *
221
 * @param str Wide string to consider.
221
 * @param str Wide string to consider.
222
 *
222
 *
223
 * @return Number of bytes used by the wide string
223
 * @return Number of bytes used by the wide string
224
 *
224
 *
225
 */
225
 */
226
size_t wstr_size(const wchar_t *str)
226
size_t wstr_size(const wchar_t *str)
227
{
227
{
228
    return (wstr_length(str) * sizeof(wchar_t));
228
    return (wstr_length(str) * sizeof(wchar_t));
229
}
229
}
230
 
230
 
231
/** Get size of string with length limit.
231
/** Get size of string with length limit.
232
 *
232
 *
233
 * Get the number of bytes which are used by up to @a max_len first
233
 * Get the number of bytes which are used by up to @a max_len first
234
 * characters in the string @a str. If @a max_len is greater than
234
 * characters in the string @a str. If @a max_len is greater than
235
 * the length of @a str, the entire string is measured (excluding the
235
 * the length of @a str, the entire string is measured (excluding the
236
 * NULL-terminator).
236
 * NULL-terminator).
237
 *
237
 *
238
 * @param str     String to consider.
238
 * @param str     String to consider.
239
 * @param max_len Maximum number of characters to measure.
239
 * @param max_len Maximum number of characters to measure.
240
 *
240
 *
241
 * @return Number of bytes used by the characters.
241
 * @return Number of bytes used by the characters.
242
 *
242
 *
243
 */
243
 */
244
size_t str_lsize(const char *str, count_t max_len)
244
size_t str_lsize(const char *str, count_t max_len)
245
{
245
{
246
    count_t len = 0;
246
    count_t len = 0;
247
    size_t offset = 0;
247
    size_t offset = 0;
248
   
248
   
249
    while (len < max_len) {
249
    while (len < max_len) {
250
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
250
        if (str_decode(str, &offset, STR_NO_LIMIT) == 0)
251
            break;
251
            break;
252
       
252
       
253
        len++;
253
        len++;
254
    }
254
    }
255
   
255
   
256
    return offset;
256
    return offset;
257
}
257
}
258
 
258
 
259
/** Get size of wide string with length limit.
259
/** Get size of wide string with length limit.
260
 *
260
 *
261
 * Get the number of bytes which are used by up to @a max_len first
261
 * Get the number of bytes which are used by up to @a max_len first
262
 * wide characters in the wide string @a str. If @a max_len is greater than
262
 * wide characters in the wide string @a str. If @a max_len is greater than
263
 * the length of @a str, the entire wide string is measured (excluding the
263
 * the length of @a str, the entire wide string is measured (excluding the
264
 * NULL-terminator).
264
 * NULL-terminator).
265
 *
265
 *
266
 * @param str     Wide string to consider.
266
 * @param str     Wide string to consider.
267
 * @param max_len Maximum number of wide characters to measure.
267
 * @param max_len Maximum number of wide characters to measure.
268
 *
268
 *
269
 * @return Number of bytes used by the wide characters.
269
 * @return Number of bytes used by the wide characters.
270
 *
270
 *
271
 */
271
 */
272
size_t wstr_lsize(const wchar_t *str, count_t max_len)
272
size_t wstr_lsize(const wchar_t *str, count_t max_len)
273
{
273
{
274
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
274
    return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
275
}
275
}
276
 
276
 
277
/** Get number of characters in a string.
277
/** Get number of characters in a string.
278
 *
278
 *
279
 * @param str NULL-terminated string.
279
 * @param str NULL-terminated string.
280
 *
280
 *
281
 * @return Number of characters in string.
281
 * @return Number of characters in string.
282
 *
282
 *
283
 */
283
 */
284
count_t str_length(const char *str)
284
count_t str_length(const char *str)
285
{
285
{
286
    count_t len = 0;
286
    count_t len = 0;
287
    size_t offset = 0;
287
    size_t offset = 0;
288
   
288
   
289
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
289
    while (str_decode(str, &offset, STR_NO_LIMIT) != 0)
290
        len++;
290
        len++;
291
   
291
   
292
    return len;
292
    return len;
293
}
293
}
294
 
294
 
295
/** Get number of characters in a wide string.
295
/** Get number of characters in a wide string.
296
 *
296
 *
297
 * @param str NULL-terminated wide string.
297
 * @param str NULL-terminated wide string.
298
 *
298
 *
299
 * @return Number of characters in @a str.
299
 * @return Number of characters in @a str.
300
 *
300
 *
301
 */
301
 */
302
count_t wstr_length(const wchar_t *wstr)
302
count_t wstr_length(const wchar_t *wstr)
303
{
303
{
304
    count_t len = 0;
304
    count_t len = 0;
305
   
305
   
306
    while (*wstr++ != 0)
306
    while (*wstr++ != 0)
307
        len++;
307
        len++;
308
   
308
   
309
    return len;
309
    return len;
310
}
310
}
311
 
311
 
312
/** Get number of characters in a string with size limit.
312
/** Get number of characters in a string with size limit.
313
 *
313
 *
314
 * @param str  NULL-terminated string.
314
 * @param str  NULL-terminated string.
315
 * @param size Maximum number of bytes to consider.
315
 * @param size Maximum number of bytes to consider.
316
 *
316
 *
317
 * @return Number of characters in string.
317
 * @return Number of characters in string.
318
 *
318
 *
319
 */
319
 */
320
count_t str_nlength(const char *str, size_t size)
320
count_t str_nlength(const char *str, size_t size)
321
{
321
{
322
    count_t len = 0;
322
    count_t len = 0;
323
    size_t offset = 0;
323
    size_t offset = 0;
324
   
324
   
325
    while (str_decode(str, &offset, size) != 0)
325
    while (str_decode(str, &offset, size) != 0)
326
        len++;
326
        len++;
327
   
327
   
328
    return len;
328
    return len;
329
}
329
}
330
 
330
 
331
/** Get number of characters in a string with size limit.
331
/** Get number of characters in a string with size limit.
332
 *
332
 *
333
 * @param str  NULL-terminated string.
333
 * @param str  NULL-terminated string.
334
 * @param size Maximum number of bytes to consider.
334
 * @param size Maximum number of bytes to consider.
335
 *
335
 *
336
 * @return Number of characters in string.
336
 * @return Number of characters in string.
337
 *
337
 *
338
 */
338
 */
339
count_t wstr_nlength(const wchar_t *str, size_t size)
339
count_t wstr_nlength(const wchar_t *str, size_t size)
340
{
340
{
341
    count_t len = 0;
341
    count_t len = 0;
342
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
342
    count_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
343
    count_t offset = 0;
343
    count_t offset = 0;
344
   
344
   
345
    while ((offset < limit) && (*str++ != 0)) {
345
    while ((offset < limit) && (*str++ != 0)) {
346
        len++;
346
        len++;
347
        offset += sizeof(wchar_t);
347
        offset += sizeof(wchar_t);
348
    }
348
    }
349
   
349
   
350
    return len;
350
    return len;
351
}
351
}
352
 
352
 
353
/** Check whether character is plain ASCII.
353
/** Check whether character is plain ASCII.
354
 *
354
 *
355
 * @return True if character is plain ASCII.
355
 * @return True if character is plain ASCII.
356
 *
356
 *
357
 */
357
 */
358
bool ascii_check(wchar_t ch)
358
bool ascii_check(wchar_t ch)
359
{
359
{
360
    if ((ch >= 0) && (ch <= 127))
360
    if ((ch >= 0) && (ch <= 127))
361
        return true;
361
        return true;
362
   
362
   
363
    return false;
363
    return false;
364
}
364
}
365
 
365
 
366
/** Check whether character is valid
366
/** Check whether character is valid
367
 *
367
 *
368
 * @return True if character is a valid Unicode code point.
368
 * @return True if character is a valid Unicode code point.
369
 *
369
 *
370
 */
370
 */
371
bool chr_check(wchar_t ch)
371
bool chr_check(wchar_t ch)
372
{
372
{
373
    if ((ch >= 0) && (ch <= 1114111))
373
    if ((ch >= 0) && (ch <= 1114111))
374
        return true;
374
        return true;
375
   
375
   
376
    return false;
376
    return false;
377
}
377
}
378
 
378
 
379
/** Compare two NULL terminated strings.
379
/** Compare two NULL terminated strings.
380
 *
380
 *
381
 * Do a char-by-char comparison of two NULL-terminated strings.
381
 * Do a char-by-char comparison of two NULL-terminated strings.
382
 * The strings are considered equal iff they consist of the same
382
 * The strings are considered equal iff they consist of the same
383
 * characters on the minimum of their lengths.
383
 * characters on the minimum of their lengths.
384
 *
384
 *
385
 * @param s1 First string to compare.
385
 * @param s1 First string to compare.
386
 * @param s2 Second string to compare.
386
 * @param s2 Second string to compare.
387
 *
387
 *
388
 * @return 0 if the strings are equal, -1 if first is smaller,
388
 * @return 0 if the strings are equal, -1 if first is smaller,
389
 *         1 if second smaller.
389
 *         1 if second smaller.
390
 *
390
 *
391
 */
391
 */
392
int str_cmp(const char *s1, const char *s2)
392
int str_cmp(const char *s1, const char *s2)
393
{
393
{
394
    wchar_t c1 = 0;
394
    wchar_t c1 = 0;
395
    wchar_t c2 = 0;
395
    wchar_t c2 = 0;
396
   
396
   
397
    size_t off1 = 0;
397
    size_t off1 = 0;
398
    size_t off2 = 0;
398
    size_t off2 = 0;
399
 
399
 
400
    while (true) {
400
    while (true) {
401
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
401
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
402
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
402
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
403
 
403
 
404
        if (c1 < c2)
404
        if (c1 < c2)
405
            return -1;
405
            return -1;
406
       
406
       
407
        if (c1 > c2)
407
        if (c1 > c2)
408
            return 1;
408
            return 1;
409
 
409
 
410
        if (c1 == 0 || c2 == 0)
410
        if (c1 == 0 || c2 == 0)
411
            break;     
411
            break;     
412
    }
412
    }
413
 
413
 
414
    return 0;
414
    return 0;
415
}
415
}
416
 
416
 
417
/** Compare two NULL terminated strings with length limit.
417
/** Compare two NULL terminated strings with length limit.
418
 *
418
 *
419
 * Do a char-by-char comparison of two NULL-terminated strings.
419
 * Do a char-by-char comparison of two NULL-terminated strings.
420
 * The strings are considered equal iff they consist of the same
420
 * The strings are considered equal iff they consist of the same
421
 * characters on the minimum of their lengths and the length limit.
421
 * characters on the minimum of their lengths and the length limit.
422
 *
422
 *
423
 * @param s1      First string to compare.
423
 * @param s1      First string to compare.
424
 * @param s2      Second string to compare.
424
 * @param s2      Second string to compare.
425
 * @param max_len Maximum number of characters to consider.
425
 * @param max_len Maximum number of characters to consider.
426
 *
426
 *
427
 * @return 0 if the strings are equal, -1 if first is smaller,
427
 * @return 0 if the strings are equal, -1 if first is smaller,
428
 *         1 if second smaller.
428
 *         1 if second smaller.
429
 *
429
 *
430
 */
430
 */
431
int str_lcmp(const char *s1, const char *s2, count_t max_len)
431
int str_lcmp(const char *s1, const char *s2, count_t max_len)
432
{
432
{
433
    wchar_t c1 = 0;
433
    wchar_t c1 = 0;
434
    wchar_t c2 = 0;
434
    wchar_t c2 = 0;
435
   
435
   
436
    size_t off1 = 0;
436
    size_t off1 = 0;
437
    size_t off2 = 0;
437
    size_t off2 = 0;
438
   
438
   
439
    count_t len = 0;
439
    count_t len = 0;
440
 
440
 
441
    while (true) {
441
    while (true) {
442
        if (len >= max_len)
442
        if (len >= max_len)
443
            break;
443
            break;
444
 
444
 
445
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
445
        c1 = str_decode(s1, &off1, STR_NO_LIMIT);
446
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
446
        c2 = str_decode(s2, &off2, STR_NO_LIMIT);
447
 
447
 
448
        if (c1 < c2)
448
        if (c1 < c2)
449
            return -1;
449
            return -1;
450
 
450
 
451
        if (c1 > c2)
451
        if (c1 > c2)
452
            return 1;
452
            return 1;
453
 
453
 
454
        if (c1 == 0 || c2 == 0)
454
        if (c1 == 0 || c2 == 0)
455
            break;
455
            break;
456
 
456
 
457
        ++len; 
457
        ++len; 
458
    }
458
    }
459
 
459
 
460
    return 0;
460
    return 0;
461
 
461
 
462
}
462
}
463
 
463
 
464
/** Copy NULL-terminated string.
464
/** Copy NULL-terminated string.
465
 *
465
 *
466
 * Copy source string @a src to destination buffer @a dst.
466
 * Copy source string @a src to destination buffer @a dst.
467
 * No more than @a size bytes are written. NULL-terminator is always
467
 * No more than @a size bytes are written. NULL-terminator is always
468
 * written after the last succesfully copied character (i.e. if the
468
 * written after the last succesfully copied character (i.e. if the
469
 * destination buffer is has at least 1 byte, it will be always
469
 * destination buffer is has at least 1 byte, it will be always
470
 * NULL-terminated).
470
 * NULL-terminated).
471
 *
471
 *
472
 * @param src   Source string.
472
 * @param src   Source string.
473
 * @param dst   Destination buffer.
473
 * @param dst   Destination buffer.
474
 * @param count Size of the destination buffer.
474
 * @param count Size of the destination buffer.
475
 *
475
 *
476
 */
476
 */
477
void str_ncpy(char *dst, const char *src, size_t size)
477
void str_ncpy(char *dst, const char *src, size_t size)
478
{
478
{
479
    /* No space for the NULL-terminator in the buffer */
479
    /* No space for the NULL-terminator in the buffer */
480
    if (size == 0)
480
    if (size == 0)
481
        return;
481
        return;
482
   
482
   
483
    wchar_t ch;
483
    wchar_t ch;
484
    size_t str_off = 0;
484
    size_t str_off = 0;
485
    size_t dst_off = 0;
485
    size_t dst_off = 0;
486
   
486
   
487
    while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) {
487
    while ((ch = str_decode(src, &str_off, STR_NO_LIMIT)) != 0) {
488
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
488
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
489
            break;
489
            break;
490
    }
490
    }
491
   
491
   
492
    if (dst_off >= size)
492
    if (dst_off >= size)
493
        dst[size - 1] = 0;
493
        dst[size - 1] = 0;
494
    else
494
    else
495
        dst[dst_off] = 0;
495
        dst[dst_off] = 0;
496
}
496
}
497
 
497
 
498
/** Copy NULL-terminated wide string to string
498
/** Copy NULL-terminated wide string to string
499
 *
499
 *
500
 * Copy source wide string @a src to destination buffer @a dst.
500
 * Copy source wide string @a src to destination buffer @a dst.
501
 * No more than @a size bytes are written. NULL-terminator is always
501
 * No more than @a size bytes are written. NULL-terminator is always
502
 * written after the last succesfully copied character (i.e. if the
502
 * written after the last succesfully copied character (i.e. if the
503
 * destination buffer is has at least 1 byte, it will be always
503
 * destination buffer is has at least 1 byte, it will be always
504
 * NULL-terminated).
504
 * NULL-terminated).
505
 *
505
 *
506
 * @param src   Source wide string.
506
 * @param src   Source wide string.
507
 * @param dst   Destination buffer.
507
 * @param dst   Destination buffer.
508
 * @param count Size of the destination buffer.
508
 * @param count Size of the destination buffer.
509
 *
509
 *
510
 */
510
 */
511
void wstr_nstr(char *dst, const wchar_t *src, size_t size)
511
void wstr_nstr(char *dst, const wchar_t *src, size_t size)
512
{
512
{
513
    /* No space for the NULL-terminator in the buffer */
513
    /* No space for the NULL-terminator in the buffer */
514
    if (size == 0)
514
    if (size == 0)
515
        return;
515
        return;
516
   
516
   
517
    wchar_t ch;
517
    wchar_t ch;
518
    count_t src_idx = 0;
518
    count_t src_idx = 0;
519
    size_t dst_off = 0;
519
    size_t dst_off = 0;
520
   
520
   
521
    while ((ch = src[src_idx++]) != 0) {
521
    while ((ch = src[src_idx++]) != 0) {
522
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
522
        if (chr_encode(ch, dst, &dst_off, size) != EOK)
523
            break;
523
            break;
524
    }
524
    }
525
   
525
   
526
    if (dst_off >= size)
526
    if (dst_off >= size)
527
        dst[size - 1] = 0;
527
        dst[size - 1] = 0;
528
    else
528
    else
529
        dst[dst_off] = 0;
529
        dst[dst_off] = 0;
530
}
530
}
531
 
531
 
532
/** Find first occurence of character in string.
532
/** Find first occurence of character in string.
533
 *
533
 *
534
 * @param str String to search.
534
 * @param str String to search.
535
 * @param ch  Character to look for.
535
 * @param ch  Character to look for.
536
 *
536
 *
537
 * @return Pointer to character in @a str or NULL if not found.
537
 * @return Pointer to character in @a str or NULL if not found.
538
 *
538
 *
539
 */
539
 */
540
const char *str_chr(const char *str, wchar_t ch)
540
const char *str_chr(const char *str, wchar_t ch)
541
{
541
{
542
    wchar_t acc;
542
    wchar_t acc;
543
    size_t off = 0;
543
    size_t off = 0;
544
   
544
   
545
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
545
    while ((acc = str_decode(str, &off, STR_NO_LIMIT)) != 0) {
546
        if (acc == ch)
546
        if (acc == ch)
547
            return (str + off);
547
            return (str + off);
548
    }
548
    }
549
   
549
   
550
    return NULL;
550
    return NULL;
551
}
551
}
552
 
552
 
553
/** Insert a wide character into a wide string.
553
/** Insert a wide character into a wide string.
554
 *
554
 *
555
 * Insert a wide character into a wide string at position
555
 * Insert a wide character into a wide string at position
556
 * @a pos. The characters after the position are shifted.
556
 * @a pos. The characters after the position are shifted.
557
 *
557
 *
558
 * @param str     String to insert to.
558
 * @param str     String to insert to.
559
 * @param ch      Character to insert to.
559
 * @param ch      Character to insert to.
560
 * @param pos     Character index where to insert.
560
 * @param pos     Character index where to insert.
561
 @ @param max_pos Characters in the buffer.
561
 @ @param max_pos Characters in the buffer.
562
 *
562
 *
563
 * @return True if the insertion was sucessful, false if the position
563
 * @return True if the insertion was sucessful, false if the position
564
 *         is out of bounds.
564
 *         is out of bounds.
565
 *
565
 *
566
 */
566
 */
567
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
567
bool wstr_linsert(wchar_t *str, wchar_t ch, count_t pos, count_t max_pos)
568
{
568
{
569
    count_t len = wstr_length(str);
569
    count_t len = wstr_length(str);
570
   
570
   
571
    if ((pos > len) || (pos + 1 > max_pos))
571
    if ((pos > len) || (pos + 1 > max_pos))
572
        return false;
572
        return false;
573
   
573
   
574
    count_t i;
574
    count_t i;
575
    for (i = len; i + 1 > pos; i--)
575
    for (i = len; i + 1 > pos; i--)
576
        str[i + 1] = str[i];
576
        str[i + 1] = str[i];
577
   
577
   
578
    str[pos] = ch;
578
    str[pos] = ch;
579
   
579
   
580
    return true;
580
    return true;
581
}
581
}
582
 
582
 
583
/** Remove a wide character from a wide string.
583
/** Remove a wide character from a wide string.
584
 *
584
 *
585
 * Remove a wide character from a wide string at position
585
 * Remove a wide character from a wide string at position
586
 * @a pos. The characters after the position are shifted.
586
 * @a pos. The characters after the position are shifted.
587
 *
587
 *
588
 * @param str String to remove from.
588
 * @param str String to remove from.
589
 * @param pos Character index to remove.
589
 * @param pos Character index to remove.
590
 *
590
 *
591
 * @return True if the removal was sucessful, false if the position
591
 * @return True if the removal was sucessful, false if the position
592
 *         is out of bounds.
592
 *         is out of bounds.
593
 *
593
 *
594
 */
594
 */
595
bool wstr_remove(wchar_t *str, count_t pos)
595
bool wstr_remove(wchar_t *str, count_t pos)
596
{
596
{
597
    count_t len = wstr_length(str);
597
    count_t len = wstr_length(str);
598
   
598
   
599
    if (pos >= len)
599
    if (pos >= len)
600
        return false;
600
        return false;
601
   
601
   
602
    count_t i;
602
    count_t i;
603
    for (i = pos + 1; i <= len; i++)
603
    for (i = pos + 1; i <= len; i++)
604
        str[i - 1] = str[i];
604
        str[i - 1] = str[i];
605
   
605
   
606
    return true;
606
    return true;
607
}
607
}
608
 
608
 
609
/** Count the number of characters in the string, not including terminating 0.
-
 
610
 *
-
 
611
 * @param str       String.
-
 
612
 * @return      Number of characters in string.
-
 
613
 */
-
 
614
size_t strlen(const char *str)
-
 
615
{
-
 
616
    size_t counter = 0;
-
 
617
 
-
 
618
    while (str[counter] != 0)
-
 
619
        counter++;
-
 
620
 
-
 
621
    return counter;
-
 
622
}
-
 
623
 
-
 
624
int strcmp(const char *a, const char *b)
-
 
625
{
-
 
626
    int c = 0;
-
 
627
   
-
 
628
    while (a[c] && b[c] && (!(a[c] - b[c])))
-
 
629
        c++;
-
 
630
   
-
 
631
    return (a[c] - b[c]);
-
 
632
}
-
 
633
 
-
 
634
int strncmp(const char *a, const char *b, size_t n)
609
int strncmp(const char *a, const char *b, size_t n)
635
{
610
{
636
    size_t c = 0;
611
    size_t c = 0;
637
 
612
 
638
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
613
    while (c < n && a[c] && b[c] && (!(a[c] - b[c])))
639
        c++;
614
        c++;
640
   
615
   
641
    return ( c < n ? a[c] - b[c] : 0);
616
    return ( c < n ? a[c] - b[c] : 0);
642
   
617
   
643
}
618
}
644
 
619
 
645
int stricmp(const char *a, const char *b)
620
int stricmp(const char *a, const char *b)
646
{
621
{
647
    int c = 0;
622
    int c = 0;
648
   
623
   
649
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
624
    while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c]))))
650
        c++;
625
        c++;
651
   
626
   
652
    return (tolower(a[c]) - tolower(b[c]));
627
    return (tolower(a[c]) - tolower(b[c]));
653
}
628
}
654
 
629
 
655
/** Return pointer to the first occurence of character c in string.
630
/** Return pointer to the first occurence of character c in string.
656
 *
631
 *
657
 * @param str       Scanned string.
632
 * @param str       Scanned string.
658
 * @param c     Searched character (taken as one byte).
633
 * @param c     Searched character (taken as one byte).
659
 * @return      Pointer to the matched character or NULL if it is not
634
 * @return      Pointer to the matched character or NULL if it is not
660
 *          found in given string.
635
 *          found in given string.
661
 */
636
 */
662
char *strchr(const char *str, int c)
637
char *strchr(const char *str, int c)
663
{
638
{
664
    while (*str != '\0') {
639
    while (*str != '\0') {
665
        if (*str == (char) c)
640
        if (*str == (char) c)
666
            return (char *) str;
641
            return (char *) str;
667
        str++;
642
        str++;
668
    }
643
    }
669
 
644
 
670
    return NULL;
645
    return NULL;
671
}
646
}
672
 
647
 
673
/** Return pointer to the last occurence of character c in string.
648
/** Return pointer to the last occurence of character c in string.
674
 *
649
 *
675
 * @param str       Scanned string.
650
 * @param str       Scanned string.
676
 * @param c     Searched character (taken as one byte).
651
 * @param c     Searched character (taken as one byte).
677
 * @return      Pointer to the matched character or NULL if it is not
652
 * @return      Pointer to the matched character or NULL if it is not
678
 *          found in given string.
653
 *          found in given string.
679
 */
654
 */
680
char *strrchr(const char *str, int c)
655
char *strrchr(const char *str, int c)
681
{
656
{
682
    char *retval = NULL;
657
    char *retval = NULL;
683
 
658
 
684
    while (*str != '\0') {
659
    while (*str != '\0') {
685
        if (*str == (char) c)
660
        if (*str == (char) c)
686
            retval = (char *) str;
661
            retval = (char *) str;
687
        str++;
662
        str++;
688
    }
663
    }
689
 
664
 
690
    return (char *) retval;
665
    return (char *) retval;
691
}
666
}
692
 
667
 
693
/** Convert string to a number.
668
/** Convert string to a number.
694
 * Core of strtol and strtoul functions.
669
 * Core of strtol and strtoul functions.
695
 *
670
 *
696
 * @param nptr      Pointer to string.
671
 * @param nptr      Pointer to string.
697
 * @param endptr    If not NULL, function stores here pointer to the first
672
 * @param endptr    If not NULL, function stores here pointer to the first
698
 *          invalid character.
673
 *          invalid character.
699
 * @param base      Zero or number between 2 and 36 inclusive.
674
 * @param base      Zero or number between 2 and 36 inclusive.
700
 * @param sgn       It's set to 1 if minus found.
675
 * @param sgn       It's set to 1 if minus found.
701
 * @return      Result of conversion.
676
 * @return      Result of conversion.
702
 */
677
 */
703
static unsigned long
678
static unsigned long
704
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
679
_strtoul(const char *nptr, char **endptr, int base, char *sgn)
705
{
680
{
706
    unsigned char c;
681
    unsigned char c;
707
    unsigned long result = 0;
682
    unsigned long result = 0;
708
    unsigned long a, b;
683
    unsigned long a, b;
709
    const char *str = nptr;
684
    const char *str = nptr;
710
    const char *tmpptr;
685
    const char *tmpptr;
711
   
686
   
712
    while (isspace(*str))
687
    while (isspace(*str))
713
        str++;
688
        str++;
714
   
689
   
715
    if (*str == '-') {
690
    if (*str == '-') {
716
        *sgn = 1;
691
        *sgn = 1;
717
        ++str;
692
        ++str;
718
    } else if (*str == '+')
693
    } else if (*str == '+')
719
        ++str;
694
        ++str;
720
   
695
   
721
    if (base) {
696
    if (base) {
722
        if ((base == 1) || (base > 36)) {
697
        if ((base == 1) || (base > 36)) {
723
            /* FIXME: set errno to EINVAL */
698
            /* FIXME: set errno to EINVAL */
724
            return 0;
699
            return 0;
725
        }
700
        }
726
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
701
        if ((base == 16) && (*str == '0') && ((str[1] == 'x') ||
727
            (str[1] == 'X'))) {
702
            (str[1] == 'X'))) {
728
            str += 2;
703
            str += 2;
729
        }
704
        }
730
    } else {
705
    } else {
731
        base = 10;
706
        base = 10;
732
       
707
       
733
        if (*str == '0') {
708
        if (*str == '0') {
734
            base = 8;
709
            base = 8;
735
            if ((str[1] == 'X') || (str[1] == 'x'))  {
710
            if ((str[1] == 'X') || (str[1] == 'x'))  {
736
                base = 16;
711
                base = 16;
737
                str += 2;
712
                str += 2;
738
            }
713
            }
739
        }
714
        }
740
    }
715
    }
741
   
716
   
742
    tmpptr = str;
717
    tmpptr = str;
743
 
718
 
744
    while (*str) {
719
    while (*str) {
745
        c = *str;
720
        c = *str;
746
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
721
        c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
747
            (c <= '9' ? c - '0' : 0xff)));
722
            (c <= '9' ? c - '0' : 0xff)));
748
        if (c > base) {
723
        if (c > base) {
749
            break;
724
            break;
750
        }
725
        }
751
       
726
       
752
        a = (result & 0xff) * base + c;
727
        a = (result & 0xff) * base + c;
753
        b = (result >> 8) * base + (a >> 8);
728
        b = (result >> 8) * base + (a >> 8);
754
       
729
       
755
        if (b > (ULONG_MAX >> 8)) {
730
        if (b > (ULONG_MAX >> 8)) {
756
            /* overflow */
731
            /* overflow */
757
            /* FIXME: errno = ERANGE*/
732
            /* FIXME: errno = ERANGE*/
758
            return ULONG_MAX;
733
            return ULONG_MAX;
759
        }
734
        }
760
   
735
   
761
        result = (b << 8) + (a & 0xff);
736
        result = (b << 8) + (a & 0xff);
762
        ++str;
737
        ++str;
763
    }
738
    }
764
   
739
   
765
    if (str == tmpptr) {
740
    if (str == tmpptr) {
766
        /*
741
        /*
767
         * No number was found => first invalid character is the first
742
         * No number was found => first invalid character is the first
768
         * character of the string.
743
         * character of the string.
769
         */
744
         */
770
        /* FIXME: set errno to EINVAL */
745
        /* FIXME: set errno to EINVAL */
771
        str = nptr;
746
        str = nptr;
772
        result = 0;
747
        result = 0;
773
    }
748
    }
774
   
749
   
775
    if (endptr)
750
    if (endptr)
776
        *endptr = (char *) str;
751
        *endptr = (char *) str;
777
 
752
 
778
    if (nptr == str) {
753
    if (nptr == str) {
779
        /*FIXME: errno = EINVAL*/
754
        /*FIXME: errno = EINVAL*/
780
        return 0;
755
        return 0;
781
    }
756
    }
782
 
757
 
783
    return result;
758
    return result;
784
}
759
}
785
 
760
 
786
/** Convert initial part of string to long int according to given base.
761
/** Convert initial part of string to long int according to given base.
787
 * The number may begin with an arbitrary number of whitespaces followed by
762
 * The number may begin with an arbitrary number of whitespaces followed by
788
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
763
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
789
 * inserted and the number will be taken as hexadecimal one. If the base is 0
764
 * inserted and the number will be taken as hexadecimal one. If the base is 0
790
 * and the number begin with a zero, number will be taken as octal one (as with
765
 * and the number begin with a zero, number will be taken as octal one (as with
791
 * base 8). Otherwise the base 0 is taken as decimal.
766
 * base 8). Otherwise the base 0 is taken as decimal.
792
 *
767
 *
793
 * @param nptr      Pointer to string.
768
 * @param nptr      Pointer to string.
794
 * @param endptr    If not NULL, function stores here pointer to the first
769
 * @param endptr    If not NULL, function stores here pointer to the first
795
 *          invalid character.
770
 *          invalid character.
796
 * @param base      Zero or number between 2 and 36 inclusive.
771
 * @param base      Zero or number between 2 and 36 inclusive.
797
 * @return      Result of conversion.
772
 * @return      Result of conversion.
798
 */
773
 */
799
long int strtol(const char *nptr, char **endptr, int base)
774
long int strtol(const char *nptr, char **endptr, int base)
800
{
775
{
801
    char sgn = 0;
776
    char sgn = 0;
802
    unsigned long number = 0;
777
    unsigned long number = 0;
803
   
778
   
804
    number = _strtoul(nptr, endptr, base, &sgn);
779
    number = _strtoul(nptr, endptr, base, &sgn);
805
 
780
 
806
    if (number > LONG_MAX) {
781
    if (number > LONG_MAX) {
807
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
782
        if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) {
808
            /* FIXME: set 0 to errno */
783
            /* FIXME: set 0 to errno */
809
            return number;     
784
            return number;     
810
        }
785
        }
811
        /* FIXME: set ERANGE to errno */
786
        /* FIXME: set ERANGE to errno */
812
        return (sgn ? LONG_MIN : LONG_MAX);
787
        return (sgn ? LONG_MIN : LONG_MAX);
813
    }
788
    }
814
   
789
   
815
    return (sgn ? -number : number);
790
    return (sgn ? -number : number);
816
}
791
}
817
 
792
 
818
 
793
 
819
/** Convert initial part of string to unsigned long according to given base.
794
/** Convert initial part of string to unsigned long according to given base.
820
 * The number may begin with an arbitrary number of whitespaces followed by
795
 * The number may begin with an arbitrary number of whitespaces followed by
821
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
796
 * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be
822
 * inserted and the number will be taken as hexadecimal one. If the base is 0
797
 * inserted and the number will be taken as hexadecimal one. If the base is 0
823
 * and the number begin with a zero, number will be taken as octal one (as with
798
 * and the number begin with a zero, number will be taken as octal one (as with
824
 * base 8). Otherwise the base 0 is taken as decimal.
799
 * base 8). Otherwise the base 0 is taken as decimal.
825
 *
800
 *
826
 * @param nptr      Pointer to string.
801
 * @param nptr      Pointer to string.
827
 * @param endptr    If not NULL, function stores here pointer to the first
802
 * @param endptr    If not NULL, function stores here pointer to the first
828
 *          invalid character
803
 *          invalid character
829
 * @param base      Zero or number between 2 and 36 inclusive.
804
 * @param base      Zero or number between 2 and 36 inclusive.
830
 * @return      Result of conversion.
805
 * @return      Result of conversion.
831
 */
806
 */
832
unsigned long strtoul(const char *nptr, char **endptr, int base)
807
unsigned long strtoul(const char *nptr, char **endptr, int base)
833
{
808
{
834
    char sgn = 0;
809
    char sgn = 0;
835
    unsigned long number = 0;
810
    unsigned long number = 0;
836
   
811
   
837
    number = _strtoul(nptr, endptr, base, &sgn);
812
    number = _strtoul(nptr, endptr, base, &sgn);
838
 
813
 
839
    return (sgn ? -number : number);
814
    return (sgn ? -number : number);
840
}
815
}
841
 
816
 
842
char *strcpy(char *dest, const char *src)
817
char *strcpy(char *dest, const char *src)
843
{
818
{
844
    char *orig = dest;
819
    char *orig = dest;
845
   
820
   
846
    while ((*(dest++) = *(src++)))
821
    while ((*(dest++) = *(src++)))
847
        ;
822
        ;
848
    return orig;
823
    return orig;
849
}
824
}
850
 
825
 
851
char *strncpy(char *dest, const char *src, size_t n)
826
char *strncpy(char *dest, const char *src, size_t n)
852
{
827
{
853
    char *orig = dest;
828
    char *orig = dest;
854
   
829
   
855
    while ((*(dest++) = *(src++)) && --n)
830
    while ((*(dest++) = *(src++)) && --n)
856
        ;
831
        ;
857
    return orig;
832
    return orig;
858
}
833
}
859
 
834
 
860
char *strcat(char *dest, const char *src)
835
char *strcat(char *dest, const char *src)
861
{
836
{
862
    char *orig = dest;
837
    char *orig = dest;
863
    while (*dest++)
838
    while (*dest++)
864
        ;
839
        ;
865
    --dest;
840
    --dest;
866
    while ((*dest++ = *src++))
841
    while ((*dest++ = *src++))
867
        ;
842
        ;
868
    return orig;
843
    return orig;
869
}
844
}
870
 
845
 
871
char * strdup(const char *s1)
846
char * strdup(const char *s1)
872
{
847
{
873
    size_t len = strlen(s1) + 1;
848
    size_t len = str_size(s1) + 1;
874
    void *ret = malloc(len);
849
    void *ret = malloc(len);
875
 
850
 
876
    if (ret == NULL)
851
    if (ret == NULL)
877
        return (char *) NULL;
852
        return (char *) NULL;
878
 
853
 
879
    return (char *) memcpy(ret, s1, len);
854
    return (char *) memcpy(ret, s1, len);
880
}
855
}
881
 
856
 
882
char *strtok(char *s, const char *delim)
857
char *strtok(char *s, const char *delim)
883
{
858
{
884
    static char *next;
859
    static char *next;
885
 
860
 
886
    return strtok_r(s, delim, &next);
861
    return strtok_r(s, delim, &next);
887
}
862
}
888
 
863
 
889
char *strtok_r(char *s, const char *delim, char **next)
864
char *strtok_r(char *s, const char *delim, char **next)
890
{
865
{
891
    char *start, *end;
866
    char *start, *end;
892
 
867
 
893
    if (s == NULL)
868
    if (s == NULL)
894
        s = *next;
869
        s = *next;
895
 
870
 
896
    /* Skip over leading delimiters. */
871
    /* Skip over leading delimiters. */
897
    while (*s && (strchr(delim, *s) != NULL)) ++s;
872
    while (*s && (strchr(delim, *s) != NULL)) ++s;
898
    start = s;
873
    start = s;
899
 
874
 
900
    /* Skip over token characters. */
875
    /* Skip over token characters. */
901
    while (*s && (strchr(delim, *s) == NULL)) ++s;
876
    while (*s && (strchr(delim, *s) == NULL)) ++s;
902
    end = s;
877
    end = s;
903
    *next = (*s ? s + 1 : s);
878
    *next = (*s ? s + 1 : s);
904
 
879
 
905
    if (start == end) {
880
    if (start == end) {
906
        return NULL;    /* No more tokens. */
881
        return NULL;    /* No more tokens. */
907
    }
882
    }
908
 
883
 
909
    /* Overwrite delimiter with NULL terminator. */
884
    /* Overwrite delimiter with NULL terminator. */
910
    *end = '\0';
885
    *end = '\0';
911
    return start;
886
    return start;
912
}
887
}
913
 
888
 
914
/** @}
889
/** @}
915
 */
890
 */
916
 
891