xref: /freebsd-src/sys/libkern/strndup.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1*3914ddf8SEdward Tomasz Napierala /*-
2*3914ddf8SEdward Tomasz Napierala  * Copyright (c) 2003 Networks Associates Technology, Inc.
3*3914ddf8SEdward Tomasz Napierala  * All rights reserved.
4*3914ddf8SEdward Tomasz Napierala  *
5*3914ddf8SEdward Tomasz Napierala  * This software was developed for the FreeBSD Project by Network
6*3914ddf8SEdward Tomasz Napierala  * Associates Laboratories, the Security Research Division of Network
7*3914ddf8SEdward Tomasz Napierala  * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
8*3914ddf8SEdward Tomasz Napierala  * ("CBOSS"), as part of the DARPA CHATS research program.
9*3914ddf8SEdward Tomasz Napierala  *
10*3914ddf8SEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
11*3914ddf8SEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
12*3914ddf8SEdward Tomasz Napierala  * are met:
13*3914ddf8SEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
14*3914ddf8SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
15*3914ddf8SEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
16*3914ddf8SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
17*3914ddf8SEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
18*3914ddf8SEdward Tomasz Napierala  *
19*3914ddf8SEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20*3914ddf8SEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*3914ddf8SEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*3914ddf8SEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23*3914ddf8SEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*3914ddf8SEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*3914ddf8SEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*3914ddf8SEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*3914ddf8SEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*3914ddf8SEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*3914ddf8SEdward Tomasz Napierala  * SUCH DAMAGE.
30*3914ddf8SEdward Tomasz Napierala  */
31*3914ddf8SEdward Tomasz Napierala 
32*3914ddf8SEdward Tomasz Napierala #include <sys/param.h>
33*3914ddf8SEdward Tomasz Napierala #include <sys/kernel.h>
34*3914ddf8SEdward Tomasz Napierala #include <sys/libkern.h>
35*3914ddf8SEdward Tomasz Napierala #include <sys/malloc.h>
36*3914ddf8SEdward Tomasz Napierala 
37*3914ddf8SEdward Tomasz Napierala char *
strndup(const char * string,size_t maxlen,struct malloc_type * type)38*3914ddf8SEdward Tomasz Napierala strndup(const char *string, size_t maxlen, struct malloc_type *type)
39*3914ddf8SEdward Tomasz Napierala {
40*3914ddf8SEdward Tomasz Napierala 	size_t len;
41*3914ddf8SEdward Tomasz Napierala 	char *copy;
42*3914ddf8SEdward Tomasz Napierala 
43*3914ddf8SEdward Tomasz Napierala 	len = strnlen(string, maxlen) + 1;
44*3914ddf8SEdward Tomasz Napierala 	copy = malloc(len, type, M_WAITOK);
45*3914ddf8SEdward Tomasz Napierala 	bcopy(string, copy, len);
46*3914ddf8SEdward Tomasz Napierala 	copy[len - 1] = '\0';
47*3914ddf8SEdward Tomasz Napierala 	return (copy);
48*3914ddf8SEdward Tomasz Napierala }
49