git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: open(2) vs fopen(3)

From
Junio C Hamano <junkio@cox.net>
Date
Sep 14, 2006, 16:37 UTC
Message-ID
<7vr6ye4d64.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0609140835080.4388@g5.osdl.org>
Linus Torvalds <torvalds@osdl.org> writes:
Show 19 quoted lines
> On Thu, 14 Sep 2006, moreau francis wrote:
>> 
>> I'm reading git source code and falling on this stupid question:
>> Why sometime open(2) is used and other time fopen(3) is
>> prefered. I'm sorry for this dump question but I have no clue.
>
> fopen() tends to result in easier usage, especially if the file in 
> question is a line-based ASCII file, and you can just use "fgets()" to 
> read it. So fopen is the simple alternative for simple problems.
>
> Using a direct open() means that you have to use the low-level IO 
> functions (I'm ignoring the use of "fdopen()"), but if done right, it has 
> a number of advantages:
>...
>  - error conditions are a lot more obvious and repeatable with the 
>    low-level things, at least so I find personally. Error handling with 
>    stdio routines is _possible_, but probably because almost nobody ever 
>    does it, it's not something that people are conditioned to do, so it 
>    ends up beign "strange".

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.

Technically it was a bug on our part not Solaris, but that was something unexpected to see.

Previous: Linus TorvaldsNext: Linus Torvalds
Message 4 of 5 in “open(2) vs fopen(3)”
  1. moreau francisSep 14, 2006
  2. Andy WhitcroftSep 14, 2006
  3. Linus TorvaldsSep 14, 2006
  4. Junio C HamanoSep 14, 2006
  5. Linus TorvaldsSep 14, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.