Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect if stdin is via keyboard or via < file?

3 views
Skip to first unread message

srb...@sbcglobal.net

unread,
Aug 26, 2005, 7:17:19 PM8/26/05
to
Can a program detect if it's stdin is
coming from keyboard input vs. being
redirected from a file?

If it's from a file, I want to fputs()
the text. But if it's from the keyboard,
I won't (don't want it typed, then
fputs()'d.

SRB

Lew Pitcher

unread,
Aug 26, 2005, 8:35:12 PM8/26/05
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

srb...@sbcglobal.net wrote:
> Can a program detect if it's stdin is
> coming from keyboard input vs. being
> redirected from a file?

Not in standard C, it can't.

But, with a platform-specific extension, it often can.

> If it's from a file, I want to fputs()
> the text. But if it's from the keyboard,
> I won't (don't want it typed, then
> fputs()'d.

You'll either have to resort to some sort of signal data (like a command
argument), or use a platform-specific extension to determine whether stdin
comes from a file or an interactive device.


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDD7VAagVFX4UWr64RAi2yAJ9M7NGcb3z4zRVIkuJ8Eg7WKaQdFQCfYBie
Wl2MOzXg/kxLYBvIVGwErRw=
=Fiqc
-----END PGP SIGNATURE-----

CBFalconer

unread,
Aug 26, 2005, 11:00:52 PM8/26/05
to
Lew Pitcher wrote:
> srb...@sbcglobal.net wrote:
>
>> Can a program detect if it's stdin is coming from keyboard input
>> vs. being redirected from a file?
>
> Not in standard C, it can't.
>
> But, with a platform-specific extension, it often can.
>
>> If it's from a file, I want to fputs() the text. But if it's from
>> the keyboard, I won't (don't want it typed, then fputs()'d.
>
> You'll either have to resort to some sort of signal data (like a
> command argument), or use a platform-specific extension to
> determine whether stdin comes from a file or an interactive device.

However, on most unixlike systems there are methods available,
which are OT here. The OP can examine one such in my id2id-20,
which is available at:

<http://cbfalconer.home.att.net/download/id2id-20.zip>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


Chris Dollin

unread,
Aug 31, 2005, 7:20:51 AM8/31/05
to
srb...@sbcglobal.net wrote:

I found the most straightforward tactic in a similar situation
(whether to issue a prompt or not, for an interactive interpreter)
to be a command-line flag. The program no longer needs to be able
to answer the question.

--
Chris "electric hedgehog" Dollin
Almost all predictions about life in 2015 are wrong in some fundamental
respect.

0 new messages