summaryrefslogtreecommitdiffstats
path: root/common.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2007-04-05 17:31:05 +0000
committerRoy Marples <roy@marples.name>2007-04-05 17:31:05 +0000
commit93573fa302c2d0ae141c8c87c5fd89fc3069b5a8 (patch)
tree61cd5cf09231578095e925c5e41bf21a7d709fde /common.c
parent235ce0896deb6ce04326f8a1514f0b600b7c8dc1 (diff)
downloaddhcpcd-93573fa302c2d0ae141c8c87c5fd89fc3069b5a8.tar.xz
Use strlcpy if available, if not define our own.
Diffstat (limited to 'common.c')
-rw-r--r--common.c29
1 files changed, 23 insertions, 6 deletions
diff --git a/common.c b/common.c
index 8246b8e1..ae626789 100644
--- a/common.c
+++ b/common.c
@@ -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__