Path: utzoo!utgpu!attcan!uunet!steinmetz!control!dixon From: dixon@control.steinmetz (walt dixon) Newsgroups: comp.sys.ibm.pc Subject: Re: How to determine stdin/out redirection Message-ID: <12265@steinmetz.ge.com> Date: 29 Sep 88 16:32:43 GMT References: <9465@cup.portal.com> <3999@tekgvs.GVS.TEK.COM> Sender: news@steinmetz.ge.com Reply-To: dixon@control.steinmetz.ge.com (walt dixon) Organization: General Electric CRD, Schenectady, NY Lines: 41 There have been a number of proposed solutions to determining whether StdIn and/or StdOut have been redirected. All these solutions rely in one way or another on device attributes. MS-DOS records device attributes in a data structure called the system file table (SFT) when it opens the file/device. Some attributes come directly from the attributes word of the device (IsStdIn, IsStdOut, IsCurClk, etc); others are maintained by DOS (eof, binary mode). Looking at device attributes is not a fool proof way of determining redirection. One could redirect StdOut to a printer. There are a couple of other data structures which help solve this problem. Every DOS program has a Program Segment Prefix (PSP). The PSP contains the address of a Job File Table (JFT). The JFT is normally contained in the PSP, but DOS 3.3 and above allow one to move the JFT. The JFT is an array of bytes indexed by file handle. Each JFT contains a one byte System File Number or SFN or a 0xff if the handle is not used. The SFN is in turn an index into the System File Table. Most C run time environments use handles for file access. Using an IOB or similar structure, the C run time can translate a file descriptor into a DOS handle. (For files that are opened by FCB, one of the undocumented FCB fields contains the SFN). DOS opens the console device in the boot process. SFN=0 corresponds to the console device. Command.com reopens the console device for StdIn and StdOut. The JFT entries corresponding to StdIn and StdOut (JFT[0]] and JFT[1] respectively) will contain a 0 in the absence of redirection. By looking within the SFT entry, one can actually find the name of the device/file. Unfortunately, any path name information is lost after the file is opened. Much of this information comes from disassebling DOS. I've described these data structures in Chapters 10 and 11 of "The MS-DOS Papers" (Howard Sams, 1988) and Chapter 4 of the 2nd edition of "The MS-DOS Developer's Guide (Howard Sams, late '88?). NB I get no royalties; just citing a good reference. I hope this helps. Standard disclaimers apply. Walt Dixon {ARPA: Dixon@GE-CRD.ARPA } {US Mail: GE Corp R&D } { PO Box 8 } { Schenectady, NY 12345 } {Phone: 518-387-5798 }