xref: /freebsd/sys/libkern/strdup.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
196c4266cSRobert Watson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
496c4266cSRobert Watson  * Copyright (c) 2003 Networks Associates Technology, Inc.
596c4266cSRobert Watson  * All rights reserved.
696c4266cSRobert Watson  *
7ae4f61f0SRobert Watson  * This software was developed for the FreeBSD Project by Network
8ae4f61f0SRobert Watson  * Associates Laboratories, the Security Research Division of Network
9ae4f61f0SRobert Watson  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10ae4f61f0SRobert Watson  * ("CBOSS"), as part of the DARPA CHATS research program.
1196c4266cSRobert Watson  *
1296c4266cSRobert Watson  * Redistribution and use in source and binary forms, with or without
1396c4266cSRobert Watson  * modification, are permitted provided that the following conditions
1496c4266cSRobert Watson  * are met:
1596c4266cSRobert Watson  * 1. Redistributions of source code must retain the above copyright
1696c4266cSRobert Watson  *    notice, this list of conditions and the following disclaimer.
1796c4266cSRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
1896c4266cSRobert Watson  *    notice, this list of conditions and the following disclaimer in the
1996c4266cSRobert Watson  *    documentation and/or other materials provided with the distribution.
2096c4266cSRobert Watson  *
2196c4266cSRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2296c4266cSRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2396c4266cSRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2496c4266cSRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2596c4266cSRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2696c4266cSRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2796c4266cSRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2896c4266cSRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2996c4266cSRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3096c4266cSRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3196c4266cSRobert Watson  * SUCH DAMAGE.
3296c4266cSRobert Watson  */
3396c4266cSRobert Watson 
34ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
35ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
36ab0de15bSDavid E. O'Brien 
3796c4266cSRobert Watson #include <sys/param.h>
3896c4266cSRobert Watson #include <sys/kernel.h>
3996c4266cSRobert Watson #include <sys/libkern.h>
4096c4266cSRobert Watson #include <sys/malloc.h>
4196c4266cSRobert Watson 
4296c4266cSRobert Watson char *
43744799eaSMatt Macy strdup_flags(const char *string, struct malloc_type *type, int flags)
4496c4266cSRobert Watson {
4596c4266cSRobert Watson 	size_t len;
4696c4266cSRobert Watson 	char *copy;
4796c4266cSRobert Watson 
4896c4266cSRobert Watson 	len = strlen(string) + 1;
49744799eaSMatt Macy 	copy = malloc(len, type, flags);
50744799eaSMatt Macy 	if (copy == NULL)
51744799eaSMatt Macy 		return (NULL);
5296c4266cSRobert Watson 	bcopy(string, copy, len);
5396c4266cSRobert Watson 	return (copy);
5496c4266cSRobert Watson }
55744799eaSMatt Macy 
56744799eaSMatt Macy char *
57744799eaSMatt Macy strdup(const char *string, struct malloc_type *type)
58744799eaSMatt Macy {
59744799eaSMatt Macy 
60744799eaSMatt Macy 	return (strdup_flags(string, type, M_WAITOK));
61744799eaSMatt Macy }
62