kern/lib/c/string/strndup.c
fef 5a5135f416
update license terms
As of now, everything except the code imported
from FreeBSD is proprietary.  Of course, it won't
be like this for long, only until we have decided
which license we like to use.  The rationale is
that releasing everything under a copyleft license
later is always easier than doing so immediately
and then changing it afterwards.
Naturally, any changes made before this commit are
still subject to the terms of the CNPL.
2021-11-15 19:23:22 +01:00

41 lines
1.2 KiB
C

/* See the end of this file for copyright and license terms. */
#include <gay/types.h>
#include <stdlib.h>
#include <string.h>
char *strndup(const char *str, usize maxlen)
{
char *copy;
usize len;
len = strnlen(str, maxlen);
copy = malloc(len + 1);
if (copy != NULL) {
memcpy(copy, str, len);
copy[len] = '\0';
}
return copy;
}
/*
* Copyright (c) 2021 fef <owo@fef.moe>
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
*
* 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.
*
* 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.
*
* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $
* $FreeBSD$
*/