summaryrefslogtreecommitdiffstats
path: root/usr/klibc/strndup.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/klibc/strndup.c')
-rw-r--r--usr/klibc/strndup.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/usr/klibc/strndup.c b/usr/klibc/strndup.c
new file mode 100644
index 0000000..20eaa8b
--- /dev/null
+++ b/usr/klibc/strndup.c
@@ -0,0 +1,18 @@
+/*
+ * strndup.c
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+char *strndup(const char *s, size_t n)
+{
+ size_t l = strnlen(s, n);
+ char *d = malloc(l + 1);
+ if (!d)
+ return NULL;
+
+ memcpy(d, s, l);
+ d[l] = '\0';
+ return d;
+}