]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/portab/strndup.c
private strndup() implementation in case libc does not provide it
[ngircd-alex.git] / src / portab / strndup.c
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
+