I'm trying to pas an input stream through a pipe and want to catch a condition in which a downstream pipeline program might fail in which case I need to restart it. So I put it into a loop:
step1 |while true ; do step2 ; done
but if the pipeline upstream is closed, then I want that loop to exit. I can't tell this from the exit status of step2 else I might have said
step1 |until [ $? -ne 0 ] ; do step2 ; done
I need a test to check if stdin in closed but not actually consume a character from stdin.
I'm trying to think how I would do it in C. Does read(2) of a zero buffer allow me to test this? I don't think it does. How about select(2)? Nope. Or does it? How about fcntl(2)? I can't find anything.
Is there really no other way than to consume a byte and then somehow put it out again? No test for file descriptor is closed?