Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Detect if stdin is via keyboard or via < file? (http://www.velocityreviews.com/forums/t439206-detect-if-stdin-is-via-keyboard-or-via-file.html)

srbstar@sbcglobal.net 08-26-2005 11:17 PM

Detect if stdin is via keyboard or via < file?
 
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 08-27-2005 12:35 AM

Re: Detect if stdin is via keyboard or via < file?
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

srbstar@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)

iD8DBQFDD7VAagVFX4UWr64RAi2yAJ9M7NGcb3z4zRVIkuJ8Eg 7WKaQdFQCfYBie
Wl2MOzXg/kxLYBvIVGwErRw=
=Fiqc
-----END PGP SIGNATURE-----

CBFalconer 08-27-2005 03:00 AM

Re: Detect if stdin is via keyboard or via < file?
 
Lew Pitcher wrote:
> srbstar@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 08-31-2005 11:20 AM

Re: Detect if stdin is via keyboard or via < file?
 
srbstar@sbcglobal.net wrote:

> 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.


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.


All times are GMT. The time now is 03:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.