Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!tektronix!sequent!bills From: bills@sequent.UUCP (Bill Sears) Newsgroups: comp.lang.c Subject: Re: File descriptors and streams and copying thereof. Message-ID: <15039@sequent.UUCP> Date: 25 Apr 89 02:31:34 GMT Reply-To: bills@sequent.UUCP (Bill Sears) Followup-To: comp.lang.c Organization: Sequent Computer Systems, Inc Lines: 23 In article <8450@xanth.cs.odu.edu> kremer@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@root44.co.uk> gwc@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.