summary refs log tree commit diff
diff options
context:
space:
mode:
authorFederico G. Schwindt <fgsch@lodoss.net>2013-08-26 10:47:04 +0100
committerFederico G. Schwindt <fgsch@lodoss.net>2013-08-26 10:47:04 +0100
commit6ac5a82eecb76ec35f3f484149ad668073a52620 (patch)
tree1e83126d7bffd98ee7a0e4ee9287e760d3ab4629
parent086cf3a2723e2dcc8e1acf49d166e254fe22e7cf (diff)
downloadngircd-6ac5a82eecb76ec35f3f484149ad668073a52620.tar.gz
ngircd-6ac5a82eecb76ec35f3f484149ad668073a52620.zip
private strndup() implementation in case libc does not provide it
-rw-r--r--configure.ng2
-rw-r--r--src/portab/Makefile.ng2
-rw-r--r--src/portab/portab.h4
-rw-r--r--src/portab/strndup.c37
4 files changed, 43 insertions, 2 deletions
diff --git a/configure.ng b/configure.ng
index 7e852251..faf3086b 100644
--- a/configure.ng
+++ b/configure.ng
@@ -188,7 +188,7 @@ AC_CHECK_FUNCS([ \
 # Optional functions
 AC_CHECK_FUNCS_ONCE([ \
 	gai_strerror getaddrinfo getnameinfo inet_aton sigaction sigprocmask \
-	snprintf vsnprintf strdup strlcpy strlcat strtok_r waitpid])
+	snprintf vsnprintf strdup strndup strlcpy strlcat strtok_r waitpid])
 
 # -- Configuration options --
 
diff --git a/src/portab/Makefile.ng b/src/portab/Makefile.ng
index dac329fa..17edbdf2 100644
--- a/src/portab/Makefile.ng
+++ b/src/portab/Makefile.ng
@@ -15,7 +15,7 @@ EXTRA_DIST = Makefile.ng
 
 noinst_LIBRARIES = libngportab.a
 
-libngportab_a_SOURCES = strdup.c strlcpy.c strtok_r.c vsnprintf.c waitpid.c
+libngportab_a_SOURCES = strdup.c strndup.c strlcpy.c strtok_r.c vsnprintf.c waitpid.c
 
 check_PROGRAMS = portabtest
 
diff --git a/src/portab/portab.h b/src/portab/portab.h
index 208d3500..a968a3b9 100644
--- a/src/portab/portab.h
+++ b/src/portab/portab.h
@@ -157,6 +157,10 @@ extern size_t strlcpy PARAMS(( char *dst, const char *src, size_t size ));
 extern char * strdup PARAMS(( const char *s ));
 #endif
 
+#ifndef HAVE_STRNDUP
+extern char * strndup PARAMS((const char *s, size_t maxlen));
+#endif
+
 #ifndef HAVE_STRTOK_R
 extern char * strtok_r PARAMS((char *str, const char *delim, char **saveptr));
 #endif
diff --git a/src/portab/strndup.c b/src/portab/strndup.c
new file mode 100644
index 00000000..d6e01c94
--- /dev/null
+++ b/src/portab/strndup.c
@@ -0,0 +1,37 @@
+/*
+ * ngIRCd -- The Next Generation IRC Daemon
+ */
+
+#include "portab.h"
+
+/**
+ * @file
+ * strndup() implementation. Public domain.
+ */
+
+#ifndef HAVE_STRNDUP
+
+#include "imp.h"
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+
+#include "exp.h"
+
+GLOBAL char *
+strndup(const char *s, size_t maxlen)
+{
+	char *dup;
+	size_t len = strlen(s);
+
+	if (len > maxlen)
+		len = maxlen;
+	len++;
+	dup = malloc(len);
+	if (dup)
+		strlcpy(dup, s, len);
+	return dup;
+}
+
+#endif
+