From c4daf5eae5207fd6831b760cc004be1e46ca5339 Mon Sep 17 00:00:00 2001 From: christos Date: Tue, 9 Jan 2018 01:53:55 +0000 Subject: [PATCH] add strndup and an alias to strdup. --- sys/kern/subr_kmem.c | 21 +++++++++++++++++++-- sys/sys/kmem.h | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/sys/kern/subr_kmem.c b/sys/kern/subr_kmem.c index 17b351402612..a1b35d719624 100644 --- a/sys/kern/subr_kmem.c +++ b/sys/kern/subr_kmem.c @@ -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 -__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) { diff --git a/sys/sys/kmem.h b/sys/sys/kmem.h index 54b617f7c8ea..bf2efb4dc63a 100644 --- a/sys/sys/kmem.h +++ b/sys/sys/kmem.h @@ -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 *); /*