tags:

views:

221

answers:

6

Im writing a program that should read input via stdin, so I have the following contruct.

FILE *fp=stdin;

But this just hangs if the user hasn't piped anything into the program, how can I check if the user is actually piping data into my program like

gunzip -c file.gz |./a.out #should work
./a.out  #should exit program with nice msg.

thanks

+2  A: 

Passing stdin to select() or poll() should tell you if input is waiting. Under many OSes you can also tell if stdin is a tty or pipe.

EDIT: I see I'm going to have to emphasize the also part of the tty test. A fifo is not a tty, yet there might be no input ready for an indefinite amount of time.

Ben Voigt
I think the OP just wanted to cover the run-on-command-line-and-wonder-why-it-"hangs" case. Probably trying to fix a PEBKAC bug. Sitting there waiting on a FIFO or other pipe would be okay.
Mike DeSimone
+3  A: 

Try "man isatty", I think that function will tell you if you are talking to the user or not.

Jeremy Friesner
+2  A: 

Use isatty to detect that stdin is coming from a terminal rather than a redirect.

Marcelo Cantos
+1  A: 

See the function "isatty" - if STDIN is a terminal, you can skip reading from it. If it's not a terminal, you're getting data piped or redirected and you can read until EOF.

Andrew Medico
+1  A: 

Since you're using file pointers, you'll need both isatty() and fileno() to do this:

#include <unistd.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    FILE* fp = stdin;

    if(isatty(fileno(fp)))
    {
        fprintf(stderr, "A nice msg.\n");
        exit(1);
    }

    /* carry on... */
    return 0;
}

Actually, that's the long way. The short way is to not use file pointers:

#include <unistd.h>

int main(int argc, char* argv[])
{
    if(isatty(STDIN_FILENO))
    {
        fprintf(stderr, "A nice msg.\n");
        exit(1);
    }

    /* carry on... */
    return 0;
}

Several standard Unix programs do this check to modify their behavior. For example, if you have ls set up to give you pretty colors, it will turn the colors off if you pipe its stdout to another program.

Mike DeSimone
A: 

An additional option you get with select() is setting a timeout for reading from stdin (with respect to either the first read from stdin or consecutive reads from stdin).

For a code example using select on stdin see:

http://stackoverflow.com/questions/1594251/how-to-check-if-stdin-is-still-opened-without-blocking

kroener