]> git.sur5r.net Git - i3/i3/blob - libi3/strndup.c
move strndup to libi3
[i3/i3] / libi3 / strndup.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2011 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <sys/types.h>
12 #include <string.h>
13
14 #include "libi3.h"
15
16 #if defined(__APPLE__)
17
18 /*
19  * Taken from FreeBSD
20  * Returns a pointer to a new string which is a duplicate of the
21  * string, but only copies at most n characters.
22  *
23  */
24 char *strndup(const char *str, size_t n) {
25     size_t len;
26     char *copy;
27
28     for (len = 0; len < n && str[len]; len++)
29         continue;
30
31     copy = smalloc(len + 1);
32     memcpy(copy, str, len);
33     copy[len] = '\0';
34     return (copy);
35 }
36
37 #endif