strdup.c revision 84221
1233545Sjchandra/* 2233545Sjchandra * Copyright (c) 1988, 1993 3233545Sjchandra * The Regents of the University of California. All rights reserved. 4233545Sjchandra * 5233545Sjchandra * Redistribution and use in source and binary forms, with or without 6233545Sjchandra * modification, are permitted provided that the following conditions 7233545Sjchandra * are met: 8233545Sjchandra * 1. Redistributions of source code must retain the above copyright 9233545Sjchandra * notice, this list of conditions and the following disclaimer. 10233545Sjchandra * 2. Redistributions in binary form must reproduce the above copyright 11233545Sjchandra * notice, this list of conditions and the following disclaimer in the 12233545Sjchandra * documentation and/or other materials provided with the distribution. 13233545Sjchandra * 3. All advertising materials mentioning features or use of this software 14233545Sjchandra * must display the following acknowledgement: 15233545Sjchandra * This product includes software developed by the University of 16233545Sjchandra * California, Berkeley and its contributors. 17233545Sjchandra * 4. Neither the name of the University nor the names of its contributors 18233545Sjchandra * may be used to endorse or promote products derived from this software 19233545Sjchandra * without specific prior written permission. 20233545Sjchandra * 21233545Sjchandra * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22233545Sjchandra * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233545Sjchandra * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233545Sjchandra * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25233545Sjchandra * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233545Sjchandra * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233545Sjchandra * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233545Sjchandra * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233545Sjchandra * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233545Sjchandra * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233545Sjchandra * SUCH DAMAGE. 32233545Sjchandra */ 33233545Sjchandra 34233545Sjchandra#include <sys/cdefs.h> 35233545Sjchandra__FBSDID("$FreeBSD: head/lib/libstand/strdup.c 84221 2001-09-30 22:28:01Z dillon $"); 36233545Sjchandra 37233545Sjchandra#if defined(LIBC_SCCS) && !defined(lint) 38233545Sjchandrastatic char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; 39233545Sjchandra#endif /* LIBC_SCCS and not lint */ 40233545Sjchandra 41233545Sjchandra#include "stand.h" 42233545Sjchandra#include <stddef.h> 43233545Sjchandra#include <string.h> 44233545Sjchandra 45233545Sjchandrachar * 46233545Sjchandrastrdup(str) 47233545Sjchandra const char *str; 48233545Sjchandra{ 49233545Sjchandra size_t len; 50233545Sjchandra char *copy = NULL; 51233545Sjchandra 52233545Sjchandra if (str != NULL) { 53233545Sjchandra len = strlen(str) + 1; 54233545Sjchandra if ((copy = malloc(len)) == NULL) 55233545Sjchandra return (NULL); 56233545Sjchandra memcpy(copy, str, len); 57233545Sjchandra } 58233545Sjchandra return (copy); 59233545Sjchandra} 60233545Sjchandra