Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!crdgw1!uunet!munnari.oz.au!comp.vuw.ac.nz!canterbury!phys169 From: phys169@csc.canterbury.ac.nz Newsgroups: comp.lang.pascal Subject: Re: Text Files, I/O Redirection Message-ID: <1991May23.111106.819@csc.canterbury.ac.nz> Date: 22 May 91 23:11:06 GMT References: <45670017@hpcupt3.cup.hp.com> Organization: University of Canterbury, Christchurch, New Zealand Lines: 115 In article <45670017@hpcupt3.cup.hp.com>, defaria@hpcupt3.cup.hp.com (Andy DeFaria) writes: > I have been having three problems lately in TP: > > 1) How do you perform I/O redirection using Exec? I remember this > being discussed and had thought that my home-brewed shell did > this properly then found out it doesn't and the posting has > since timed out. I thought that I needed to Exec Command.Com > and specify the ">" or "|" options in the Options parameter but > this not working. > The basic answer is to the the handle duplication DOS calls; copy the current handle, open a file for redirection, and force a copy of its handle to be put on handle 1 (for output) or 0 (for stdin), etc. When you've finished, force the old one back again and close the temporary copy. An example of all this is at the end of the posting. > 2) How do you get the name of an open text file from AFile : Text > variable? > Note that this gets what name was given by the last assign, so you can't get the name from a handle in general this way (e.g. you can't use it to see the name of where the stdout is going). {make sure you have a "uses DOS" at the start} st:=TextRec(AFile).Name; {uses the pre-defined type TextRec} st:=copy(st,1,pred(pos(#0,st))); {truncate (it is ASCIZ)} writeln('The file AFile is assigned to: ',st); > 3) The FileSize function doesn't work on Text files. How can I > easily and efficiently get the FileSize (in bytes) of a Text > file? > The following inline assembler will do that, except for devices. function TextSize(var TF ) : longint; inline( $5F/ {pop di} $07/ {pop es} $B8/$4202/ {mov ax,4202} $33/$C9/ {xor cx,cx} $8B/$D1/ {mov dx,cx} $26/$8B/$1D/{mov bx,es:[di]} $CD/$21/ {int 21} $50/ {push ax} $52/ {push dx} $B8/$4200/ {mov ax,4200} $33/$C9/ {xor cx,cx} $8B/$D1/ {mov dx,cx} $26/$8B/$1D/{mov bx,es:[di]} $CD/$21/ {int 21} $5A/ {pop dx} $58); {pop ax} Hope this helps, Mark Aitchison, Physics, University of Canterbury, New Zealand, Up Under. --snip-- program TryRedirection; {$M 9000,0,0} uses DOS; const StandardInputHandle = 0; StandardOutputHandle= 1; StandardErrorHandle = 2; StandardPrinterHandle=4; StandardAuxHandle = 3; var RedirectionFile : text; OldHandle : word; reg : registers; function Truncate(st : string) : string; begin Truncate:=copy(st,1,pred(pos(#0,st))); end; function Redirect(HandleToRedirect,NewFileHandle : word) : word; begin with reg do begin AH:=$45; BX:=HandleToRedirect; MsDos(reg); {Duplicate a file handle} Redirect:=AX; {Return the temporary copy of the original handle} AH:=$46; CX:=HandleToRedirect; BX:=NewFileHandle; if BX<>CX then MsDos(reg); {MSDOS might hang if BX=CX!} end; end; procedure UnRedirect(HandleToRedirect,OldFileHandle : word); begin with reg do begin AH:=$46; {force duplicate handle} CX:=HandleToRedirect; BX:=OldFileHandle; if BX<>CX then MsDos(reg); {MSDOS might hang if BX=CX!} AH:=$3E; {close a handle} BX:=OldFileHandle; MsDos(reg); end; end; begin writeln('This goes to the standard output file (screen)'); writeToStandardFile(StandardOutputHandle,' So does this...'^M^J); assign(RedirectionFile,'Test.out'); rewrite(RedirectionFile); writeln('Output will be redirected to: ',truncate(TextRec(RedirectionFile).name)); writeln(RedirectionFile,'This is the file to get the redirected output...'); OldHandle:=Redirect(StandardOutputHandle,TextRec(RedirectionFile).handle); close(RedirectionFile); exec('c:\COMMAND.COM','/C dir try*.*'); UnRedirect(StandardOutputHandle,OldHandle); end.