svn commit: r343124 - head/stand/libsa
Toomas Soome
tsoome at FreeBSD.org
Thu Jan 17 22:00:03 UTC 2019
Author: tsoome
Date: Thu Jan 17 22:00:02 2019
New Revision: 343124
URL: https://svnweb.freebsd.org/changeset/base/343124
Log:
libsa: add asprintf()
asprintf() is a nice tool for string processing.
MFC after: 2 weeks
Modified:
head/stand/libsa/printf.c
head/stand/libsa/stand.h
Modified: head/stand/libsa/printf.c
==============================================================================
--- head/stand/libsa/printf.c Thu Jan 17 21:52:41 2019 (r343123)
+++ head/stand/libsa/printf.c Thu Jan 17 22:00:02 2019 (r343124)
@@ -122,6 +122,34 @@ snprint_func(int ch, void *arg)
}
int
+asprintf(char **buf, const char *cfmt, ...)
+{
+ int retval;
+ struct print_buf arg;
+ va_list ap;
+
+ *buf = NULL;
+ va_start(ap, cfmt);
+ retval = kvprintf(cfmt, NULL, NULL, 10, ap);
+ va_end(ap);
+ if (retval <= 0)
+ return (-1);
+
+ arg.size = retval + 1;
+ arg.buf = *buf = malloc(arg.size);
+ if (*buf == NULL)
+ return (-1);
+
+ va_start(ap, cfmt);
+ retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
+ va_end(ap);
+
+ if (arg.size >= 1)
+ *(arg.buf)++ = 0;
+ return (retval);
+}
+
+int
snprintf(char *buf, size_t size, const char *cfmt, ...)
{
int retval;
Modified: head/stand/libsa/stand.h
==============================================================================
--- head/stand/libsa/stand.h Thu Jan 17 21:52:41 2019 (r343123)
+++ head/stand/libsa/stand.h Thu Jan 17 22:00:02 2019 (r343124)
@@ -268,6 +268,7 @@ extern void *reallocf(void *ptr, size_t size);
extern void mallocstats(void);
extern int printf(const char *fmt, ...) __printflike(1, 2);
+extern int asprintf(char **buf, const char *cfmt, ...) __printflike(2, 3);
extern int sprintf(char *buf, const char *cfmt, ...) __printflike(2, 3);
extern int snprintf(char *buf, size_t size, const char *cfmt, ...) __printflike(3, 4);
extern int vprintf(const char *fmt, __va_list);
More information about the svn-src-head
mailing list