From: Linus Torvalds <torvalds@osdl.org>
To: Junio C Hamano <junkio@cox.net>
Cc: moreau francis <francis_moreau2000@yahoo.fr>, git@vger.kernel.org
Subject: Re: open(2) vs fopen(3)
Date: Thu, 14 Sep 2006 10:31:27 -0700 (PDT) [thread overview]
Message-ID: <Pine.LNX.4.64.0609141023130.4388@g5.osdl.org> (raw)
In-Reply-To: <7vr6ye4d64.fsf@assigned-by-dhcp.cox.net>
On Thu, 14 Sep 2006, Junio C Hamano wrote:
>
> Another issue related with this is that stdio implementations
> tend to have unintuitive interaction with signals, one fine
> example of it being the problem we fixed with commit fb7a653,
> where on Solaris fgets(3) did not restart the underlying read(2)
> upon SIGALRM.
Yeah. However, I think it's worth just posting the code in question to
explain _why_ error handling with stdio sucks so badly, and why nobody
does it..
Here's the snippet:
if (!fgets(line, sizeof(line), stdin)) {
if (feof(stdin))
break;
if (!ferror(stdin))
die("fgets returned NULL, not EOF, not error!");
if (errno != EINTR)
die("fgets: %s", strerror(errno));
clearerr(stdin);
so with the <stdio.h> functions, you have to check FOUR DIFFERENT THINGS
(1: return value, 2: feof() value, 3: ferror() value, and 4: errno) to get
things right, and to add insult to injury, you then have to do an explicit
clear.
In other words, the fundamental reason nobody bothers checking errors with
stdio is that stdio just makes it a damn pain in the ass to do so - by
having a million different thing you have to do (and ordering actually
matters).
In contrast, the <unistd.h> interfaces are a paragon of clarity: you check
just two things - the return value, and possibly "errno".
Now, <unistd.h> isn't perfect either, and in the kernel we have simplified
things further, by getting rid of "errno", and just having the return
value contain errno too. Making things not only trivially thread-safe, but
also actually easier to code and understand, because you don't have
anything to be confused about: the return value is always the only thing
you need to look at in order to know what went wrong.
But unistd.h sure is a lot better than stdio in this area. Of course,
stdio.h is just a lot easier to use when you don't actually care about the
errors, which is also partly the _reason_ why caring about errors is so
hard (the whole separate clearerr() and ferror() interfaces exist exactly
_because_ people don't care about errors in many cases, and you're
supposed to maybe have some way to test at the end whether an error
happened or not).
So stdio.h is pretty much geared towards delayed error handling, which in
practice ends up often meaning "no error handling at all".
Linus
prev parent reply other threads:[~2006-09-14 17:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-14 9:15 open(2) vs fopen(3) moreau francis
2006-09-14 10:52 ` Andy Whitcroft
2006-09-14 15:46 ` Linus Torvalds
2006-09-14 16:37 ` Junio C Hamano
2006-09-14 17:31 ` Linus Torvalds [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.64.0609141023130.4388@g5.osdl.org \
--to=torvalds@osdl.org \
--cc=francis_moreau2000@yahoo.fr \
--cc=git@vger.kernel.org \
--cc=junkio@cox.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).