1270096Strasz/*- 2270096Strasz * Copyright (c) 2003 Networks Associates Technology, Inc. 3270096Strasz * All rights reserved. 4270096Strasz * 5270096Strasz * This software was developed for the FreeBSD Project by Network 6270096Strasz * Associates Laboratories, the Security Research Division of Network 7270096Strasz * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 8270096Strasz * ("CBOSS"), as part of the DARPA CHATS research program. 9270096Strasz * 10270096Strasz * Redistribution and use in source and binary forms, with or without 11270096Strasz * modification, are permitted provided that the following conditions 12270096Strasz * are met: 13270096Strasz * 1. Redistributions of source code must retain the above copyright 14270096Strasz * notice, this list of conditions and the following disclaimer. 15270096Strasz * 2. Redistributions in binary form must reproduce the above copyright 16270096Strasz * notice, this list of conditions and the following disclaimer in the 17270096Strasz * documentation and/or other materials provided with the distribution. 18270096Strasz * 19270096Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22270096Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29270096Strasz * SUCH DAMAGE. 30270096Strasz */ 31270096Strasz 32270096Strasz#include <sys/cdefs.h> 33270096Strasz__FBSDID("$FreeBSD: releng/11.0/sys/libkern/strndup.c 270096 2014-08-17 09:44:42Z trasz $"); 34270096Strasz 35270096Strasz#include <sys/param.h> 36270096Strasz#include <sys/kernel.h> 37270096Strasz#include <sys/libkern.h> 38270096Strasz#include <sys/malloc.h> 39270096Strasz 40270096Straszchar * 41270096Straszstrndup(const char *string, size_t maxlen, struct malloc_type *type) 42270096Strasz{ 43270096Strasz size_t len; 44270096Strasz char *copy; 45270096Strasz 46270096Strasz len = strnlen(string, maxlen) + 1; 47270096Strasz copy = malloc(len, type, M_WAITOK); 48270096Strasz bcopy(string, copy, len); 49270096Strasz copy[len - 1] = '\0'; 50270096Strasz return (copy); 51270096Strasz} 52