Change safe_read() to select() before reading.
Fix timeout checking in safe_read().