Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!watcgl!onfcanim!dave From: dave@onfcanim.UUCP Newsgroups: comp.unix.wizards Subject: Re: /dev/stdin for 4.3? Message-ID: <15318@onfcanim.UUCP> Date: Sun, 17-May-87 23:05:23 EDT Article-I.D.: onfcanim.15318 Posted: Sun May 17 23:05:23 1987 Date-Received: Wed, 27-May-87 02:49:50 EDT References: <7359@brl-adm.ARPA> <5856@brl-smoke.ARPA> Reply-To: dave@onfcanim.UUCP (Dave Martindale) Organization: National Film Board / Office national du film, Montreal Lines: 41 In article <5856@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: > [ discussing how to implement /dev/fd*, /dev/std{in,out,err} ] >This is almost trivial: > 1) Implement /dev/fd instead (general file descriptor device), > using minor device to select the fd #. Make /dev/stdin > a link to /dev/fd/0, similarly for /dev/stdout etc. > 2) All you need is the open() entry, which should simply snarf > up the process's corresponding file table index, just > like a dup() except you don't allocate anything. > 3) read(), write(), select(), close(), etc. don't even have > to know how you got the fd open. (You'll be using the > entries associated with the original non-/dev/fd open, > so there need be no entries for these in the /dev/fd > driver.) I believe it's better to do the equivalent of a full dup(), returning a newly-opened descriptor. You still need only an open() entry, but you have to alloc an ofile table slot and increment the reference count on the file struct. If you just pass back the old already-open descriptor, you have two "names" for the same "object", though the program thinks they are entirely distinct. Closing the unit via one name causes the other name to also become invalid, which may be rather surprising. For example, consider the program fragment: outfile = fopen(argv[argc-1], "w"); fwrite(stuff, sizeof stuff, 1, outfile); fclose(outfile); printf("status message"); If this is called with an argument of "/dev/stdout" in order to get its output written to standard output, the fclose will actually close unit 1 and the last printf will never appear. Dup()'ing the unit avoids this. Now, you can also argue that a new file struct should be allocated too, exactly the same as would happen if you really open()ed the same file twice. However, I think this less likely to cause trouble, and having the two logically-separate units sharing the same seek pointer may actually be useful sometimes.