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 <wmiles@sgl.com> Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
parent
8f92fe47a7
commit
bb94729c09
5 changed files with 7 additions and 46 deletions
|
@ -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
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <alpm.h>
|
||||
#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;
|
||||
|
|
|
@ -29,6 +29,8 @@
|
|||
#include <langinfo.h>
|
||||
#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;
|
||||
|
|
1
src/util/util-common.c
Symbolic link
1
src/util/util-common.c
Symbolic link
|
@ -0,0 +1 @@
|
|||
../common/util-common.c
|
1
src/util/util-common.h
Symbolic link
1
src/util/util-common.h
Symbolic link
|
@ -0,0 +1 @@
|
|||
../common/util-common.h
|
Loading…
Add table
Reference in a new issue