return value of fprintf

Křištof Želechovski giecrilj at
Tue Jan 23 10:29:27 UTC 2007

Oh I see.  
Indeed, the documentation was fixed to explain this in FreeBSD 5.2 
- I must have overlooked that.  
My documentation is apparently taken from FreeBSD 4.4.  
Error return value is not documented here.
I have run the code in a loop and the return value is periodically -1.  
Interestingly enough, 
although the stream error indicator is persistent, 
it is not taken into account to prevent data corruption as in C++ streams.  
The reason may be that POSIX streams must set errno on failure 
(this is *not* required by the ANSI standard)
but there is no errno value to indicate that the error condition is set 
(except EBADF - 
but the POSIX standard should be modified to allow this extension, 
since at present it only means "Bad file descriptor").
Thanks for your time

-----Original Message-----
From: Oliver Fromme [mailto:olli at] 
Sent: Tuesday, January 23, 2007 8:59 AM
To: freebsd-standards at FreeBSD.ORG; giecrilj at
Subject: Re: return value of fprintf

K?i?tof ?elechovski <giecrilj at> wrote:
 > The
 > h=FreeBSD+7-current&format=html> fprintf manual page says it is ANSI C
 > compatible and it returns the number of characters printed.
 > The latter is correct: I get printf("abcd") == 4 even under >/dev/full.
 > (uname is Interix on x86 ver. 3.5)

Because the printf() function doesn't write to /dev/full
directly.  It writes to a stdio buffer associated with
the standard output stream.

 > However, the former is not: according to the ANSI C standard
 > <> ,
 > printf should return a negative value in this case.

POSIX/SUSv4 (which is aligned with the ISO C standard)
says:  "If an output error was encountered, these
functions shall return a negative value."

FreeBSD is compliant with that specification.  Note the
use of the words "if ... encountered":  If the file
system is full, that condition is encountered _only_
the next time the stdio library flushes its buffer
(which can be as late as the close() call).  It is
_not_ encountered by the fprintf() function itself,
so it is perferctly legal that it does not return a
negative value.

Best regards

Oliver Fromme,  secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing
Dienstleistungen mit Schwerpunkt FreeBSD:
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

"Emacs ist für mich kein Editor. Für mich ist das genau das gleiche, als
wenn ich nach einem Fahrrad (für die Sonntagbrötchen) frage und einen
pangalaktischen Raumkreuzer mit 10 km Gesamtlänge bekomme. Ich weiß nicht,
was ich damit soll." -- Frank Klemm, de.comp.os.unix.discussion

More information about the freebsd-standards mailing list