xref: /freebsd/sys/libkern/strdup.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
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 
3496c4266cSRobert Watson #include <sys/param.h>
3596c4266cSRobert Watson #include <sys/kernel.h>
3696c4266cSRobert Watson #include <sys/libkern.h>
3796c4266cSRobert Watson #include <sys/malloc.h>
3896c4266cSRobert Watson 
3996c4266cSRobert Watson char *
strdup_flags(const char * string,struct malloc_type * type,int flags)40744799eaSMatt Macy strdup_flags(const char *string, struct malloc_type *type, int flags)
4196c4266cSRobert Watson {
4296c4266cSRobert Watson 	size_t len;
4396c4266cSRobert Watson 	char *copy;
4496c4266cSRobert Watson 
4596c4266cSRobert Watson 	len = strlen(string) + 1;
46744799eaSMatt Macy 	copy = malloc(len, type, flags);
47744799eaSMatt Macy 	if (copy == NULL)
48744799eaSMatt Macy 		return (NULL);
4996c4266cSRobert Watson 	bcopy(string, copy, len);
5096c4266cSRobert Watson 	return (copy);
5196c4266cSRobert Watson }
52744799eaSMatt Macy 
53744799eaSMatt Macy char *
strdup(const char * string,struct malloc_type * type)54744799eaSMatt Macy strdup(const char *string, struct malloc_type *type)
55744799eaSMatt Macy {
56744799eaSMatt Macy 
57744799eaSMatt Macy 	return (strdup_flags(string, type, M_WAITOK));
58744799eaSMatt Macy }
59