improve error handling of ttyname_r and isatty