| 1 | #include "lib/str.h" | 
|---|
| 2 | #include "config.h" | 
|---|
| 3 |  | 
|---|
| 4 | #if !defined(HAVE_STRLCPY) | 
|---|
| 5 | size_t strlcpy(char *dst, const char *src, size_t siz) { | 
|---|
| 6 | char *d = dst; | 
|---|
| 7 | const char *s = src; | 
|---|
| 8 | size_t n = siz; | 
|---|
| 9 |  | 
|---|
| 10 | // Copy as many bytes as will fit | 
|---|
| 11 | if (n != 0) { | 
|---|
| 12 | while (--n != 0) { | 
|---|
| 13 | if ((*d++ = *s++) == '\0') { | 
|---|
| 14 | break; | 
|---|
| 15 | } | 
|---|
| 16 | } | 
|---|
| 17 | } | 
|---|
| 18 |  | 
|---|
| 19 | // Not enough room in dst, add NUL and traverse rest of src | 
|---|
| 20 | if (n == 0) { | 
|---|
| 21 | if (siz != 0) { | 
|---|
| 22 | // NUL-terminate dst | 
|---|
| 23 | *d = '\0'; | 
|---|
| 24 | } | 
|---|
| 25 | while (*s++) | 
|---|
| 26 | ; | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | // count does not include NUL | 
|---|
| 30 | return (s - src - 1); | 
|---|
| 31 | } | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 34 | #if !defined(HAVE_STRLCAT) | 
|---|
| 35 | size_t strlcat(char *dst, const char *src, size_t siz) { | 
|---|
| 36 | char *d = dst; | 
|---|
| 37 | const char *s = src; | 
|---|
| 38 | size_t n = siz; | 
|---|
| 39 | size_t dlen; | 
|---|
| 40 |  | 
|---|
| 41 | // Find the end of dst and adjust bytes left but don't go past end | 
|---|
| 42 | while (n-- != 0 && *d != '\0') { | 
|---|
| 43 | d++; | 
|---|
| 44 | } | 
|---|
| 45 | dlen = d - dst; | 
|---|
| 46 | n = siz - dlen; | 
|---|
| 47 |  | 
|---|
| 48 | if (n == 0) { | 
|---|
| 49 | return(dlen + strlen(s)); | 
|---|
| 50 | } | 
|---|
| 51 | while (*s != '\0') { | 
|---|
| 52 | if (n != 1) { | 
|---|
| 53 | *d++ = *s; | 
|---|
| 54 | n--; | 
|---|
| 55 | } | 
|---|
| 56 | s++; | 
|---|
| 57 | } | 
|---|
| 58 | *d = '\0'; | 
|---|
| 59 |  | 
|---|
| 60 | // count does not include NUL | 
|---|
| 61 | return (dlen + (s - src)); | 
|---|
| 62 | } | 
|---|
| 63 | #endif | 
|---|
| 64 |  | 
|---|