Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!decwrl!uunet!nih-csl!lhc!adm!smoke!gwyn From: gwyn@smoke.brl.mil (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: Redirection question Message-ID: <14447@smoke.brl.mil> Date: 13 Nov 90 06:51:58 GMT References: Organization: U.S. Army Ballistic Research Laboratory, APG, MD. Lines: 22 In article roarment@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.)