strdup.c revision 38451
131921Sbrian/* 231921Sbrian * Copyright (c) 1988, 1993 331921Sbrian * The Regents of the University of California. All rights reserved. 431921Sbrian * 531921Sbrian * Redistribution and use in source and binary forms, with or without 631921Sbrian * modification, are permitted provided that the following conditions 731921Sbrian * are met: 831921Sbrian * 1. Redistributions of source code must retain the above copyright 931921Sbrian * notice, this list of conditions and the following disclaimer. 1031921Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1131921Sbrian * notice, this list of conditions and the following disclaimer in the 1231921Sbrian * documentation and/or other materials provided with the distribution. 1331921Sbrian * 3. All advertising materials mentioning features or use of this software 1431921Sbrian * must display the following acknowledgement: 1531921Sbrian * This product includes software developed by the University of 1631921Sbrian * California, Berkeley and its contributors. 1731921Sbrian * 4. Neither the name of the University nor the names of its contributors 1831921Sbrian * may be used to endorse or promote products derived from this software 1931921Sbrian * without specific prior written permission. 2031921Sbrian * 2131921Sbrian * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2231921Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2331921Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2431921Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2531921Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2650479Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2730715Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2830715Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2936285Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3026940Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3136285Sbrian * SUCH DAMAGE. 3258028Sbrian */ 3336285Sbrian 3436285Sbrian#if defined(LIBC_SCCS) && !defined(lint) 3571657Sbrianstatic char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; 3671657Sbrian#endif /* LIBC_SCCS and not lint */ 3736285Sbrian 3871657Sbrian#include "stand.h" 3971657Sbrian#include <stddef.h> 4071657Sbrian#include <string.h> 4171657Sbrian 4271657Sbrianchar * 4336285Sbrianstrdup(str) 4436285Sbrian const char *str; 4571657Sbrian{ 4671657Sbrian size_t len; 4771657Sbrian char *copy = NULL; 4871657Sbrian 4971657Sbrian if (str != NULL) { 5071657Sbrian len = strlen(str) + 1; 5171657Sbrian if ((copy = malloc(len)) == NULL) 5236285Sbrian return (NULL); 5336285Sbrian memcpy(copy, str, len); 5436285Sbrian } 5536285Sbrian return (copy); 5636285Sbrian} 5771657Sbrian