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