This new attachment obsoletes the old patch. If stdin is not terminal,
this does not register a handler for SIGWINCH at all.

Thank you.

On 8/4/20, Soumendra Ganguly <[email protected]> wrote:
> Program: script(1)
>
> Issue description: Currently, script(1) passes &tt and &win to openpty
> even when isatty(0) == 0. Also, the SIGWINCH handler does not check if
> stdin is a tty.
>
> The patch follows [ also attached ].
>
> Signed-off-by: Soumendra Ganguly <[email protected]>
> ---
> --- src/usr.bin/script/script.c       2020-08-03 21:33:44.636187087 -0500
> +++ script.c  2020-08-04 01:03:15.925066340 -0500
> @@ -74,18 +74,18 @@
>  #include <util.h>
>  #include <err.h>
>
> -FILE *fscript;
> -int  master, slave;
> +static FILE *fscript;
> +static int master, slave;
>  volatile sig_atomic_t child;
> -pid_t        subchild;
> -char *fname;
> +static pid_t subchild;
> +static char *fname;
>
>  volatile sig_atomic_t dead;
>  volatile sig_atomic_t sigdeadstatus;
>  volatile sig_atomic_t flush;
>
> -struct       termios tt;
> -int          istty;
> +static int istty;
> +static struct termios tt;
>
>  __dead void done(int);
>  void dooutput(void);
> @@ -132,13 +132,18 @@
>       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;
> +     istty = isatty(STDIN_FILENO);
> +     if (istty) {
> +             if (tcgetattr(STDIN_FILENO, &tt) == -1)
> +                     err(1, "tcgetattr");
> +             if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
> +                     err(1, "ioctl");
> +             if (openpty(&master, &slave, NULL, &tt, &win) == -1)
> +                     err(1, "openpty");
> +     } else {
> +             if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
> +                     err(1, "openpty");              
>       }
> -     if (openpty(&master, &slave, NULL, &tt, &win) == -1)
> -             err(1, "openpty");
>
>       (void)printf("Script started, output file is %s\n", fname);
>       if (istty) {
> @@ -227,7 +232,7 @@
>       struct winsize win;
>       pid_t pgrp;
>
> -     if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) {
> +     if (istty && ioctl(STDIN_FILENO, TIOCGWINSZ, &win) != -1) {
>               ioctl(slave, TIOCSWINSZ, &win);
>               if (ioctl(slave, TIOCGPGRP, &pgrp) != -1)
>                       killpg(pgrp, SIGWINCH);
>
--- src/usr.bin/script/script.c 2020-08-04 02:41:13.226129480 -0500
+++ script.c    2020-08-04 02:41:35.078146831 -0500
@@ -74,18 +74,18 @@
 #include <util.h>
 #include <err.h>
 
-FILE   *fscript;
-int    master, slave;
+static FILE *fscript;
+static int master, slave;
 volatile sig_atomic_t child;
-pid_t  subchild;
-char   *fname;
+static pid_t subchild;
+static char *fname;
 
 volatile sig_atomic_t dead;
 volatile sig_atomic_t sigdeadstatus;
 volatile sig_atomic_t flush;
 
-struct termios tt;
-int            istty;
+static int istty;
+static struct termios tt;
 
 __dead void done(int);
 void dooutput(void);
@@ -132,13 +132,18 @@
        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;
+       istty = isatty(STDIN_FILENO);
+       if (istty) {
+               if (tcgetattr(STDIN_FILENO, &tt) == -1)
+                       err(1, "tcgetattr");
+               if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
+                       err(1, "ioctl");
+               if (openpty(&master, &slave, NULL, &tt, &win) == -1)
+                       err(1, "openpty");
+       } else {
+               if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
+                       err(1, "openpty");              
        }
-       if (openpty(&master, &slave, NULL, &tt, &win) == -1)
-               err(1, "openpty");
 
        (void)printf("Script started, output file is %s\n", fname);
        if (istty) {
@@ -147,13 +152,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) {

Reply via email to