The select
function can do it on a POSIX system. On Windows or others you would need other options.
Note that if the shell sets up the pipeline and the operating system schedules the child to run first (first | child)
it could execute the select and not find any input ready to read.
Note that I changed the timeout from 0 to 100,000 microseconds (0.1 seconds) which helps avoid randomly missing the input. However, if the system is under heavy load it could still fail to detect stdin being ready to read.
See:
#include <stdio.h>
#include <sys/select.h>
int main() {
fd_set read_fds;
struct timeval tv = {0, 100000};
int fd = fileno(stdin);
int r;
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);
r = select(fd + 1, &read_fds, NULL, NULL, &tv);
if (r > 0) {
char buffer[256];
if (fgets(buffer, 256, stdin)) {
puts(buffer);
}
}
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…