if (stdio->fd < 0)
return 0;
- // Don't restore anything if we didn't save things before
- if (!stdio->attrs_saved)
- return 0;
-
- // Skip everything if fd is not a TTY
- if (!isatty(stdio->fd))
- return 0;
-
// Fetch the flags
flags = fcntl(stdio->fd, F_GETFL, 0);
if (flags < 0) {
}
// Restore the attributes
- r = tcsetattr(stdio->fd, TCSANOW, &stdio->attrs);
- if (r) {
- CTX_ERROR(pty->ctx, "Could not restore terminal attributes for %d, ignoring: %s\n",
- stdio->fd, strerror(errno));
- return -errno;
+ if (stdio->attrs_saved) {
+ r = tcsetattr(stdio->fd, TCSANOW, &stdio->attrs);
+ if (r) {
+ CTX_ERROR(pty->ctx, "Could not restore terminal attributes for %d, ignoring: %s\n",
+ stdio->fd, strerror(errno));
+ return -errno;
+ }
}
// Close the file descriptor
// Store all attributes
r = tcgetattr(stdio->fd, &stdio->attrs);
if (r) {
- CTX_ERROR(pty->ctx, "Could not fetch terminal attributes from fd %d: %s\n",
- stdio->fd, strerror(errno));
- return -errno;
+ switch (errno) {
+ case ENOTTY:
+ return 0;
+
+ default:
+ CTX_ERROR(pty->ctx, "Could not fetch terminal attributes from fd %d: %s\n",
+ stdio->fd, strerror(errno));
+
+ return -errno;
+ }
}
// Mark the attributes as saved