Rev 3427 | Rev 3728 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3427 | Rev 3727 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2005 Martin Decky |
2 | * Copyright (c) 2005 Martin Decky |
3 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
3 | * Copyright (C) 1998 by Wes Peters <wes@softweyr.com> |
4 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
4 | * Copyright (c) 1988, 1993 The Regents of the University of California. |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
9 | * are met: |
10 | * |
10 | * |
11 | * - Redistributions of source code must retain the above copyright |
11 | * - Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * - Redistributions in binary form must reproduce the above copyright |
13 | * - Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
16 | * - The name of the author may not be used to endorse or promote products |
16 | * - The name of the author may not be used to endorse or promote products |
17 | * derived from this software without specific prior written permission. |
17 | * derived from this software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /** @addtogroup libc |
31 | /** @addtogroup libc |
32 | * @{ |
32 | * @{ |
33 | */ |
33 | */ |
34 | /** @file |
34 | /** @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <string.h> |
37 | #include <string.h> |
38 | #include <unistd.h> |
38 | #include <unistd.h> |
39 | #include <ctype.h> |
39 | #include <ctype.h> |
40 | #include <limits.h> |
40 | #include <limits.h> |
41 | #include <align.h> |
41 | #include <align.h> |
42 | #include <sys/types.h> |
42 | #include <sys/types.h> |
43 | #include <malloc.h> |
43 | #include <malloc.h> |
44 | 44 | ||
45 | /* Dummy implementation of mem/ functions */ |
45 | /* Dummy implementation of mem/ functions */ |
46 | 46 | ||
47 | void *memset(void *s, int c, size_t n) |
47 | void *memset(void *s, int c, size_t n) |
48 | { |
48 | { |
49 | char *os = s; |
49 | char *os = s; |
50 | 50 | ||
51 | while (n--) |
51 | while (n--) |
52 | *(os++) = c; |
52 | *(os++) = c; |
53 | 53 | ||
54 | return s; |
54 | return s; |
55 | } |
55 | } |
56 | 56 | ||
57 | struct along { |
57 | struct along { |
58 | unsigned long n; |
58 | unsigned long n; |
59 | } __attribute__ ((packed)); |
59 | } __attribute__ ((packed)); |
60 | 60 | ||
61 | static void *unaligned_memcpy(void *dst, const void *src, size_t n) |
61 | static void *unaligned_memcpy(void *dst, const void *src, size_t n) |
62 | { |
62 | { |
63 | int i, j; |
63 | int i, j; |
64 | struct along *adst = dst; |
64 | struct along *adst = dst; |
65 | const struct along *asrc = src; |
65 | const struct along *asrc = src; |
66 | 66 | ||
67 | for (i = 0; i < n / sizeof(unsigned long); i++) |
67 | for (i = 0; i < n / sizeof(unsigned long); i++) |
68 | adst[i].n = asrc[i].n; |
68 | adst[i].n = asrc[i].n; |
69 | 69 | ||
70 | for (j = 0; j < n % sizeof(unsigned long); j++) |
70 | for (j = 0; j < n % sizeof(unsigned long); j++) |
71 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = |
71 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = |
72 | ((unsigned char *) (((unsigned long *) src) + i))[j]; |
72 | ((unsigned char *) (((unsigned long *) src) + i))[j]; |
73 | 73 | ||
74 | return (char *) dst; |
74 | return (char *) dst; |
75 | } |
75 | } |
76 | 76 | ||
- | 77 | /** Copy memory block. */ |
|
77 | void *memcpy(void *dst, const void *src, size_t n) |
78 | void *memcpy(void *dst, const void *src, size_t n) |
78 | { |
79 | { |
79 | int i, j; |
80 | size_t i; |
- | 81 | size_t mod, fill; |
|
- | 82 | size_t word_size; |
|
- | 83 | size_t n_words; |
|
80 | 84 | ||
- | 85 | const unsigned long *srcw; |
|
- | 86 | unsigned long *dstw; |
|
- | 87 | const uint8_t *srcb; |
|
- | 88 | uint8_t *dstb; |
|
- | 89 | ||
- | 90 | word_size = sizeof(unsigned long); |
|
- | 91 | ||
- | 92 | /* |
|
- | 93 | * Are source and destination addresses congruent modulo word_size? |
|
- | 94 | * If not, use unaligned_memcpy(). |
|
- | 95 | */ |
|
- | 96 | ||
81 | if (((long) dst & (sizeof(long) - 1)) || |
97 | if (((uintptr_t) dst & (word_size - 1)) != |
82 | ((long) src & (sizeof(long) - 1))) |
98 | ((uintptr_t) src & (word_size - 1))) |
83 | return unaligned_memcpy(dst, src, n); |
99 | return unaligned_memcpy(dst, src, n); |
84 | 100 | ||
- | 101 | /* |
|
85 | for (i = 0; i < n / sizeof(unsigned long); i++) |
102 | * mod is the address modulo word size. fill is the length of the |
- | 103 | * initial buffer segment before the first word boundary. |
|
86 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
104 | * If the buffer is very short, use unaligned_memcpy(), too. |
87 | 105 | */ |
|
- | 106 | ||
88 | for (j = 0; j < n % sizeof(unsigned long); j++) |
107 | mod = (uintptr_t) dst & (word_size - 1); |
- | 108 | fill = word_size - mod; |
|
- | 109 | if (fill > n) fill = n; |
|
- | 110 | ||
- | 111 | /* Copy the initial segment. */ |
|
- | 112 | ||
- | 113 | srcb = src; |
|
- | 114 | dstb = dst; |
|
- | 115 | ||
- | 116 | i = fill; |
|
- | 117 | while (i-- > 0) |
|
- | 118 | *dstb++ = *srcb++; |
|
- | 119 | ||
- | 120 | /* Compute remaining length. */ |
|
- | 121 | ||
- | 122 | n -= fill; |
|
- | 123 | if (n == 0) return dst; |
|
- | 124 | ||
- | 125 | /* Pointers to aligned segment. */ |
|
- | 126 | ||
89 | ((unsigned char *) (((unsigned long *) dst) + i))[j] = |
127 | dstw = (unsigned long *) dstb; |
90 | ((unsigned char *) (((unsigned long *) src) + i))[j]; |
128 | srcw = (const unsigned long *) srcb; |
- | 129 | ||
- | 130 | n_words = n / word_size; /* Number of whole words to copy. */ |
|
- | 131 | n -= n_words * word_size; /* Remaining bytes at the end. */ |
|
- | 132 | ||
- | 133 | /* "Fast" copy. */ |
|
- | 134 | i = n_words; |
|
- | 135 | while (i-- > 0) |
|
- | 136 | *dstw++ = *srcw++; |
|
- | 137 | ||
91 | 138 | /* |
|
- | 139 | * Copy the rest. |
|
- | 140 | */ |
|
- | 141 | ||
- | 142 | srcb = (const uint8_t *) srcw; |
|
- | 143 | dstb = (uint8_t *) dstw; |
|
- | 144 | ||
- | 145 | i = n; |
|
- | 146 | while (i-- > 0) |
|
- | 147 | *dstb++ = *srcb++; |
|
- | 148 | ||
92 | return (char *) dst; |
149 | return dst; |
93 | } |
150 | } |
94 | 151 | ||
95 | void *memmove(void *dst, const void *src, size_t n) |
152 | void *memmove(void *dst, const void *src, size_t n) |
96 | { |
153 | { |
97 | int i, j; |
154 | int i, j; |
98 | 155 | ||
99 | if (src > dst) |
156 | if (src > dst) |
100 | return memcpy(dst, src, n); |
157 | return memcpy(dst, src, n); |
101 | 158 | ||
102 | for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--) |
159 | for (j = (n % sizeof(unsigned long)) - 1; j >= 0; j--) |
103 | ((unsigned char *) ((unsigned long *) dst))[j] = |
160 | ((unsigned char *) ((unsigned long *) dst))[j] = |
104 | ((unsigned char *) ((unsigned long *) src))[j]; |
161 | ((unsigned char *) ((unsigned long *) src))[j]; |
105 | 162 | ||
106 | for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--) |
163 | for (i = n / sizeof(unsigned long) - 1; i >=0 ; i--) |
107 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
164 | ((unsigned long *) dst)[i] = ((unsigned long *) src)[i]; |
108 | 165 | ||
109 | return (char *) dst; |
166 | return (char *) dst; |
110 | } |
167 | } |
111 | 168 | ||
112 | /** Compare two memory areas. |
169 | /** Compare two memory areas. |
113 | * |
170 | * |
114 | * @param s1 Pointer to the first area to compare. |
171 | * @param s1 Pointer to the first area to compare. |
115 | * @param s2 Pointer to the second area to compare. |
172 | * @param s2 Pointer to the second area to compare. |
116 | * @param len Size of the first area in bytes. Both areas must have |
173 | * @param len Size of the first area in bytes. Both areas must have |
117 | * the same length. |
174 | * the same length. |
118 | * @return If len is 0, return zero. If the areas match, return |
175 | * @return If len is 0, return zero. If the areas match, return |
119 | * zero. Otherwise return non-zero. |
176 | * zero. Otherwise return non-zero. |
120 | */ |
177 | */ |
121 | int bcmp(const char *s1, const char *s2, size_t len) |
178 | int bcmp(const char *s1, const char *s2, size_t len) |
122 | { |
179 | { |
123 | for (; len && *s1++ == *s2++; len--) |
180 | for (; len && *s1++ == *s2++; len--) |
124 | ; |
181 | ; |
125 | return len; |
182 | return len; |
126 | } |
183 | } |
127 | 184 | ||
128 | /** Count the number of characters in the string, not including terminating 0. |
185 | /** Count the number of characters in the string, not including terminating 0. |
129 | * |
186 | * |
130 | * @param str String. |
187 | * @param str String. |
131 | * @return Number of characters in string. |
188 | * @return Number of characters in string. |
132 | */ |
189 | */ |
133 | size_t strlen(const char *str) |
190 | size_t strlen(const char *str) |
134 | { |
191 | { |
135 | size_t counter = 0; |
192 | size_t counter = 0; |
136 | 193 | ||
137 | while (str[counter] != 0) |
194 | while (str[counter] != 0) |
138 | counter++; |
195 | counter++; |
139 | 196 | ||
140 | return counter; |
197 | return counter; |
141 | } |
198 | } |
142 | 199 | ||
143 | int strcmp(const char *a, const char *b) |
200 | int strcmp(const char *a, const char *b) |
144 | { |
201 | { |
145 | int c = 0; |
202 | int c = 0; |
146 | 203 | ||
147 | while (a[c] && b[c] && (!(a[c] - b[c]))) |
204 | while (a[c] && b[c] && (!(a[c] - b[c]))) |
148 | c++; |
205 | c++; |
149 | 206 | ||
150 | return (a[c] - b[c]); |
207 | return (a[c] - b[c]); |
151 | } |
208 | } |
152 | 209 | ||
153 | int strncmp(const char *a, const char *b, size_t n) |
210 | int strncmp(const char *a, const char *b, size_t n) |
154 | { |
211 | { |
155 | size_t c = 0; |
212 | size_t c = 0; |
156 | 213 | ||
157 | while (c < n && a[c] && b[c] && (!(a[c] - b[c]))) |
214 | while (c < n && a[c] && b[c] && (!(a[c] - b[c]))) |
158 | c++; |
215 | c++; |
159 | 216 | ||
160 | return ( c < n ? a[c] - b[c] : 0); |
217 | return ( c < n ? a[c] - b[c] : 0); |
161 | 218 | ||
162 | } |
219 | } |
163 | 220 | ||
164 | int stricmp(const char *a, const char *b) |
221 | int stricmp(const char *a, const char *b) |
165 | { |
222 | { |
166 | int c = 0; |
223 | int c = 0; |
167 | 224 | ||
168 | while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c])))) |
225 | while (a[c] && b[c] && (!(tolower(a[c]) - tolower(b[c])))) |
169 | c++; |
226 | c++; |
170 | 227 | ||
171 | return (tolower(a[c]) - tolower(b[c])); |
228 | return (tolower(a[c]) - tolower(b[c])); |
172 | } |
229 | } |
173 | 230 | ||
174 | /** Return pointer to the first occurence of character c in string. |
231 | /** Return pointer to the first occurence of character c in string. |
175 | * |
232 | * |
176 | * @param str Scanned string. |
233 | * @param str Scanned string. |
177 | * @param c Searched character (taken as one byte). |
234 | * @param c Searched character (taken as one byte). |
178 | * @return Pointer to the matched character or NULL if it is not |
235 | * @return Pointer to the matched character or NULL if it is not |
179 | * found in given string. |
236 | * found in given string. |
180 | */ |
237 | */ |
181 | char *strchr(const char *str, int c) |
238 | char *strchr(const char *str, int c) |
182 | { |
239 | { |
183 | while (*str != '\0') { |
240 | while (*str != '\0') { |
184 | if (*str == (char) c) |
241 | if (*str == (char) c) |
185 | return (char *) str; |
242 | return (char *) str; |
186 | str++; |
243 | str++; |
187 | } |
244 | } |
188 | 245 | ||
189 | return NULL; |
246 | return NULL; |
190 | } |
247 | } |
191 | 248 | ||
192 | /** Return pointer to the last occurence of character c in string. |
249 | /** Return pointer to the last occurence of character c in string. |
193 | * |
250 | * |
194 | * @param str Scanned string. |
251 | * @param str Scanned string. |
195 | * @param c Searched character (taken as one byte). |
252 | * @param c Searched character (taken as one byte). |
196 | * @return Pointer to the matched character or NULL if it is not |
253 | * @return Pointer to the matched character or NULL if it is not |
197 | * found in given string. |
254 | * found in given string. |
198 | */ |
255 | */ |
199 | char *strrchr(const char *str, int c) |
256 | char *strrchr(const char *str, int c) |
200 | { |
257 | { |
201 | char *retval = NULL; |
258 | char *retval = NULL; |
202 | 259 | ||
203 | while (*str != '\0') { |
260 | while (*str != '\0') { |
204 | if (*str == (char) c) |
261 | if (*str == (char) c) |
205 | retval = (char *) str; |
262 | retval = (char *) str; |
206 | str++; |
263 | str++; |
207 | } |
264 | } |
208 | 265 | ||
209 | return (char *) retval; |
266 | return (char *) retval; |
210 | } |
267 | } |
211 | 268 | ||
212 | /** Convert string to a number. |
269 | /** Convert string to a number. |
213 | * Core of strtol and strtoul functions. |
270 | * Core of strtol and strtoul functions. |
214 | * |
271 | * |
215 | * @param nptr Pointer to string. |
272 | * @param nptr Pointer to string. |
216 | * @param endptr If not NULL, function stores here pointer to the first |
273 | * @param endptr If not NULL, function stores here pointer to the first |
217 | * invalid character. |
274 | * invalid character. |
218 | * @param base Zero or number between 2 and 36 inclusive. |
275 | * @param base Zero or number between 2 and 36 inclusive. |
219 | * @param sgn It's set to 1 if minus found. |
276 | * @param sgn It's set to 1 if minus found. |
220 | * @return Result of conversion. |
277 | * @return Result of conversion. |
221 | */ |
278 | */ |
222 | static unsigned long |
279 | static unsigned long |
223 | _strtoul(const char *nptr, char **endptr, int base, char *sgn) |
280 | _strtoul(const char *nptr, char **endptr, int base, char *sgn) |
224 | { |
281 | { |
225 | unsigned char c; |
282 | unsigned char c; |
226 | unsigned long result = 0; |
283 | unsigned long result = 0; |
227 | unsigned long a, b; |
284 | unsigned long a, b; |
228 | const char *str = nptr; |
285 | const char *str = nptr; |
229 | const char *tmpptr; |
286 | const char *tmpptr; |
230 | 287 | ||
231 | while (isspace(*str)) |
288 | while (isspace(*str)) |
232 | str++; |
289 | str++; |
233 | 290 | ||
234 | if (*str == '-') { |
291 | if (*str == '-') { |
235 | *sgn = 1; |
292 | *sgn = 1; |
236 | ++str; |
293 | ++str; |
237 | } else if (*str == '+') |
294 | } else if (*str == '+') |
238 | ++str; |
295 | ++str; |
239 | 296 | ||
240 | if (base) { |
297 | if (base) { |
241 | if ((base == 1) || (base > 36)) { |
298 | if ((base == 1) || (base > 36)) { |
242 | /* FIXME: set errno to EINVAL */ |
299 | /* FIXME: set errno to EINVAL */ |
243 | return 0; |
300 | return 0; |
244 | } |
301 | } |
245 | if ((base == 16) && (*str == '0') && ((str[1] == 'x') || |
302 | if ((base == 16) && (*str == '0') && ((str[1] == 'x') || |
246 | (str[1] == 'X'))) { |
303 | (str[1] == 'X'))) { |
247 | str += 2; |
304 | str += 2; |
248 | } |
305 | } |
249 | } else { |
306 | } else { |
250 | base = 10; |
307 | base = 10; |
251 | 308 | ||
252 | if (*str == '0') { |
309 | if (*str == '0') { |
253 | base = 8; |
310 | base = 8; |
254 | if ((str[1] == 'X') || (str[1] == 'x')) { |
311 | if ((str[1] == 'X') || (str[1] == 'x')) { |
255 | base = 16; |
312 | base = 16; |
256 | str += 2; |
313 | str += 2; |
257 | } |
314 | } |
258 | } |
315 | } |
259 | } |
316 | } |
260 | 317 | ||
261 | tmpptr = str; |
318 | tmpptr = str; |
262 | 319 | ||
263 | while (*str) { |
320 | while (*str) { |
264 | c = *str; |
321 | c = *str; |
265 | c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : |
322 | c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 : |
266 | (c <= '9' ? c - '0' : 0xff))); |
323 | (c <= '9' ? c - '0' : 0xff))); |
267 | if (c > base) { |
324 | if (c > base) { |
268 | break; |
325 | break; |
269 | } |
326 | } |
270 | 327 | ||
271 | a = (result & 0xff) * base + c; |
328 | a = (result & 0xff) * base + c; |
272 | b = (result >> 8) * base + (a >> 8); |
329 | b = (result >> 8) * base + (a >> 8); |
273 | 330 | ||
274 | if (b > (ULONG_MAX >> 8)) { |
331 | if (b > (ULONG_MAX >> 8)) { |
275 | /* overflow */ |
332 | /* overflow */ |
276 | /* FIXME: errno = ERANGE*/ |
333 | /* FIXME: errno = ERANGE*/ |
277 | return ULONG_MAX; |
334 | return ULONG_MAX; |
278 | } |
335 | } |
279 | 336 | ||
280 | result = (b << 8) + (a & 0xff); |
337 | result = (b << 8) + (a & 0xff); |
281 | ++str; |
338 | ++str; |
282 | } |
339 | } |
283 | 340 | ||
284 | if (str == tmpptr) { |
341 | if (str == tmpptr) { |
285 | /* |
342 | /* |
286 | * No number was found => first invalid character is the first |
343 | * No number was found => first invalid character is the first |
287 | * character of the string. |
344 | * character of the string. |
288 | */ |
345 | */ |
289 | /* FIXME: set errno to EINVAL */ |
346 | /* FIXME: set errno to EINVAL */ |
290 | str = nptr; |
347 | str = nptr; |
291 | result = 0; |
348 | result = 0; |
292 | } |
349 | } |
293 | 350 | ||
294 | if (endptr) |
351 | if (endptr) |
295 | *endptr = (char *) str; |
352 | *endptr = (char *) str; |
296 | 353 | ||
297 | if (nptr == str) { |
354 | if (nptr == str) { |
298 | /*FIXME: errno = EINVAL*/ |
355 | /*FIXME: errno = EINVAL*/ |
299 | return 0; |
356 | return 0; |
300 | } |
357 | } |
301 | 358 | ||
302 | return result; |
359 | return result; |
303 | } |
360 | } |
304 | 361 | ||
305 | /** Convert initial part of string to long int according to given base. |
362 | /** Convert initial part of string to long int according to given base. |
306 | * The number may begin with an arbitrary number of whitespaces followed by |
363 | * The number may begin with an arbitrary number of whitespaces followed by |
307 | * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be |
364 | * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be |
308 | * inserted and the number will be taken as hexadecimal one. If the base is 0 |
365 | * inserted and the number will be taken as hexadecimal one. If the base is 0 |
309 | * and the number begin with a zero, number will be taken as octal one (as with |
366 | * and the number begin with a zero, number will be taken as octal one (as with |
310 | * base 8). Otherwise the base 0 is taken as decimal. |
367 | * base 8). Otherwise the base 0 is taken as decimal. |
311 | * |
368 | * |
312 | * @param nptr Pointer to string. |
369 | * @param nptr Pointer to string. |
313 | * @param endptr If not NULL, function stores here pointer to the first |
370 | * @param endptr If not NULL, function stores here pointer to the first |
314 | * invalid character. |
371 | * invalid character. |
315 | * @param base Zero or number between 2 and 36 inclusive. |
372 | * @param base Zero or number between 2 and 36 inclusive. |
316 | * @return Result of conversion. |
373 | * @return Result of conversion. |
317 | */ |
374 | */ |
318 | long int strtol(const char *nptr, char **endptr, int base) |
375 | long int strtol(const char *nptr, char **endptr, int base) |
319 | { |
376 | { |
320 | char sgn = 0; |
377 | char sgn = 0; |
321 | unsigned long number = 0; |
378 | unsigned long number = 0; |
322 | 379 | ||
323 | number = _strtoul(nptr, endptr, base, &sgn); |
380 | number = _strtoul(nptr, endptr, base, &sgn); |
324 | 381 | ||
325 | if (number > LONG_MAX) { |
382 | if (number > LONG_MAX) { |
326 | if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) { |
383 | if ((sgn) && (number == (unsigned long) (LONG_MAX) + 1)) { |
327 | /* FIXME: set 0 to errno */ |
384 | /* FIXME: set 0 to errno */ |
328 | return number; |
385 | return number; |
329 | } |
386 | } |
330 | /* FIXME: set ERANGE to errno */ |
387 | /* FIXME: set ERANGE to errno */ |
331 | return (sgn ? LONG_MIN : LONG_MAX); |
388 | return (sgn ? LONG_MIN : LONG_MAX); |
332 | } |
389 | } |
333 | 390 | ||
334 | return (sgn ? -number : number); |
391 | return (sgn ? -number : number); |
335 | } |
392 | } |
336 | 393 | ||
337 | 394 | ||
338 | /** Convert initial part of string to unsigned long according to given base. |
395 | /** Convert initial part of string to unsigned long according to given base. |
339 | * The number may begin with an arbitrary number of whitespaces followed by |
396 | * The number may begin with an arbitrary number of whitespaces followed by |
340 | * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be |
397 | * optional sign (`+' or `-'). If the base is 0 or 16, the prefix `0x' may be |
341 | * inserted and the number will be taken as hexadecimal one. If the base is 0 |
398 | * inserted and the number will be taken as hexadecimal one. If the base is 0 |
342 | * and the number begin with a zero, number will be taken as octal one (as with |
399 | * and the number begin with a zero, number will be taken as octal one (as with |
343 | * base 8). Otherwise the base 0 is taken as decimal. |
400 | * base 8). Otherwise the base 0 is taken as decimal. |
344 | * |
401 | * |
345 | * @param nptr Pointer to string. |
402 | * @param nptr Pointer to string. |
346 | * @param endptr If not NULL, function stores here pointer to the first |
403 | * @param endptr If not NULL, function stores here pointer to the first |
347 | * invalid character |
404 | * invalid character |
348 | * @param base Zero or number between 2 and 36 inclusive. |
405 | * @param base Zero or number between 2 and 36 inclusive. |
349 | * @return Result of conversion. |
406 | * @return Result of conversion. |
350 | */ |
407 | */ |
351 | unsigned long strtoul(const char *nptr, char **endptr, int base) |
408 | unsigned long strtoul(const char *nptr, char **endptr, int base) |
352 | { |
409 | { |
353 | char sgn = 0; |
410 | char sgn = 0; |
354 | unsigned long number = 0; |
411 | unsigned long number = 0; |
355 | 412 | ||
356 | number = _strtoul(nptr, endptr, base, &sgn); |
413 | number = _strtoul(nptr, endptr, base, &sgn); |
357 | 414 | ||
358 | return (sgn ? -number : number); |
415 | return (sgn ? -number : number); |
359 | } |
416 | } |
360 | 417 | ||
361 | char *strcpy(char *dest, const char *src) |
418 | char *strcpy(char *dest, const char *src) |
362 | { |
419 | { |
363 | char *orig = dest; |
420 | char *orig = dest; |
364 | 421 | ||
365 | while ((*(dest++) = *(src++))) |
422 | while ((*(dest++) = *(src++))) |
366 | ; |
423 | ; |
367 | return orig; |
424 | return orig; |
368 | } |
425 | } |
369 | 426 | ||
370 | char *strncpy(char *dest, const char *src, size_t n) |
427 | char *strncpy(char *dest, const char *src, size_t n) |
371 | { |
428 | { |
372 | char *orig = dest; |
429 | char *orig = dest; |
373 | 430 | ||
374 | while ((*(dest++) = *(src++)) && --n) |
431 | while ((*(dest++) = *(src++)) && --n) |
375 | ; |
432 | ; |
376 | return orig; |
433 | return orig; |
377 | } |
434 | } |
378 | 435 | ||
379 | char *strcat(char *dest, const char *src) |
436 | char *strcat(char *dest, const char *src) |
380 | { |
437 | { |
381 | char *orig = dest; |
438 | char *orig = dest; |
382 | while (*dest++) |
439 | while (*dest++) |
383 | ; |
440 | ; |
384 | --dest; |
441 | --dest; |
385 | while ((*dest++ = *src++)) |
442 | while ((*dest++ = *src++)) |
386 | ; |
443 | ; |
387 | return orig; |
444 | return orig; |
388 | } |
445 | } |
389 | 446 | ||
390 | char * strdup(const char *s1) |
447 | char * strdup(const char *s1) |
391 | { |
448 | { |
392 | size_t len = strlen(s1) + 1; |
449 | size_t len = strlen(s1) + 1; |
393 | void *ret = malloc(len); |
450 | void *ret = malloc(len); |
394 | 451 | ||
395 | if (ret == NULL) |
452 | if (ret == NULL) |
396 | return (char *) NULL; |
453 | return (char *) NULL; |
397 | 454 | ||
398 | return (char *) memcpy(ret, s1, len); |
455 | return (char *) memcpy(ret, s1, len); |
399 | } |
456 | } |
400 | 457 | ||
401 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
458 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
402 | char * strtok_r(char *s, const char *delim, char **last) |
459 | char * strtok_r(char *s, const char *delim, char **last) |
403 | { |
460 | { |
404 | char *spanp, *tok; |
461 | char *spanp, *tok; |
405 | int c, sc; |
462 | int c, sc; |
406 | 463 | ||
407 | if (s == NULL && (s = *last) == NULL) |
464 | if (s == NULL && (s = *last) == NULL) |
408 | return (NULL); |
465 | return (NULL); |
409 | 466 | ||
410 | cont: |
467 | cont: |
411 | c = *s++; |
468 | c = *s++; |
412 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
469 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
413 | if (c == sc) |
470 | if (c == sc) |
414 | goto cont; |
471 | goto cont; |
415 | } |
472 | } |
416 | 473 | ||
417 | if (c == 0) { /* no non-delimiter characters */ |
474 | if (c == 0) { /* no non-delimiter characters */ |
418 | *last = NULL; |
475 | *last = NULL; |
419 | return (NULL); |
476 | return (NULL); |
420 | } |
477 | } |
421 | 478 | ||
422 | tok = s - 1; |
479 | tok = s - 1; |
423 | 480 | ||
424 | for (;;) { |
481 | for (;;) { |
425 | c = *s++; |
482 | c = *s++; |
426 | spanp = (char *)delim; |
483 | spanp = (char *)delim; |
427 | do { |
484 | do { |
428 | if ((sc = *spanp++) == c) { |
485 | if ((sc = *spanp++) == c) { |
429 | if (c == 0) |
486 | if (c == 0) |
430 | s = NULL; |
487 | s = NULL; |
431 | else |
488 | else |
432 | s[-1] = '\0'; |
489 | s[-1] = '\0'; |
433 | *last = s; |
490 | *last = s; |
434 | return (tok); |
491 | return (tok); |
435 | } |
492 | } |
436 | } while (sc != 0); |
493 | } while (sc != 0); |
437 | } |
494 | } |
438 | } |
495 | } |
439 | 496 | ||
440 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
497 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
441 | char * strtok(char *s, const char *delim) |
498 | char * strtok(char *s, const char *delim) |
442 | { |
499 | { |
443 | static char *last; |
500 | static char *last; |
444 | 501 | ||
445 | return (strtok_r(s, delim, &last)); |
502 | return (strtok_r(s, delim, &last)); |
446 | } |
503 | } |
447 | 504 | ||
448 | /** @} |
505 | /** @} |
449 | */ |
506 | */ |
450 | 507 |