Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 31 Jan 2019 17:07:00 +0000 (UTC)
From:      Toomas Soome <tsoome@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r343615 - stable/12/stand/libsa
Message-ID:  <201901311707.x0VH70jX007831@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: tsoome
Date: Thu Jan 31 17:06:59 2019
New Revision: 343615
URL: https://svnweb.freebsd.org/changeset/base/343615

Log:
  MFC r343124:
  
  libsa: add asprintf()
  
  asprintf() is a nice tool for string processing.

Modified:
  stable/12/stand/libsa/printf.c
  stable/12/stand/libsa/stand.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/stand/libsa/printf.c
==============================================================================
--- stable/12/stand/libsa/printf.c	Thu Jan 31 17:04:55 2019	(r343614)
+++ stable/12/stand/libsa/printf.c	Thu Jan 31 17:06:59 2019	(r343615)
@@ -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: stable/12/stand/libsa/stand.h
==============================================================================
--- stable/12/stand/libsa/stand.h	Thu Jan 31 17:04:55 2019	(r343614)
+++ stable/12/stand/libsa/stand.h	Thu Jan 31 17:06:59 2019	(r343615)
@@ -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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201901311707.x0VH70jX007831>