What you are describing is the standard behavior of read
on a file in non-blocking
mode.
If there is no data to read it will return -1 with errno set to EAGAIN. You can then go about doing something else for awhile.
If there is data to read it will return how much was read.
If there is no possibility of reading more data (because of eof on a file or the closing of the write end of a pipe or whatnot) then it returns 0.
You can set non-blocking mode either as a parameter to open
or via fcntl
.