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