Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!oliveb!pyramid!prls!philabs!micomvax!musocs!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP Newsgroups: comp.unix.wizards Subject: Re: /dev/stdin for 4.3? Message-ID: <792@mcgill-vision.UUCP> Date: Thu, 4-Jun-87 18:54:28 EDT Article-I.D.: mcgill-v.792 Posted: Thu Jun 4 18:54:28 1987 Date-Received: Sat, 13-Jun-87 11:24:59 EDT References: <7359@brl-adm.ARPA> <5856@brl-smoke.ARPA> <15318@onfcanim.UUCP> Organization: McGill University, Montreal Lines: 76 In article <15318@onfcanim.UUCP>, dave@onfcanim.UUCP (Dave Martindale) writes: > 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. Is this a SysV thing? I was not aware of any way to make a device which could be treated like a directory like that (/dev/fd/0). > 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. Here's how I did it: % ls -l /dev/std* crw-rw-rw- 1 mouse 64, 2 Mar 23 05:31 /dev/stderr crw-rw-rw- 1 mouse 64, 0 Mar 23 05:31 /dev/stdin crw-rw-rw- 1 mouse 64, 1 Mar 23 05:31 /dev/stdout % machine/conf.c entry: stddevopen, nodev, nodev, nodev, /* 64 */ nodev, nulldev, nulldev, 0, nodev, nodev, (notice, only the "open" slot has anything in it). stddevopen(dev,mode) dev_t dev; int mode; { int fd = minor(dev); int rfd = u.u_r.r_val1; if ((fd < 0) || (fd >= NOFILE)) { return(EBADF); } if (u.u_ofile[fd] == NULL) { return(ENXIO); } if ((mode&FREAD) && !(u.u_ofile[fd]->f_flag&FREAD)) { return(EACCES); } if ((mode&FWRITE) && !(u.u_ofile[fd]->f_flag&FWRITE)) { return(EACCES); } u.u_ofile[rfd]->f_count --; u.u_ofile[rfd] = u.u_ofile[fd]; u.u_pofile[rfd] = u.u_pofile[fd] & ~UF_EXCLOSE; u.u_ofile[fd]->f_count ++; return(0); } So there. (stddevopen() is, functionally, just another interface to dup().) Another use for /dev/std*: in a csh script, how do you generate a fixed message on the script's standard error? sh -c 'echo this is the message 1>&2' Ugly. Unreadable by someone who doesn't know sh syntax. Forks an unnecessary sh process. echo this is the message > /dev/stderr Much better. (Yes, I know sh is better for most scripts.) der Mouse (mouse@mcgill-vision.uucp)