diff options
| author | Roy Marples <roy@marples.name> | 2007-04-05 17:31:05 +0000 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2007-04-05 17:31:05 +0000 |
| commit | 93573fa302c2d0ae141c8c87c5fd89fc3069b5a8 (patch) | |
| tree | 61cd5cf09231578095e925c5e41bf21a7d709fde /common.c | |
| parent | 235ce0896deb6ce04326f8a1514f0b600b7c8dc1 (diff) | |
| download | dhcpcd-93573fa302c2d0ae141c8c87c5fd89fc3069b5a8.tar.xz | |
Use strlcpy if available, if not define our own.
Diffstat (limited to 'common.c')
| -rw-r--r-- | common.c | 29 |
1 files changed, 23 insertions, 6 deletions
@@ -27,15 +27,32 @@ #include "common.h" #include "logger.h" -/* A way of safely handling strncpy */ -char *safe_strncpy (char *dst, const char *src, size_t size) +/* strlcpy is nice, shame glibc does not define it */ +#ifdef __GLIBC__ +size_t strlcpy (char *dst, const char *src, size_t size) { - if (! size) - return dst; + const char *s = src; + size_t n = size; - dst[--size] = '\0'; - return strncpy (dst, src, size); + if (n && --n) + do + { + if (! (*dst++ = *src++)) + break; + } + while (--n); + + if (! n) + { + if (size) + *dst = '\0'; + while (*src++) + ; + } + + return (src - s - 1); } +#endif /* This requires us to link to rt on glibc, so we use sysinfo instead */ #ifdef __linux__ |
