Three small changes.
1. Based on the test "script < /dev/null", revision 1.33 changes
(void)tcgetattr(STDIN_FILENO, &tt);
(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
to
if (isatty(0)) {
if (tcgetattr(STDIN_FILENO, &tt) == 0 &&
ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == 0)
istty = 1;
}
Actually
if (tcgetattr(STDIN_FILENO, &tt) == 0 &&
ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == 0)
istty = 1;
would have been sufficient since tcgetattr will fail with errno set to
ENOTTY if stdin is not a tty. This patch removes the redundant
isatty(0).
2. The following snippet
if (isatty(slave))
puts("ISTTY");
else
puts("NOTTY");
was added to see that "script < /dev/null" prints "NOTTY". The glibc
implementation of openpty [ see
https://code.woboq.org/userspace/glibc/login/openpty.c.html ] debates
if tcsetattr (slave, TCSAFLUSH, termp) and ioctl (slave, TIOCSWINSZ,
winp) errors should be ignored [ look for comment /* XXX Should we
ignore errors here? */ ]. If slave is not a tty, such calls will fail
with errno set to ENOTTY. While openpty(&master, &slave, NULL, &tt,
&win) works for now, !istty case should call openpty like this
openpty(&master, &slave, NULL, NULL, NULL)
for future-proofing.
3. Register sigwinch handler only if(istty).
Signed-off-by: Soumendra Ganguly <[email protected]>
---
--- src/usr.bin/script/script.c 2020-08-04 02:41:13.226129480 -0500
+++ script.c 2020-08-05 00:23:50.922879719 -0500
@@ -132,13 +132,14 @@
if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
err(1, "%s", fname);
- if (isatty(0)) {
- if (tcgetattr(STDIN_FILENO, &tt) == 0 &&
- ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == 0)
- istty = 1;
- }
- if (openpty(&master, &slave, NULL, &tt, &win) == -1)
+ if (tcgetattr(STDIN_FILENO, &tt) == 0 &&
+ ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == 0) {
+ istty = 1;
+ if (openpty(&master, &slave, NULL, &tt, &win) == -1)
+ err(1, "openpty");
+ } else if (openpty(&master, &slave, NULL, NULL, NULL) == -1) {
err(1, "openpty");
+ }
(void)printf("Script started, output file is %s\n", fname);
if (istty) {
@@ -147,13 +148,13 @@
cfmakeraw(&rtt);
rtt.c_lflag &= ~ECHO;
(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
- }
- bzero(&sa, sizeof sa);
- sigemptyset(&sa.sa_mask);
- sa.sa_handler = handlesigwinch;
- sa.sa_flags = SA_RESTART;
- (void)sigaction(SIGWINCH, &sa, NULL);
+ bzero(&sa, sizeof sa);
+ sigemptyset(&sa.sa_mask);
+ sa.sa_handler = handlesigwinch;
+ sa.sa_flags = SA_RESTART;
+ (void)sigaction(SIGWINCH, &sa, NULL);
+ }
child = fork();
if (child == -1) {