]> arthur.barton.de Git - ngircd-alex.git/commitdiff
private strndup() implementation in case libc does not provide it
authorFederico G. Schwindt <fgsch@lodoss.net>
Mon, 26 Aug 2013 09:47:04 +0000 (10:47 +0100)
committerFederico G. Schwindt <fgsch@lodoss.net>
Mon, 26 Aug 2013 09:47:04 +0000 (10:47 +0100)
configure.ng
src/portab/Makefile.ng
src/portab/portab.h
src/portab/strndup.c [new file with mode: 0644]

index 7e8522515192677b67275a2b0d1e8b8bc5b64e83..faf3086b6b87b2d5d3d964e9024f4f067d8c056e 100644 (file)
@@ -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 --
 
index dac329fa7c0954b399f2298955644ebe03ff7b4a..17edbdf21b09f087e1606fc73794522f811c9ef8 100644 (file)
@@ -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
 
index 208d3500342a1d2aba40b07a7eee0af9a4ec98c3..a968a3b9bd924fbbe6e04eee47d1dc5f4f925e21 100644 (file)
@@ -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 (file)
index 0000000..d6e01c9
--- /dev/null
@@ -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
+