2015-08-26 23:28:10 +00:00
|
|
|
/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
|
2008-12-06 09:37:54 +00:00
|
|
|
|
|
|
|
/*
|
2015-08-26 23:28:10 +00:00
|
|
|
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
|
2008-12-06 09:37:54 +00:00
|
|
|
*
|
2015-08-26 23:28:10 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2008-12-06 09:37:54 +00:00
|
|
|
*
|
2015-08-26 23:28:10 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2008-12-06 09:37:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
char *
|
2015-08-26 23:28:10 +00:00
|
|
|
strndup(const char *str, size_t maxlen)
|
2008-12-06 09:37:54 +00:00
|
|
|
{
|
|
|
|
char *copy;
|
2015-08-26 23:28:10 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = strnlen(str, maxlen);
|
|
|
|
copy = malloc(len + 1);
|
|
|
|
if (copy != NULL) {
|
|
|
|
(void)memcpy(copy, str, len);
|
|
|
|
copy[len] = '\0';
|
|
|
|
}
|
2008-12-06 09:37:54 +00:00
|
|
|
|
2015-08-26 23:28:10 +00:00
|
|
|
return copy;
|
2008-12-06 09:37:54 +00:00
|
|
|
}
|