strdup.c revision 84221
138451Smsmith/* 238451Smsmith * Copyright (c) 1988, 1993 338451Smsmith * The Regents of the University of California. All rights reserved. 438451Smsmith * 538451Smsmith * Redistribution and use in source and binary forms, with or without 638451Smsmith * modification, are permitted provided that the following conditions 738451Smsmith * are met: 838451Smsmith * 1. Redistributions of source code must retain the above copyright 938451Smsmith * notice, this list of conditions and the following disclaimer. 1038451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1138451Smsmith * notice, this list of conditions and the following disclaimer in the 1238451Smsmith * documentation and/or other materials provided with the distribution. 1338451Smsmith * 3. All advertising materials mentioning features or use of this software 1438451Smsmith * must display the following acknowledgement: 1538451Smsmith * This product includes software developed by the University of 1638451Smsmith * California, Berkeley and its contributors. 1738451Smsmith * 4. Neither the name of the University nor the names of its contributors 1838451Smsmith * may be used to endorse or promote products derived from this software 1938451Smsmith * without specific prior written permission. 2038451Smsmith * 2138451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2238451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2338451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2438451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2538451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2638451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2738451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2838451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2938451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3038451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3138451Smsmith * SUCH DAMAGE. 3238451Smsmith */ 3338451Smsmith 3484221Sdillon#include <sys/cdefs.h> 3584221Sdillon__FBSDID("$FreeBSD: head/lib/libstand/strdup.c 84221 2001-09-30 22:28:01Z dillon $"); 3684221Sdillon 3738451Smsmith#if defined(LIBC_SCCS) && !defined(lint) 3838451Smsmithstatic char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; 3938451Smsmith#endif /* LIBC_SCCS and not lint */ 4038451Smsmith 4138451Smsmith#include "stand.h" 4238451Smsmith#include <stddef.h> 4338451Smsmith#include <string.h> 4438451Smsmith 4538451Smsmithchar * 4638451Smsmithstrdup(str) 4738451Smsmith const char *str; 4838451Smsmith{ 4938451Smsmith size_t len; 5038451Smsmith char *copy = NULL; 5138451Smsmith 5238451Smsmith if (str != NULL) { 5338451Smsmith len = strlen(str) + 1; 5438451Smsmith if ((copy = malloc(len)) == NULL) 5538451Smsmith return (NULL); 5638451Smsmith memcpy(copy, str, len); 5738451Smsmith } 5838451Smsmith return (copy); 5938451Smsmith} 60