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