Check errno for all IO calls.

The posix open(), close(), read() and write() functions will fail and
set errno to EINTR if they are interrupted by a signal.  This can
potentially occur even when the process is stopped with Ctrl-Z and
restarted, since these actions involve signals.  The fread(), fwrite()
etc. calls on most modern unices bend the posix rules a little: their
default behavior is to retry after being interrupted.  However, for
unices that do not automatically retry (AIX?), we should include the
retry logic ourselves.

Note that a retry is not always desired.  For example, if SIGINT is
caught, the goal should be to clean up rather than to continue.