2 * Replacement for a missing strndup.
4 * The canonical version of this file is maintained in the rra-c-util package,
5 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
7 * Written by Russ Allbery <eagle@eyrie.org>
9 * The authors hereby relinquish any claim to any copyright that they may have
10 * in this work, whether granted under contract or by operation of law or
11 * international treaty, and hereby commit to the public, at large, that they
12 * shall not, at any time in the future, seek to enforce any copyright in this
13 * work against any person or entity, or prevent any person or entity from
14 * copying, publishing, distributing or creating derivative works of this
19 #include <portable/system.h>
24 * If we're running the test suite, rename the functions to avoid conflicts
25 * with the system versions.
29 # define strndup test_strndup
30 char *test_strndup(const char *, size_t);
34 strndup(const char *s, size_t n)
45 /* Don't assume that the source string is nul-terminated. */
46 for (p = s; (size_t) (p - s) < n && *p != '\0'; p++)
49 copy = malloc(length + 1);
52 memcpy(copy, s, length);