Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 29 Apr 2017 21:48:12 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r317598 - head/usr.bin/printf
Message-ID:  <201704292148.v3TLmCmm070707@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Sat Apr 29 21:48:11 2017
New Revision: 317598
URL: https://svnweb.freebsd.org/changeset/base/317598

Log:
  printf: Output formatted data directly, instead of via asprintf.
  
  Long ago, sh used to have its own optimized and restricted string formatting
  implementation, which the printf builtin had to bypass via asprintf() to a
  temporary buffer. Since sh has used libc's string formatting implementation
  for a long time, remove the workaround.
  
  Add a check to keep  printf %c ''  working the same way (output nothing);
  POSIX allows both outputting nothing and outputting a NUL byte.
  
  Also, this change avoids silently discarding format directives for whose
  output asprintf() cannot allocate memory.

Modified:
  head/usr.bin/printf/printf.c

Modified: head/usr.bin/printf/printf.c
==============================================================================
--- head/usr.bin/printf/printf.c	Sat Apr 29 19:20:50 2017	(r317597)
+++ head/usr.bin/printf/printf.c	Sat Apr 29 21:48:11 2017	(r317598)
@@ -70,20 +70,15 @@ static const char rcsid[] =
 #endif
 
 #define	PF(f, func) do {						\
-	char *b = NULL;							\
 	if (havewidth)							\
 		if (haveprec)						\
-			(void)asprintf(&b, f, fieldwidth, precision, func); \
+			(void)printf(f, fieldwidth, precision, func);	\
 		else							\
-			(void)asprintf(&b, f, fieldwidth, func);	\
+			(void)printf(f, fieldwidth, func);		\
 	else if (haveprec)						\
-		(void)asprintf(&b, f, precision, func);			\
+		(void)printf(f, precision, func);			\
 	else								\
-		(void)asprintf(&b, f, func);				\
-	if (b) {							\
-		(void)fputs(b, stdout);					\
-		free(b);						\
-	}								\
+		(void)printf(f, func);					\
 } while (0)
 
 static int	 asciicode(void);
@@ -394,7 +389,8 @@ printf_doformat(char *fmt, int *rval)
 		char p;
 
 		p = getchr();
-		PF(start, p);
+		if (p != '\0')
+			PF(start, p);
 		break;
 	}
 	case 's': {



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