[comp.lang.pascal] How to determine if I/O redire

Kai_Henningsen@ms.maus.de (Kai Henningsen) (05/23/91)

Eric Cabot elmo @ troi.cc.rochester.edu schrieb am Mo 20.05.1991, 13:46
A16525@SUB in Cl-Pascal:

EC>  I want to be able to detect if the user is redirecting standard
EC>input and/or standard output  on the command line. I'd like

There *is* a way, but you have to know your DOS :-)

I'd do this with DOS Function $44 Subfunction $00, IOCTL get device
information. It works as follows:

uses Dos;

var r: Registers;

with r do begin
  ax:=$4400;
  bx:=TextRec(Input).Handle; { or Output or whatever }
  MsDos(r);
  if odd(Flags) then { Error, code is in ax, same values as Turbo I/O errors }
  else { normal case, it worked - result in dx }
  begin
    if dx and $80 = 0 then
      { normal file, dx and $3f = drive (A=0), dx and $40 = 0 if has been
        written }
    else
      { it's a device, like CON, PRN, etc; dx and 1 <>0 if standard input,
        dx and 2 <>0 if standard output, dx and 4 <>0 if NUL, dx and 8 <>0 if
        clock (somewhat unusual), dx and $20 <>0 if in binary mode (not
        normally, ignores ^Z and the like (Printer.Lst)), dx and $40 <>0 if
        EOF-on-input (^Z), dx and $4000<>0 if supports IOCTL Read/Write
        ($4402,$4403) (most people don't need that) }
  end
end;


So, I'd probably test for dx and $80 = 0 to detect redirection, unless you also
want to know wether the user redirected to/from serial ports or somesuch - then
you'll also need to test some more bits.

Also, with this scheme, you can test separately for redirection on input and
output (and, of course, wether file names the user gave you are files or
devices).

Note, however, that things get slightly complicated when you use the CRT unit:
it will redirect Input and Output to it's own routines. To get a file that
works with DOS Standard I/O, use something like

var stdin: text;

assign(stdin, '');
reset(stdin);

- same for stdout, only with rewrite; you can, of course, use the standard Text
files Input and Output for this to get the pre-CRT situation back.

I hope I was at least marginally understandable; if not, feel free to ask.
If you think this $4400 is an interesting function, get a DOS reference and
look for more $44xx functions - there are.
--
Kai Henningsen  Internet: kh@ms.maus.de
Muenster         UUCP: any_backbone_that_knows_domains!ms.maus.de!kh
Germany         Fido: kh%maus ms, 2:242/2.6 or Kai Henningsen, 2:242/2.244