Add a simple vasprintf() implementation that uses 2 passes, one to compute

the length and a second to place the data. Requested by rmind@
This commit is contained in:
christos 2019-05-20 20:35:45 +00:00
parent 120c6c97f3
commit 147918d081
2 changed files with 17 additions and 3 deletions

View File

@ -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 @@ vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
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).
*

View File

@ -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 @@ void printf(const char *, ...) __printflike(1, 2);
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);