From bb94729c098f21ae850044a9201c1d5b92baea1f Mon Sep 17 00:00:00 2001 From: Will Miles Date: Fri, 27 Feb 2015 14:43:23 -0500 Subject: util: Use util-common for strndup in pacsort and pactree This small refactor reduces the number of replications of the local imeplementation of strndup. Signed-off-by: Will Miles Signed-off-by: Allan McRae --- src/util/Makefile.am | 4 ++-- src/util/pacsort.c | 23 +---------------------- src/util/pactree.c | 24 ++---------------------- src/util/util-common.c | 1 + src/util/util-common.h | 1 + 5 files changed, 7 insertions(+), 46 deletions(-) create mode 120000 src/util/util-common.c create mode 120000 src/util/util-common.h (limited to 'src') diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 25c025b5..936d7ffb 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -20,10 +20,10 @@ AM_CFLAGS = -pedantic -D_GNU_SOURCE $(WARNING_CFLAGS) cleanupdelta_SOURCES = cleanupdelta.c cleanupdelta_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la -pacsort_SOURCES = pacsort.c +pacsort_SOURCES = pacsort.c util-common.c pacsort_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la -pactree_SOURCES = pactree.c +pactree_SOURCES = pactree.c util-common.c pactree_LDADD = $(top_builddir)/lib/libalpm/.libs/libalpm.la testpkg_SOURCES = testpkg.c diff --git a/src/util/pacsort.c b/src/util/pacsort.c index d864fdb9..9d248fad 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -25,6 +25,7 @@ #include #include +#include "util-common.h" #define DELIM ' ' @@ -69,28 +70,6 @@ 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 67f456fe..11ad7cae 100644 --- a/src/util/pactree.c +++ b/src/util/pactree.c @@ -29,6 +29,8 @@ #include #endif +#include "util-common.h" + #define LINE_MAX 512 typedef struct tdepth { @@ -121,28 +123,6 @@ int searchsyncs = 0; const char *dbpath = DBPATH; const char *configfile = CONFFILE; -#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 size_t strtrim(char *str) { char *end, *pch = str; diff --git a/src/util/util-common.c b/src/util/util-common.c new file mode 120000 index 00000000..a2f6c508 --- /dev/null +++ b/src/util/util-common.c @@ -0,0 +1 @@ +../common/util-common.c \ No newline at end of file diff --git a/src/util/util-common.h b/src/util/util-common.h new file mode 120000 index 00000000..3f0b9826 --- /dev/null +++ b/src/util/util-common.h @@ -0,0 +1 @@ +../common/util-common.h \ No newline at end of file -- cgit v1.2.3-70-g09d2