[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fileno(tmpfile()) returns EBADF

From: Marcus Brinkmann
Subject: Re: fileno(tmpfile()) returns EBADF
Date: Sat, 20 Apr 2002 06:01:46 +0200
User-agent: Mutt/1.3.28i

On Fri, Apr 19, 2002 at 04:51:35PM -0700, Thomas Bushnell, BSG wrote:
> Marcus Brinkmann <marcus@gnu.org> writes:
> > I don't have my copy of POSIX around, but I also remember a vague
> > requirement (or expectation) that the file descriptor allocated
> > is always the smallest file descriptor available.  
> No, that's no requirement.  It's the way Unix historically worked, but
> Posix has no such requirement at all.

Uh, I have now rebooted and the standard (the draft7 from the Austin
group) in front of me.  Let's look at the details:

About open():
"The open( ) function shall return a file descriptor for the named file that
is the lowest file descriptor not currently open for that process."

About fopen():
"The fopen( ) function shall allocate a file descriptor as open( ) does."

Roland already quoted the part about tmpfile():
"The file is opened as fopen( ) for update (w+)."

And, just to complete the picture, here is what it says about F_DUPFD:
"Return a new file descriptor which shall be the lowest numbered available
(that is, not already open) file descriptor greater than or equal to the third
argument, arg, taken as an integer of type int."

And about pipe():
"Their integer values shall be the two lowest available at the time of the
pipe( ) call."

And I can provide similar quotes for posix_openpt, posix_typed_mem_open, and
shm_open.  That seems to be a very strong requirement throughout.  Of course
all that matters here is that tmpfile is part of the set of functions with
this requirement.

POSIX even suggests the following.  Look carefully at the first example:
"Reassigning a File Descriptor
 The following example closes the file descriptor associated with standard
 output for the current process, re-assigns standard output to a new file
 descriptor, and closes the original file descriptor to clean up. This example
 assumes that the file descriptor 0 (which is the descriptor for standard input)
 is not closed.
#include <unistd.h>
int pfd;
Incidentally, this is exactly what could be achieved using:
dup2(pfd, 1);


`Rhubarb is no Egyptian god.' Debian http://www.debian.org brinkmd@debian.org
Marcus Brinkmann              GNU    http://www.gnu.org    marcus@gnu.org

reply via email to

[Prev in Thread] Current Thread [Next in Thread]