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