Poll0
In a pipeline you may have one segment that must wait for input before executing. Think xargs
but passing data through rather than using it as command-line arguments. Today I addressed this with a simple C program, called poll0
. It polls file descriptor 0
and exits 0
if data is ready, and 1
otherwise. It takes an optional argument giving a millisecond timeout for the call to the poll()
system call.
It’s does one thing only. It’s simple. And it solves a useful problem. And the bonus: Claude wrote it.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
int main(int argc, char *argv[]) {
int timeout = -1;
if (argc > 2) {
fprintf(stderr, "Usage: %s [timeout_ms]\n", argv[0]);
return 1;
}
if (argc == 2) {
timeout = atoi(argv[1]);
}
struct pollfd fds[1];
fds[0].fd = 0; // stdin
fds[0].events = POLLIN;
int result = poll(fds, 1, timeout);
if (result < 0) {
perror("poll");
return 1;
}
return (result == 0) ? 1 : 0; // 1 on timeout, 0 on input available
}