* Flush on main exit?
@ 2019-10-18 21:13 Tim Lewis
2019-10-19 1:49 ` [edk2-devel] " Doran, Mark
0 siblings, 1 reply; 2+ messages in thread
From: Tim Lewis @ 2019-10-18 21:13 UTC (permalink / raw)
To: devel
[-- Attachment #1: Type: text/plain, Size: 1176 bytes --]
I have noticed recently, when porting BSD applications, that if main exits
normally, the buffers are not flushed. This is most obvious when using
StdLib along with printf or fprintf to stdout.
Has anyone else noticed this? If there is a \n in the output, it gets
flushed to stdout, but if the string does not contain a \n then often
nothing happens. This is most obvious with 1-line help or logo strings that
never show up. Of course, most BSD apps use stderr for their usage, but even
this doesn't go anywhere
static void
usage(void)
{
(void)fprintf(stderr, "usage: which [-as] program ...\n");
exit(EXIT_FAILURE);
}
Per the posix standard:
OpenGroup says:
https://pubs.opengroup.org/onlinepubs/009695399/functions/exit.html
The exit() function shall then flush all open streams with unwritten
buffered data, close all open streams, and remove all files created by
<https://pubs.opengroup.org/onlinepubs/009695399/functions/tmpfile.html>
tmpfile(). Finally, control shall be terminated with the consequences
described below.
I have seen similar behavior with CURL and printf.
Any thoughts here?
Tim
[-- Attachment #2: Type: text/html, Size: 4360 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [edk2-devel] Flush on main exit?
2019-10-18 21:13 Flush on main exit? Tim Lewis
@ 2019-10-19 1:49 ` Doran, Mark
0 siblings, 0 replies; 2+ messages in thread
From: Doran, Mark @ 2019-10-19 1:49 UTC (permalink / raw)
To: devel@edk2.groups.io, tim.lewis@insyde.com
[-- Attachment #1: Type: text/plain, Size: 2327 bytes --]
I think the ISO C standard is actually the governing spec for exit() now. However, it more or less says the same thing as POSIX.1.
I actually recall some of the early input to POSIX discussions about exit() and flushing behavior (fflush() got lumped in to the same conversation). It was somewhat of a hot topic with the X/Open work group especially because ICL mainframes were "virtual" enough that the meaning of flushing was somewhat in question (for files at any rate) when there weren't actual disks to receive the flush.
In any case the C standard now seems quite clear to my eye: you ought to get your message on standard out or error regardless of newline or not. That should apply as I read the spec whether you have code that does an explicit exit() from main(), or a return or even if execution just reaches the closing brace on the main() function. Not knowing anything else, I'd say you are seeing a bug in the implementation.
--
Cheers,
Mark.
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Tim Lewis
Sent: Friday, October 18, 2019 2:13 PM
To: devel@edk2.groups.io
Subject: [edk2-devel] Flush on main exit?
I have noticed recently, when porting BSD applications, that if main exits normally, the buffers are not flushed. This is most obvious when using StdLib along with printf or fprintf to stdout.
Has anyone else noticed this? If there is a \n in the output, it gets flushed to stdout, but if the string does not contain a \n then often nothing happens. This is most obvious with 1-line help or logo strings that never show up. Of course, most BSD apps use stderr for their usage, but even this doesn't go anywhere
static void
usage(void)
{
(void)fprintf(stderr, "usage: which [-as] program ...\n");
exit(EXIT_FAILURE);
}
Per the posix standard:
OpenGroup says:
https://pubs.opengroup.org/onlinepubs/009695399/functions/exit.html
The exit() function shall then flush all open streams with unwritten buffered data, close all open streams, and remove all files created by tmpfile()<https://pubs.opengroup.org/onlinepubs/009695399/functions/tmpfile.html>. Finally, control shall be terminated with the consequences described below.
I have seen similar behavior with CURL and printf.
Any thoughts here?
Tim
[-- Attachment #2: Type: text/html, Size: 8088 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-10-19 1:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-10-18 21:13 Flush on main exit? Tim Lewis
2019-10-19 1:49 ` [edk2-devel] " Doran, Mark
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox