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 Board of Trustees of the Leland Stanford Junior University
11 * Copying and distribution of this file, with or without modification, are
12 * permitted in any medium without royalty provided the copyright notice and
13 * this notice are preserved. This file is offered as-is, without any
16 * SPDX-License-Identifier: FSFAP
20 #include <portable/system.h>
25 * If we're running the test suite, rename the functions to avoid conflicts
26 * with the system versions.
30 # define strndup test_strndup
31 char *test_strndup(const char *, size_t);
35 strndup(const char *s, size_t n)
46 /* Don't assume that the source string is nul-terminated. */
47 for (p = s; (size_t) (p - s) < n && *p != '\0'; p++)
50 copy = malloc(length + 1);
53 memcpy(copy, s, length);