1185690Skib/*      $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere Exp $       */
2185690Skib
3185690Skib/*
4185690Skib * Copyright (c) 1988, 1993
5185690Skib *	The Regents of the University of California.  All rights reserved.
6185690Skib *
7185690Skib * Redistribution and use in source and binary forms, with or without
8185690Skib * modification, are permitted provided that the following conditions
9185690Skib * are met:
10185690Skib * 1. Redistributions of source code must retain the above copyright
11185690Skib *    notice, this list of conditions and the following disclaimer.
12185690Skib * 2. Redistributions in binary form must reproduce the above copyright
13185690Skib *    notice, this list of conditions and the following disclaimer in the
14185690Skib *    documentation and/or other materials provided with the distribution.
15251069Semaste * 3. Neither the name of the University nor the names of its contributors
16185690Skib *    may be used to endorse or promote products derived from this software
17185690Skib *    without specific prior written permission.
18185690Skib *
19185690Skib * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20185690Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21185690Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22185690Skib * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23185690Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24185690Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25185690Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26185690Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27185690Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28185690Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29185690Skib * SUCH DAMAGE.
30185690Skib */
31185690Skib
32185690Skib#include <sys/cdefs.h>
33185690Skib__FBSDID("$FreeBSD$");
34185690Skib
35185690Skib#include <stddef.h>
36185690Skib#include <stdlib.h>
37185690Skib#include <string.h>
38185690Skib
39185690Skibchar *
40185690Skibstrndup(const char *str, size_t n)
41185690Skib{
42185690Skib	size_t len;
43185690Skib	char *copy;
44185690Skib
45203391Sed	len = strnlen(str, n);
46185690Skib	if ((copy = malloc(len + 1)) == NULL)
47185690Skib		return (NULL);
48185690Skib	memcpy(copy, str, len);
49185690Skib	copy[len] = '\0';
50185690Skib	return (copy);
51185690Skib}
52