<snip off topic answer.
It is not an answer (a 5 lines program can not be), it is a pointer.
Which, as you don't know the OPs platform (it was not specified) could
be pointing in COMPLETELY the wrong direction.
It is the OP who must decide if follow it and collect more information
about, if applicable, ... . In this way, and together with the groups
that has been pointed there are a starting point. Can be in this group
there are not BSD/POSIX/... specialist, but at least there are enough
knowledgment to point a few of posible continuation lines that unblocks
a situation and helps a colleague.
Had you specified that it was a POSIX/BSD/whatever solution and pointed
the OP to an appropriate group then it would not have been so bad.
However, there are systems with select etc. where they ONLY work on
sockets, not on the standard input stream. Did you give any indication
that it might compile and appear to work until the critical moment that
the OP shows it to his/her boss? No. You gave an off topic response with
absolutely NO indication it was off topic, or that there are better
places to discus the best solution, or that it might compile but do
completely the wrong thing. That can not only be not helpful, it can
also be counter-productive both for this group (by encouraging
discussion of things that are off topic) and for the OP (by making the
OP try it, have problems getting it to work, and spend ages trying to
find out what is going with you giving POSIX answers when for all you
know the OP is using CPM.