Topic: How to do isatty(stream)?
Author: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
Date: Mon, 1 Aug 1994 07:23:57 GMT Raw View
Ronald F. Guilmette (rfg@netcom.com) wrote:
: In article <9421002.3065@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
: >leland@dtd.com (Leland Woodbury) writes:
: >>2. How can I determine whether or not a stream refers to a tty device?
: >>In C, I can use isatty():
: >>
: >> int flag = isatty(fileno(infp));
: >
: >This is not portable ANSI C, since `isatty' and `fileno' are not
: >in the ANSI standard library...
: Ummm... `isatty' and `fileno' may not be `standard' but I believe that they
: are fairly portable. At least most UNIX and DOS C compilers supply them.
: Which leads to the question... Should C++ I/O streams have member functions
: called `isatty' and/or `fileno'?
They are POSIX standard functions.
--
Stephen Baynes baynes@mulsoc2.serigate.philips.nl
Philips Semicondutors Ltd
Southampton My views are my own.
United Kingdom
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sun, 31 Jul 1994 21:06:11 GMT Raw View
In article <9421002.3065@mulga.cs.mu.OZ.AU> fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>leland@dtd.com (Leland Woodbury) writes:
>>2. How can I determine whether or not a stream refers to a tty device?
>>In C, I can use isatty():
>>
>> int flag = isatty(fileno(infp));
>
>This is not portable ANSI C, since `isatty' and `fileno' are not
>in the ANSI standard library...
Ummm... `isatty' and `fileno' may not be `standard' but I believe that they
are fairly portable. At least most UNIX and DOS C compilers supply them.
Which leads to the question... Should C++ I/O streams have member functions
called `isatty' and/or `fileno'?
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: leland@dtd.com (Leland Woodbury)
Date: 27 Jul 1994 23:46:17 GMT Raw View
Two questions about iostreams from a C programmer who's new to C++; the
first sets up the second, which is the really important one.
1. The following C statement allows infp to point to either standard
input or to a file:
extern char *infile;
FILE *infp = infile ? fopen(infile, "r") : stdin;
How can I write the equivalent in C++? The best alternatives I can find
are either something like:
if(infile)
istream *insp = new ifstream(infile);
else
istream *insp = &cin;
(can't use the ?: syntax because of the mixed types) or, if I want to
continue to use 'cin' as the name of my input stream (and I am not
worried about the istream_with_assign class surviving future revisions
of the language), I can do something like
if(infile) cin = *new ifstream(infile);
But this still doesn't solve my second, related problem which is:
2. How can I determine whether or not a stream refers to a tty device?
In C, I can use isatty():
int flag = isatty(fileno(infp));
which would be fine if I could always get a file descriptor from a
stream, but as near as I can tell, only the fstream makes the file
descriptor available to me (through its filebuf member's fd() member
function). So, for example, there's no way to find out the file
descriptor used by cin.
I'd be much obliged for any ideas, thoughts, advice... hell, even
(helpful) flames.
--
Leland Woodbury
AT&T Downtown Digital, New York City
<leland@dtd.com>
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 28 Jul 1994 16:09:37 GMT Raw View
leland@dtd.com (Leland Woodbury) writes:
>Two questions about iostreams from a C programmer who's new to C++; the
>first sets up the second, which is the really important one.
>
>1. The following C statement allows infp to point to either standard
>input or to a file:
>
> extern char *infile;
> FILE *infp = infile ? fopen(infile, "r") : stdin;
>
>How can I write the equivalent in C++?
ifstream ifs;
istream & isr = (infile ? ifs.open(infile, ios::in), (istream&) ifs
: (istream&) cin);
Now `isr' refers to either `ifs' (the newly opened file stream) or `cin'.
>2. How can I determine whether or not a stream refers to a tty device?
>In C, I can use isatty():
>
> int flag = isatty(fileno(infp));
This is not portable ANSI C, since `isatty' and `fileno' are not
in the ANSI standard library. Similarly, the following C code
will work on most Unix boxes, but it's not portable C++.
filebuf * isp = dynamic_cast<filebuf *> (isr.rdbuf());
bool flag = isp && isatty(isp->fd());
Note that the first line of the above relies of `dynamic_cast', which is
not yet implemented in many C++ compilers; using
filebuf * isp = (filebuf *) (isr.rdbuf());
instead *may* work, but it's probably rather unreliable.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au