9 /* $Id: compat_strndup.c,v 1.1 2018/02/27 11:16:23 schwarze Exp $ */
10 /* OpenBSD: strndup.c,v 1.2 2015/08/31 02:53:57 guenther Exp */
12 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
14 * Permission to use, copy, modify, and distribute this software for any
15 * purpose with or without fee is hereby granted, provided that the above
16 * copyright notice and this permission notice appear in all copies.
18 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
19 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
21 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
23 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
24 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <sys/types.h>
33 strndup(const char *str, size_t maxlen)
38 for (len = 0; len < maxlen && str[len] != '\0'; len++)
41 copy = malloc(len + 1);
43 (void)memcpy(copy, str, len);