Subject: Re: standards/32981: man page is wrong on return values of printf
To: None <standards-manager@netbsd.org, gnats-admin@netbsd.org,>
From: Johan Veenhuizen <veenhuizen@users.sourceforge.net>
List: netbsd-bugs
Date: 03/05/2006 15:00:06
The following reply was made to PR standards/32981; it has been noted by GNATS.

From: Johan Veenhuizen <veenhuizen@users.sourceforge.net>
To: standards-manager@netbsd.org, gnats-bugs@netbsd.org
Cc: 
Subject: Re: standards/32981: man page is wrong on return values of printf
Date: Sun, 05 Mar 2006 15:55:15 +0100

 Johan Veenhuizen wrote:
 > I have no copies of the C90/C99 standards in front of me,
 > but "The C Programming Language, 2nd Ed.", POSIX and SUS
 > document different return values for fprintf/sprintf/etc
 > than the NetBSD manual page.
 >
 > I have no clue about asprintf and vasprintf.  Maybe their
 > return values should be changed as well, but they are not
 > standardised yet as far as I know.
 >
 > ...
 >
 > --- printf.3	20 Jul 2005 13:31:15 -0000	1.40
 > +++ printf.3	3 Mar 2006 21:38:40 -0000
 > @@ -667,7 +667,7 @@
 >  and
 >  .Fn vfprintf
 >  return the number of characters printed.
 > -Otherwise \-1 is returned and
 > +Otherwise a negative value is returned and
 >  .Dv errno
 >  is set to indicate the error.
 >  .Pp
 >
 > ...
 
 Just to make things clear.  I didn't mean that the actual return
 values should change.  What I meant was that the *documentation*
 is too detailed about the return values.  All the standards say
 that "a negative value" should be returned, so the correct way of
 checking the success of printf is to write the following:
 
 	if (printf("...", ...) < 0)
 		err(1, "stdout");
 
 The NetBSD manual page for printf does not document this standard,
 but instead documents the *implementation* of printf in its C
 library.  It says that -1 is returned on errors, suggesting this
 value is the way to test if the operation succeeded:
 
 	if (printf("...", ...) == -1)
 		err(1, "stdout");
 
 The problem is that this is not guaranteed to work on other systems
 than NetBSD.
 
 There is no information lost by modifying the manual page to say
 "a negative value" instead of "-1".  This is the correct use of
 printf and should be encouraged by the manual.
 
 As a side note.  I have noticed that it is not uncommon for NetBSD
 commands to simply ignore output errors and pretend as if nothing
 bad had happened.  There are lots of "(void)printf(...)" in the
 code but very often no calls to ferror()/fflush() to verify that
 the writes succeeded.  My best example of this is find(1), whose
 *purpose* is to write path names to standard output.  Consider this
 example:
 
 	$ mount /dev/fd0a mnt
 	$ find /usr >mnt/file_list
 
 What if the floppy gets filled up?  find(1) won't tell you.  Is
 this behaviour considered "traditional" and the "BSD way"?  I have
 a patch for find(1) that fixes this, and I would be happy to fix
 other commands in similar ways if people would appreciate this.
 
 Regards,
 Johan