diff options
author | Rob Landley <rob@landley.net> | 2016-01-28 13:36:12 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2016-01-28 13:36:12 -0600 |
commit | 8f7137e4e4850e17eea8c045865885bb1bc2f3bc (patch) | |
tree | bea4c382b22f63358f0eaa04d2c944234f04aa19 /lib | |
parent | 33f50f5ff5930815e0a6a6e4af55905df2ec6bdf (diff) | |
download | toybox-8f7137e4e4850e17eea8c045865885bb1bc2f3bc.tar.gz |
Bugfix I forgot to checkin, plus a wrapper function.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.h | 1 | ||||
-rw-r--r-- | lib/xwrap.c | 12 |
2 files changed, 11 insertions, 2 deletions
@@ -101,6 +101,7 @@ void *xzalloc(size_t size); void *xrealloc(void *ptr, size_t size); char *xstrndup(char *s, size_t n); char *xstrdup(char *s); +void *xmemdup(void *s, long len); char *xmprintf(char *format, ...) printf_format; void xprintf(char *format, ...) printf_format; void xputs(char *s); diff --git a/lib/xwrap.c b/lib/xwrap.c index a20d4b6b..65e3018d 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -21,9 +21,9 @@ void xstrncpy(char *dest, char *src, size_t size) void xstrncat(char *dest, char *src, size_t size) { - long len = strlen(src); + long len = strlen(dest); - if (len+strlen(dest)+1 > size) + if (len+strlen(src)+1 > size) error_exit("'%s%s' > %ld bytes", dest, src, (long)size); strcpy(dest+len, src); } @@ -80,6 +80,14 @@ char *xstrdup(char *s) return xstrndup(s, strlen(s)); } +void *xmemdup(void *s, long len) +{ + void *ret = xmalloc(len); + memcpy(ret, s, len); + + return ret; +} + // Die unless we can allocate enough space to sprintf() into. char *xmprintf(char *format, ...) { |