]> err.no Git - varnish/commitdiff
Add strndup() to libcompat.
authordes <des@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 22 Aug 2006 07:11:59 +0000 (07:11 +0000)
committerdes <des@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Tue, 22 Aug 2006 07:11:59 +0000 (07:11 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@882 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/tcp.c
varnish-cache/configure.ac
varnish-cache/include/Makefile.am
varnish-cache/include/compat/strndup.h [new file with mode: 0644]
varnish-cache/lib/libcompat/Makefile.am
varnish-cache/lib/libcompat/strndup.c [new file with mode: 0644]

index d59afe60e4e370aad051a24b3f8179f249dff76e..5cfe38033a1cac943434a2a4254a57c26623dafe 100644 (file)
 #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)
 {
index c3faca72e4a6b47ffe189db1c8d255ad850d39a7..60db17662450ce103bef383fde2121ee00276fd1 100644 (file)
@@ -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
index c2350df61d51fd1b66b89aaaeaa0666941ed9e09..254710af820dd32127831eaef0767129c6759f2c 100644 (file)
@@ -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 (file)
index 0000000..163e226
--- /dev/null
@@ -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
index 7448bc2786de1e69bdc608c9d6cb21452ac6ef1c..885d3a075677a6db6446e5f0b350f5f272199b9c 100644 (file)
@@ -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 (file)
index 0000000..a88493a
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * $Id$
+ *
+ */
+
+#ifndef HAVE_STRNDUP
+
+#include <stdlib.h>
+#include <string.h>
+
+#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