Article View: comp.os.msdos.programmer
Article #5936Re: Sensing STDOUT redirection in C
From: Adrian Collins
Date: Tue, 23 Apr 1991 12:48
Date: Tue, 23 Apr 1991 12:48
56 lines
2573 bytes
2573 bytes
In <1991Apr18.165820.6035@amc.com> jwbirdsa@amc.com (James Birdsall) writes:
>In article <1991Apr16.145847.20316@midway.uchicago.edu> valley@gsbsun.uchicago.edu (Doug Dougherty) writes:
>>collinsa@p4.cs.man.ac.uk (Adrian Collins) writes:
>>>Does anybody know how to sense whether output redirection has been used
>>>on the command line?
>>isatty(1)
>>(Works in Turbo C)
> Well, almost. isatty() returns true if the supplied handle is the
>console *or* a printer or serial port. Determining whether the handle is
>actually the console or not requires an ioctl() and checking some bits.
This is what I actually wanted to do, although I didn't really make it
very clear in my original article.
As I've had quite a few requests for responses to my original query, here
is a method of working out whether STDIN and STDOUT are being redirected.
This is a slightly altered version of the code sent to me by
Nick FitzGerald.
unsigned int get_device_data(int handle)
{
union REGS regs;
regs.h.ah = 0x44; /* function number */
regs.h.al = 0; /* subfunction number */
regs.x.bx = handle; /* file or device handle */
int86(0x21,®s,®s);
return ( regs.x.dx );
}
int output_redirected()
{
return ( ( get_device_data(fileno(stdout)) & 0x82 ) != 0x82 );
}
int input_redirected()
{
return ( ( get_device_data(fileno(stdin)) & 0x81) != 0x81 );
}
The masks have been altered to 0x81 & 0x82 from 0x01 and 0x02, so that the
code correctly senses output redirected to a file.
Thanks to everybody for your help.
Cheers,
Adrian
---
Adrian Collins collinsa@uk.ac.man.cs.p4
Department of Computer Science a.m.collins@uk.ac.mcc
University of Manchester
Manchester, "Let me face the peril"
UK. "No, it's too perilous!"
- The Holy Grail
Technical Details
Message-ID:
<collinsa.672410890@p4.cs.man.ac.uk>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!mucs!p4.cs.man.ac.uk!collinsaReferences:
<collinsa.671721083@p4.cs.man.ac.uk> <1991Apr16.145847.20316@midway.uchicago.edu> <1991Apr18.165820.6035@amc.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!mucs!p4.cs.man.ac.uk!collinsa From: collinsa@p4.cs.man.ac.uk (Adrian Collins) Newsgroups: comp.os.msdos.programmer Subject: Re: Sensing STDOUT redirection in C Keywords: STDOUT, Redirection, MSDOS Message-ID: <collinsa.672410890@p4.cs.man.ac.uk> Date: 23 Apr 91 12:48:10 GMT References: <collinsa.671721083@p4.cs.man.ac.uk> <1991Apr16.145847.20316@midway.uchicago.edu> <1991Apr18.165820.6035@amc.com> Sender: news@cs.man.ac.uk Lines: 56