]> git.sur5r.net Git - i3/i3/blobdiff - libi3/strndup.c
move strndup to libi3
[i3/i3] / libi3 / strndup.c
diff --git a/libi3/strndup.c b/libi3/strndup.c
new file mode 100644 (file)
index 0000000..f02e0af
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * vim:ts=4:sw=4:expandtab
+ *
+ * i3 - an improved dynamic tiling window manager
+ *
+ * © 2009-2011 Michael Stapelberg and contributors
+ *
+ * See file LICENSE for license information.
+ *
+ */
+#include <sys/types.h>
+#include <string.h>
+
+#include "libi3.h"
+
+#if defined(__APPLE__)
+
+/*
+ * Taken from FreeBSD
+ * Returns a pointer to a new string which is a duplicate of the
+ * string, but only copies at most n characters.
+ *
+ */
+char *strndup(const char *str, size_t n) {
+    size_t len;
+    char *copy;
+
+    for (len = 0; len < n && str[len]; len++)
+        continue;
+
+    copy = smalloc(len + 1);
+    memcpy(copy, str, len);
+    copy[len] = '\0';
+    return (copy);
+}
+
+#endif