Commit c62cef66 authored by Dag Erling Smørgrav's avatar Dag Erling Smørgrav

Add strndup() to libcompat.

git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@882 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent cb74e1d4
......@@ -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)
{
......
......@@ -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
......
......@@ -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 \
......
/*
* $Id$
*/
#ifndef COMPAT_STRNDUP_H_INCLUDED
#define COMPAT_STRNDUP_H_INCLUDED
#ifndef HAVE_STRNDUP
char *strndup(const char *str, size_t len);
#endif
#endif
......@@ -11,6 +11,7 @@ libcompat_a_SOURCES = \
srandomdev.c \
strlcat.c \
strlcpy.c \
strndup.c \
vis.c
libcompat_a_CFLAGS = -include config.h
/*
* $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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment