Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Add a simple vasprintf() implementation that uses 2 pass...



details:   https://anonhg.NetBSD.org/src/rev/ca21f11c6218
branches:  trunk
changeset: 451394:ca21f11c6218
user:      christos <christos%NetBSD.org@localhost>
date:      Mon May 20 20:35:45 2019 +0000

description:
Add a simple vasprintf() implementation that uses 2 passes, one to compute
the length and a second to place the data. Requested by rmind@

diffstat:

 sys/kern/subr_prf.c |  16 ++++++++++++++--
 sys/sys/systm.h     |   4 +++-
 2 files changed, 17 insertions(+), 3 deletions(-)

diffs (55 lines):

diff -r aac594de3a79 -r ca21f11c6218 sys/kern/subr_prf.c
--- a/sys/kern/subr_prf.c       Mon May 20 20:25:09 2019 +0000
+++ b/sys/kern/subr_prf.c       Mon May 20 20:35:45 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_prf.c,v 1.176 2019/01/14 19:21:54 jdolecek Exp $  */
+/*     $NetBSD: subr_prf.c,v 1.177 2019/05/20 20:35:45 christos Exp $  */
 
 /*-
  * Copyright (c) 1986, 1988, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.176 2019/01/14 19:21:54 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.177 2019/05/20 20:35:45 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -1181,6 +1181,18 @@
        return retval;
 }
 
+int
+vasprintf(char **bf, const char *fmt, va_list ap)
+{
+       int retval;
+       va_list cap;
+
+       va_copy(cap, ap);
+       retval = kprintf(fmt, TOBUFONLY, NULL, NULL, ap) + 1;
+       *bf = kmem_alloc(retval, KM_SLEEP);
+       return vsnprintf(*bf, retval, fmt, cap);
+}
+
 /*
  * kprintf: scaled down version of printf(3).
  *
diff -r aac594de3a79 -r ca21f11c6218 sys/sys/systm.h
--- a/sys/sys/systm.h   Mon May 20 20:25:09 2019 +0000
+++ b/sys/sys/systm.h   Mon May 20 20:35:45 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: systm.h,v 1.284 2019/05/04 10:07:11 maxv Exp $ */
+/*     $NetBSD: systm.h,v 1.285 2019/05/20 20:35:45 christos Exp $     */
 
 /*-
  * Copyright (c) 1982, 1988, 1991, 1993
@@ -236,6 +236,8 @@
 
 int    snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
 
+int    vasprintf(char **, const char *, va_list) __printflike(2, 0);
+
 void   vprintf(const char *, va_list) __printflike(1, 0);
 
 int    vsnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);



Home | Main Index | Thread Index | Old Index