strdup.c revision 251069
1107590Sobrien/*
2117395Skan * Copyright (c) 1988, 1993
3107590Sobrien *	The Regents of the University of California.  All rights reserved.
4107590Sobrien *
5107590Sobrien * Redistribution and use in source and binary forms, with or without
6107590Sobrien * modification, are permitted provided that the following conditions
7107590Sobrien * are met:
8107590Sobrien * 1. Redistributions of source code must retain the above copyright
9107590Sobrien *    notice, this list of conditions and the following disclaimer.
10107590Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11107590Sobrien *    notice, this list of conditions and the following disclaimer in the
12107590Sobrien *    documentation and/or other materials provided with the distribution.
13107590Sobrien * 3. Neither the name of the University nor the names of its contributors
14107590Sobrien *    may be used to endorse or promote products derived from this software
15107590Sobrien *    without specific prior written permission.
16107590Sobrien *
17107590Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18107590Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19107590Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20107590Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21107590Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22107590Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23107590Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24107590Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25107590Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26107590Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27107590Sobrien * SUCH DAMAGE.
28107590Sobrien */
29107590Sobrien
30107590Sobrien#if defined(LIBC_SCCS) && !defined(lint)
31107590Sobrienstatic char sccsid[] = "@(#)strdup.c	8.1 (Berkeley) 6/4/93";
32107590Sobrien#endif /* LIBC_SCCS and not lint */
33107590Sobrien#include <sys/cdefs.h>
34107590Sobrien__FBSDID("$FreeBSD: head/lib/libc/string/strdup.c 251069 2013-05-28 20:57:40Z emaste $");
35107590Sobrien
36107590Sobrien#include <stddef.h>
37107590Sobrien#include <stdlib.h>
38107590Sobrien#include <string.h>
39107590Sobrien
40107590Sobrienchar *
41107590Sobrienstrdup(const char *str)
42107590Sobrien{
43107590Sobrien	size_t len;
44107590Sobrien	char *copy;
45107590Sobrien
46107590Sobrien	len = strlen(str) + 1;
47107590Sobrien	if ((copy = malloc(len)) == NULL)
48117395Skan		return (NULL);
49117395Skan	memcpy(copy, str, len);
50107590Sobrien	return (copy);
51107590Sobrien}
52107590Sobrien