summaryrefslogtreecommitdiff
path: root/oslib
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2017-06-08 09:37:52 -0600
committerJens Axboe <axboe@kernel.dk>2017-06-08 09:37:52 -0600
commitb29c71c4a1e96390051d012b4e35c8d0718f7ce3 (patch)
treec3523c761932752bcbe37b6c1f635ed64f6c2e6a /oslib
parenta1554f6519e7c91b262a35486d100d757b3b5b8a (diff)
Add strndup() function, if we don't have it
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'oslib')
-rw-r--r--oslib/strndup.c18
-rw-r--r--oslib/strndup.h9
2 files changed, 27 insertions, 0 deletions
diff --git a/oslib/strndup.c b/oslib/strndup.c
new file mode 100644
index 0000000..318ca93
--- /dev/null
+++ b/oslib/strndup.c
@@ -0,0 +1,18 @@
+#include <stdlib.h>
+#include <string.h>
+
+#ifndef CONFIG_HAVE_STRNDUP
+
+char *strndup(const char *s, size_t n)
+{
+ char *str = malloc(n + 1);
+
+ if (str) {
+ strncpy(str, s, n);
+ str[n] = '\0';
+ }
+
+ return str;
+}
+
+#endif
diff --git a/oslib/strndup.h b/oslib/strndup.h
new file mode 100644
index 0000000..669364e
--- /dev/null
+++ b/oslib/strndup.h
@@ -0,0 +1,9 @@
+#ifdef CONFIG_HAVE_STRNDUP
+
+#include <string.h>
+
+#else
+
+char *strndup(const char *s, size_t n);
+
+#endif