File descriptors and streams and copying thereof.

Bill Sears bills at sequent.UUCP
Tue Apr 25 12:31:34 AEST 1989


In article <8450 at xanth.cs.odu.edu> kremer at cs.odu.edu (Lloyd Kremer) writes:
>	close(0);
>
>	if(open("my_filename", O_RDONLY) == -1)
>	/* attempt to open your file.  If open succeeds, it will return
>		file descriptor 0, since it always returns the earliest
>		one, and we just closed 0 */

And in article <731 at root44.co.uk> gwc at root.co.uk (Geoff Clare) writes:
>It is, of course, safe to assume the stream returned by fopen() and
>friends has the lowest available file descriptor (i.e. fileno(stream)),
>...

It is NOT safe to assume that the file descriptor returned either
by open(2) or the fopen(3) family will be the lowest available file
descriptor.  Although this may be true on many machines, it is not
guaranteed to be the case (I have worked with a machine where this
did not always work).  Nowhere in the manuals that I have seen (v7,
SYS V.2, BSD4.2, BSD4.3) does the open(2) system call or the fopen(3)
library function guarantee that the file descriptor returned will
be the lowest one available, only the dup(2) system call makes that
guarantee.



More information about the Comp.lang.c mailing list