Rev 3448 | Rev 4348 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3448 | Rev 4337 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 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: |
| Line 33... | Line 32... | ||
| 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 | */ |
| Line 396... | Line 310... | ||
| 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 | 318 | ||
| 407 | if (s == NULL && (s = *last) == NULL) |
319 | return strtok_r(s, delim, &next); |
| 408 | return (NULL); |
320 | } |
| 409 | 321 | ||
| 410 | cont: |
- | |
| 411 | c = *s++; |
- | |
| 412 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { |
322 | char *strtok_r(char *s, const char *delim, char **next) |
| 413 | if (c == sc) |
- | |
| 414 | goto cont; |
- | |
| 415 | } |
323 | { |
| - | 324 | char *start, *end; |
|
| 416 | 325 | ||
| 417 | if (c == 0) { /* no non-delimiter characters */ |
- | |
| 418 | *last = NULL; |
326 | if (s == NULL) |
| 419 | return (NULL); |
327 | s = *next; |
| 420 | } |
- | |
| 421 | 328 | ||
| - | 329 | /* Skip over leading delimiters. */ |
|
| - | 330 | while (*s && (strchr(delim, *s) != NULL)) ++s; |
|
| 422 | tok = s - 1; |
331 | start = s; |
| 423 | 332 | ||
| 424 | for (;;) { |
- | |
| 425 | c = *s++; |
- | |
| 426 | spanp = (char *)delim; |
333 | /* Skip over token characters. */ |
| 427 | do { |
- | |
| 428 | if ((sc = *spanp++) == c) { |
334 | while (*s && (strchr(delim, *s) == NULL)) ++s; |
| 429 | if (c == 0) |
- | |
| 430 | s = NULL; |
- | |
| 431 | else |
- | |
| 432 | s[-1] = '\0'; |
- | |
| 433 | *last = s; |
335 | end = s; |
| 434 | return (tok); |
- | |
| 435 | } |
- | |
| 436 | } while (sc != 0); |
336 | *next = (*s ? s + 1 : s); |
| 437 | } |
- | |
| 438 | } |
- | |
| 439 | 337 | ||
| 440 | /* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */ |
338 | if (start == end) { |
| 441 | char * strtok(char *s, const char *delim) |
339 | return NULL; /* No more tokens. */ |
| 442 | { |
340 | } |
| 443 | static char *last; |
- | |
| 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 | */ |