summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--configure.ac1
-rw-r--r--openbsd-compat/Makefile.in1
-rw-r--r--openbsd-compat/strndup.c43
3 files changed, 45 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index 7e6e1ebda..70e72be77 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1809,6 +1809,7 @@ AC_CHECK_FUNCS([ \
1809 strlcat \ 1809 strlcat \
1810 strlcpy \ 1810 strlcpy \
1811 strmode \ 1811 strmode \
1812 strndup \
1812 strnlen \ 1813 strnlen \
1813 strnvis \ 1814 strnvis \
1814 strptime \ 1815 strptime \
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in
index 93e8da720..8e3b42991 100644
--- a/openbsd-compat/Makefile.in
+++ b/openbsd-compat/Makefile.in
@@ -51,6 +51,7 @@ OPENBSD=base64.o \
51 strlcat.o \ 51 strlcat.o \
52 strlcpy.o \ 52 strlcpy.o \
53 strmode.o \ 53 strmode.o \
54 strndup.o \
54 strnlen.o \ 55 strnlen.o \
55 strptime.o \ 56 strptime.o \
56 strsep.o \ 57 strsep.o \
diff --git a/openbsd-compat/strndup.c b/openbsd-compat/strndup.c
new file mode 100644
index 000000000..0fcb96f6b
--- /dev/null
+++ b/openbsd-compat/strndup.c
@@ -0,0 +1,43 @@
1/* $OpenBSD: strndup.c,v 1.2 2015/08/31 02:53:57 guenther Exp $ */
2
3/*
4 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include "config.h"
20#ifndef HAVE_STRNDUP
21#include <sys/types.h>
22
23#include <stddef.h>
24#include <stdlib.h>
25#include <string.h>
26
27char *
28strndup(const char *str, size_t maxlen)
29{
30 char *copy;
31 size_t len;
32
33 len = strnlen(str, maxlen);
34 copy = malloc(len + 1);
35 if (copy != NULL) {
36 (void)memcpy(copy, str, len);
37 copy[len] = '\0';
38 }
39
40 return copy;
41}
42DEF_WEAK(strndup);
43#endif /* HAVE_STRNDUP */