Better handling the return value from write()