zhyfer
zhyfer

Reputation: 920

c - check for when nothing is piped to stdin

Basically I'm trying to check if anything is in stdin when the program is called, so if I've got another file called output that writes to stdout then ./output | ./program should work and ./program should exit with an error

Upvotes: 4

Views: 560

Answers (2)

lhf
lhf

Reputation: 72422

In POSIX, you can use isatty.

Upvotes: 5

user611775
user611775

Reputation: 1353

isatty checks for a tty, not a pipe. Use fstat(STDIN_FILENO, &sb) instead and check for S_ISFIFO(sb.st_mode).

To check whether there is anything "in" stdin that you could possibly read, you use, for example poll(2) with an event mask of POLLIN.

Upvotes: 3

Related Questions