From bdd59ba05078b6195190962777f0ff95c5b6cd95 Mon Sep 17 00:00:00 2001 From: des Date: Tue, 22 Aug 2006 07:11:59 +0000 Subject: [PATCH] Add strndup() to libcompat. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@882 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/tcp.c | 17 ++++------------- varnish-cache/configure.ac | 1 + varnish-cache/include/Makefile.am | 1 + varnish-cache/include/compat/strndup.h | 12 ++++++++++++ varnish-cache/lib/libcompat/Makefile.am | 1 + varnish-cache/lib/libcompat/strndup.c | 24 ++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 varnish-cache/include/compat/strndup.h create mode 100644 varnish-cache/lib/libcompat/strndup.c diff --git a/varnish-cache/bin/varnishd/tcp.c b/varnish-cache/bin/varnishd/tcp.c index d59afe60..5cfe3803 100644 --- a/varnish-cache/bin/varnishd/tcp.c +++ b/varnish-cache/bin/varnishd/tcp.c @@ -17,6 +17,10 @@ #ifndef HAVE_STRLCPY #include "compat/strlcpy.h" #endif +#ifndef HAVE_STRNDUP +#include "compat/strndup.h" +#endif + #include "mgt.h" /*--------------------------------------------------------------------*/ @@ -75,19 +79,6 @@ accept_filter(int fd) } #endif -static char * -strndup(const char *p, unsigned n) -{ - char *q; - - q = malloc(n + 1); - if (q != NULL) { - memcpy(q, p, n); - q[n] = '\0'; - } - return (q); -} - int TCP_parse(const char *str, char **addr, char **port) { diff --git a/varnish-cache/configure.ac b/varnish-cache/configure.ac index c3faca72..60db1766 100644 --- a/varnish-cache/configure.ac +++ b/varnish-cache/configure.ac @@ -71,6 +71,7 @@ AC_CHECK_FUNCS([asprintf vasprintf]) AC_CHECK_FUNCS([setproctitle]) AC_CHECK_FUNCS([srandomdev]) AC_CHECK_FUNCS([strlcat strlcpy]) +AC_CHECK_FUNCS([strndup]) AC_CHECK_FUNCS([vis strvis strvisx]) # On some systems, clock_gettime is in librt rather than libc diff --git a/varnish-cache/include/Makefile.am b/varnish-cache/include/Makefile.am index c2350df6..254710af 100644 --- a/varnish-cache/include/Makefile.am +++ b/varnish-cache/include/Makefile.am @@ -10,6 +10,7 @@ noinst_HEADERS = \ compat/srandomdev.h \ compat/strlcat.h \ compat/strlcpy.h \ + compat/strndup.h \ compat/vasprintf.h \ compat/vis.h \ hash.h \ diff --git a/varnish-cache/include/compat/strndup.h b/varnish-cache/include/compat/strndup.h new file mode 100644 index 00000000..163e226e --- /dev/null +++ b/varnish-cache/include/compat/strndup.h @@ -0,0 +1,12 @@ +/* + * $Id$ + */ + +#ifndef COMPAT_STRNDUP_H_INCLUDED +#define COMPAT_STRNDUP_H_INCLUDED + +#ifndef HAVE_STRNDUP +char *strndup(const char *str, size_t len); +#endif + +#endif diff --git a/varnish-cache/lib/libcompat/Makefile.am b/varnish-cache/lib/libcompat/Makefile.am index 7448bc27..885d3a07 100644 --- a/varnish-cache/lib/libcompat/Makefile.am +++ b/varnish-cache/lib/libcompat/Makefile.am @@ -11,6 +11,7 @@ libcompat_a_SOURCES = \ srandomdev.c \ strlcat.c \ strlcpy.c \ + strndup.c \ vis.c libcompat_a_CFLAGS = -include config.h diff --git a/varnish-cache/lib/libcompat/strndup.c b/varnish-cache/lib/libcompat/strndup.c new file mode 100644 index 00000000..a88493af --- /dev/null +++ b/varnish-cache/lib/libcompat/strndup.c @@ -0,0 +1,24 @@ +/* + * $Id$ + * + */ + +#ifndef HAVE_STRNDUP + +#include +#include + +#include "compat/strndup.h" + +char * +strndup(const char *str, size_t len) +{ + char *dup; + + /* wasteful if len is large and str is short */ + if ((dup = calloc(len + 1, 1)) != NULL) + strncpy(dup, str, len); + return (dup); +} + +#endif -- 2.39.5