From 83ee9708b1c0ed3c262e0148ea8cdc880f177d0d Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sun, 18 Sep 2011 16:22:38 -0400 Subject: src/util: provide strndup definitions where needed Signed-off-by: Dave Reisner Signed-off-by: Dan McGee --- src/util/pacsort.c | 22 ++++++++++++++++++++++ src/util/pactree.c | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) (limited to 'src') diff --git a/src/util/pacsort.c b/src/util/pacsort.c index 2b12f097..0eedf59d 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -48,6 +48,28 @@ static struct options_t { char delim; } opts; +#ifndef HAVE_STRNDUP +/* A quick and dirty implementation derived from glibc */ +static size_t strnlen(const char *s, size_t max) +{ + register const char *p; + for(p = s; *p && max--; ++p); + return (p - s); +} + +char *strndup(const char *s, size_t n) +{ + size_t len = strnlen(s, n); + char *new = (char *) malloc(len + 1); + + if(new == NULL) + return NULL; + + new[len] = '\0'; + return (char *)memcpy(new, s, len); +} +#endif + static struct buffer_t *buffer_new(size_t initial_size) { struct buffer_t *buf; diff --git a/src/util/pactree.c b/src/util/pactree.c index 11ca4b9e..09fe1011 100644 --- a/src/util/pactree.c +++ b/src/util/pactree.c @@ -91,6 +91,28 @@ int unique = 0; int searchsyncs = 0; const char *dbpath = DBPATH; +#ifndef HAVE_STRNDUP +/* A quick and dirty implementation derived from glibc */ +static size_t strnlen(const char *s, size_t max) +{ + register const char *p; + for(p = s; *p && max--; ++p); + return (p - s); +} + +char *strndup(const char *s, size_t n) +{ + size_t len = strnlen(s, n); + char *new = (char *) malloc(len + 1); + + if(new == NULL) + return NULL; + + new[len] = '\0'; + return (char *)memcpy(new, s, len); +} +#endif + static char *strtrim(char *str) { char *pch = str; -- cgit v1.2.3-70-g09d2