How do I check if my program has data piped into i

2019-04-25 03:21发布

问题:

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

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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



回答5:

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.



回答6:

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:

How to check if stdin is still opened without blocking?



标签: c++ c pipe stdin