Board index » delphi » A strange problem.

A strange problem.

I've run into a bit of a mystery. Below are two functions. One
detects whether input is being redirected. The other detects
whether output is being redirected.

Function OutputRedirected:Boolean;
var rg:registers;
 OutputRedirected:=(rg.dl and 128=0) or (rg.dl and 2 = 0);
Function InputRedirected:Boolean;
var rg:registers;
 InputRedirected:=(rg.dl and 128=0) or (rg.dl and 1 = 0);

Both of these functions work! But, I wrote the following program

program pip;
uses dos;
var i,j,k:integer;
for k:=1 to 10 do begin
 writeln(i:5,' + ',j:5,' = ',i+j:5);
for k:=1 to 10 do begin
writeln('****> ',rgsv.dl);

Now, I prepared a text file called n.txt with 10 lines, each
containing a pair of numbers to be processed.

When I type pip.exe<n.txt, it outputs the answers, and then it
gives  rgsv.dl as being equal to 66.

This answer works for me because input is redirected and should
be detected if

(rg.dl and 128=0) or (rg.dl and 1 = 0)

01000010 = 66 (in binary).

Notice that if rg.dl = 66, then (rg.dl AND 1 = 0) should return
TRUE since 66 is even. So far, so good.

Then, when I type pip.exe>n2.txt and inspect the contents of
n2, I get all of the ouput of pip.exe and at the bottom it shows
rgsv.dl equal to 211.

I have a problem with this. Output redirected should return true

(rg.dl and 128=0) or (rg.dl and 2 = 0)

11010011 = 211 (in binary).

Notice that if rgdl = 211 then both (rg.dl AND 128=0) arefalse
AND (rg.dl and 2=0) are false.

And yet the outputredirected function works. It returns true when
output is redirected. I've confirmed it by writing a program which
outputs the value of that function and then I've run the program
using redirection.

What is the cause of this discreprency? TIA.

P.S.,  The reason why pip.pas contains a for loop which outputs random
uppercase letters is because
I wanted to see what value rgsv.dl would have if I tried piping. For
example pip.exe|find "**".
Patrick D. Rockwell


Re:A strange problem.

In Article <> "Patrick D. Rockwell" <> wrote:
>I've run into a bit of a mystery. Below are two functions. One
>detects whether input is being redirected. The other detects
>whether output is being redirected.

>Function OutputRedirected:Boolean;
>var rg:registers;
> rg.bx:=1;


> MsDos(rg);
> OutputRedirected:=(rg.dl and 128=0) or (rg.dl and 2 = 0);
>Function InputRedirected:Boolean;
>var rg:registers;
> rg.bx:=0;


> MsDos(rg);
> InputRedirected:=(rg.dl and 128=0) or (rg.dl and 1 = 0);

Here is the function from Ralf Brown's interrupt list.

        AX = 4400h
        BX = handle
Return: CF clear if successful
            DX = device information word (see below)
            AX destroyed
        CF set on error
            AX = error code (01h,05h,06h) (see AH=59h)
Notes:  value in DH corresponds to high byte of device driver's attribute word
          if handle refers to a character device
        Novell NetWare reportedly does not return a drive number in bits 5-0
          for a disk file
SeeAlso: AX=4401h,INT 2F/AX=122Bh

Bitfields for device information word:
 character device
  bit 14: device driver can process IOCTL requests (see AX=4402h)
  bit 13: output until busy supported
  bit 11: driver supports OPEN/CLOSE calls
  bit  7: set (indicates device)
  bit  6: EOF on input
  bit  5: raw (binary) mode
  bit  4: device is special (uses INT 29)
  bit  3: clock device
  bit  2: NUL device
  bit  1: standard output
  bit  0: standard input
 disk file
  bit 15: file is remote (DOS 3+)
  bit 14: don't set file date/time on closing (DOS 3+)
  bit 11: media not removable
  bit  8: (DOS 4 only) generate INT 24 if no disk space on write or read past
                end of file
  bit  7: clear (indicates file)
  bit  6: file has not been written
  bits 5-0: drive number (0 = A:)

So the first test rg.dl and 128=0 is true if the handle points to a file.
The second test is to check if the device is standard input/output.


Other Threads