Redirection question

Doug Gwyn gwyn at smoke.brl.mil
Tue Nov 13 17:51:58 AEST 1990


In article <roarment.658423394 at faui09> roarment at faui09.informatik.uni-erlangen.de (Roberto Armenti) writes:
>Is there a _portable_ way to recognize whether input comes from
>stdin or from another stream ?

Wrong question.  It comes from stdin if you're reading it from the
stdin stream.

>To make the problem clearer : i'm working on a programm that takes
>its input from stdin. If however someone chooses to redirect the input
>to a file, so that data is read from that file, how can I figure that
>out ( Prompting and so on should not occur in this case ) ???

What you really want to determine is whether or not input is coming
from an interactive device.  This is in general impossible to
ascertain.  The usual UNIX kludge is to invoke isatty(fileno(stdin));
I don't know whether or not MS/DOS has something equivalent.  Anyway,
it is not a reliable test, since just because something is coming via
a terminal port does not necessarily mean that there is a human typing
the input.  You should probably use a command option to determine how
prompting should be done, if at all.  (One approach is to not prompt
initially, but support an input command that enables/disables
prompting; the UNIX System V "ed" uses this method.)



More information about the Comp.lang.c mailing list