C - check when nothing is sent to stdin

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

+4
source share
2 answers

On POSIX, you can use isatty .

+6
source

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

To check if there is anything "in" stdin that you could read, you use, for example, poll(2) with the POLLIN event POLLIN .

+2
source

Source: https://habr.com/ru/post/1339401/


All Articles