Subject: Re: Printf builtin missing v flag support
From: "Jun T." <[email-masked]>
To: "[email-masked]" <[email-masked]>
Date: Mon, 4 Jan 2016 14:50:48 +0900
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Mac OS X (where open_memstream() does not exist), B03print.ztst fails
as follows.

*** 1,2 ****
  once more
! into-the-breach-
--- 1,2 ----
  once more
! 
Test ./B03print.ztst failed: output differs from expected as shown above for:
 unset foo
 print -v foo once more
 print -r -- $foo
 printf -v foo "%s-" into the breach
 print -r -- $foo
Error output:
(eval):printf:4: i/o error: inappropriate ioctl for device
Was testing: print and printf into a variable

A patch is attached below.
fread() returns the number of elements (items) successfully read.
rcount++ for the trailing NULL.

diff --git a/Src/builtin.c b/Src/builtin.c
index 04d8f11..03fefa6 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4879,10 +4879,10 @@ bin_print(char *name, char **args, Options ops, int func)
 #else
 	rewind(fout);
 	buf = (char *)zalloc(count + 1);
-	rcount = fread(buf, count, 1, fout);
+	rcount = fread(buf, 1, count, fout);
 	if (rcount < count)
 	    zwarnnam(name, "i/o error: %e", errno);
-	buf[rcount] = '\0';
+	buf[rcount++] = '\0';
 #endif
 	queue_signals();
 	stringval = metafy(buf, rcount - 1, META_REALLOC);