add strndup and an alias to strdup.

This commit is contained in:
christos 2018-01-09 01:53:55 +00:00
parent 8ffa3f42a9
commit c4daf5eae5
2 changed files with 22 additions and 3 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr_kmem.c,v 1.65 2017/11/09 23:20:12 riastradh Exp $ */
/* $NetBSD: subr_kmem.c,v 1.66 2018/01/09 01:53:55 christos Exp $ */
/*-
* Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
@ -100,7 +100,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.65 2017/11/09 23:20:12 riastradh Exp $");
__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.66 2018/01/09 01:53:55 christos Exp $");
#ifdef _KERNEL_OPT
#include "opt_kmem.h"
@ -544,6 +544,23 @@ kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
return ptr;
}
char *
kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
{
KASSERT(str != NULL);
KASSERT(maxlen != 0);
size_t len = strnlen(str, maxlen);
char *ptr = kmem_alloc(len + 1, flags);
if (ptr == NULL)
return NULL;
memcpy(ptr, str, len);
ptr[len] = '\0';
return ptr;
}
void
kmem_strfree(char *str)
{

View File

@ -1,4 +1,4 @@
/* $NetBSD: kmem.h,v 1.10 2017/11/07 18:35:57 christos Exp $ */
/* $NetBSD: kmem.h,v 1.11 2018/01/09 01:53:55 christos Exp $ */
/*-
* Copyright (c)2006 YAMAMOTO Takashi,
@ -47,6 +47,8 @@ void kmem_intr_free(void *, size_t);
char * kmem_asprintf(const char *, ...) __printflike(1, 2);
char * kmem_strdupsize(const char *, size_t *, km_flag_t);
#define kmem_strdup(s, f) kmem_strdupsize((s), NULL, (f))
char * kmem_strndup(const char *, size_t, km_flag_t);
void kmem_strfree(char *);
/*