don't do a tcsetattr/window ioctl unless asked to
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 5 Jun 1991 04:52:54 +0000 (20:52 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Wed, 5 Jun 1991 04:52:54 +0000 (20:52 -0800)
SCCS-vsn: bin/stty/stty.c 5.26

usr/src/bin/stty/stty.c

index cef1daf..44d4af2 100644 (file)
@@ -12,7 +12,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)stty.c     5.25 (Berkeley) %G%";
+static char sccsid[] = "@(#)stty.c     5.26 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -43,7 +43,7 @@ main(argc, argv)
        struct winsize win;
        struct termios t;
        enum FMT fmt;
        struct winsize win;
        struct termios t;
        enum FMT fmt;
-       int ch, ctl, ldisc, tmp;
+       int ch, ctl, ldisc, set, tmp, wset;
 
        ctl = STDIN_FILENO;
        fmt = NOTSET;
 
        ctl = STDIN_FILENO;
        fmt = NOTSET;
@@ -97,10 +97,11 @@ args:       argc -= optind;
        
 #define        CHK(s)  (**argv == s[0] && !strcmp(*argv, s))
 
        
 #define        CHK(s)  (**argv == s[0] && !strcmp(*argv, s))
 
-       for (; *argv; ++argv) {
+       for (set = wset = 0; *argv; ++argv) {
                if (CHK("-nl")) {
                        t.c_iflag |= ICRNL;
                        t.c_oflag |= ONLCR;
                if (CHK("-nl")) {
                        t.c_iflag |= ICRNL;
                        t.c_oflag |= ONLCR;
+                       set = 1;
                        continue;
                }
                if (CHK("all")) {
                        continue;
                }
                if (CHK("all")) {
@@ -114,6 +115,7 @@ args:       argc -= optind;
                        t.c_oflag |= OPOST;
                        t.c_lflag |= ISIG|IEXTEN;
                        t.c_lflag &= ~ICANON;
                        t.c_oflag |= OPOST;
                        t.c_lflag |= ISIG|IEXTEN;
                        t.c_lflag &= ~ICANON;
+                       set = 1;
                        continue;
                }
                if (CHK("cols")) {
                        continue;
                }
                if (CHK("cols")) {
@@ -125,6 +127,7 @@ args:       argc -= optind;
                        if (!*++argv)
                                err("option requires an argument -- columns");
 columns:               win.ws_col = atoi(*argv);
                        if (!*++argv)
                                err("option requires an argument -- columns");
 columns:               win.ws_col = atoi(*argv);
+                       wset = 1;
                        continue;
                }
                if (CHK("cooked"))
                        continue;
                }
                if (CHK("cooked"))
@@ -136,6 +139,7 @@ columns:            win.ws_col = atoi(*argv);
                        t.c_lflag &= ~ECHOPRT;
                        t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
                        t.c_iflag &= ~IXANY;
                        t.c_lflag &= ~ECHOPRT;
                        t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
                        t.c_iflag &= ~IXANY;
+                       set = 1;
                        continue;
                }
                if (CHK("everything")) {
                        continue;
                }
                if (CHK("everything")) {
@@ -147,7 +151,7 @@ columns:            win.ws_col = atoi(*argv);
                        ioctl(ctl, TIOCEXT, &tmp);
                        continue;
                }
                        ioctl(ctl, TIOCEXT, &tmp);
                        continue;
                }
-               if (CHK("extrpc")) {
+               if (CHK("extproc")) {
                        tmp = 1;
                        ioctl(ctl, TIOCEXT, &tmp);
                        continue;
                        tmp = 1;
                        ioctl(ctl, TIOCEXT, &tmp);
                        continue;
@@ -156,6 +160,7 @@ columns:            win.ws_col = atoi(*argv);
                        if (!*++argv)
                                err("option requires an argument -- ispeed");
                        cfsetispeed(&t, atoi(*argv));
                        if (!*++argv)
                                err("option requires an argument -- ispeed");
                        cfsetispeed(&t, atoi(*argv));
+                       set = 1;
                        continue;
                }
                if (CHK("new"))
                        continue;
                }
                if (CHK("new"))
@@ -163,6 +168,7 @@ columns:            win.ws_col = atoi(*argv);
                if (CHK("nl")) {
                        t.c_iflag &= ~ICRNL;
                        t.c_oflag &= ~ONLCR;
                if (CHK("nl")) {
                        t.c_iflag &= ~ICRNL;
                        t.c_oflag &= ~ONLCR;
+                       set = 1;
                        continue;
                }
                if (CHK("old"))
                        continue;
                }
                if (CHK("old"))
@@ -171,6 +177,7 @@ columns:            win.ws_col = atoi(*argv);
                        if (!*++argv)
                                err("option requires an argument -- ospeed");
                        cfsetospeed(&t, atoi(*argv));
                        if (!*++argv)
                                err("option requires an argument -- ospeed");
                        cfsetospeed(&t, atoi(*argv));
+                       set = 1;
                        continue;
                }
                if (CHK("-raw"))
                        continue;
                }
                if (CHK("-raw"))
@@ -179,12 +186,14 @@ columns:          win.ws_col = atoi(*argv);
                        cfmakeraw(&t);
                        t.c_cflag &= ~(CSIZE|PARENB);
                        t.c_cflag |= CS8;
                        cfmakeraw(&t);
                        t.c_cflag &= ~(CSIZE|PARENB);
                        t.c_cflag |= CS8;
+                       set = 1;
                        continue;
                }
                if (CHK("rows")) {
                        if (!*++argv)
                                err("option requires an argument -- rows");
                        win.ws_row = atoi(*argv);
                        continue;
                }
                if (CHK("rows")) {
                        if (!*++argv)
                                err("option requires an argument -- rows");
                        win.ws_row = atoi(*argv);
+                       wset = 1;
                        continue;
                }
                if (CHK("sane")) {
                        continue;
                }
                if (CHK("sane")) {
@@ -195,6 +204,7 @@ reset:                      t.c_cflag = TTYDEF_CFLAG | (t.c_cflag & CLOCAL);
 #define        LKEEP   (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
                        t.c_lflag = TTYDEF_LFLAG | (t.c_lflag & LKEEP);
                        t.c_oflag = TTYDEF_OFLAG;
 #define        LKEEP   (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
                        t.c_lflag = TTYDEF_LFLAG | (t.c_lflag & LKEEP);
                        t.c_oflag = TTYDEF_OFLAG;
+                       set = 1;
                        continue;
                }
                if (CHK("size")) {
                        continue;
                }
                if (CHK("size")) {
@@ -216,24 +226,28 @@ tty:                      tmp = TTYDISC;
                        if (CHK(mp->name)) {
                                t.c_cflag &= ~mp->unset;
                                t.c_cflag |= mp->set;
                        if (CHK(mp->name)) {
                                t.c_cflag &= ~mp->unset;
                                t.c_cflag |= mp->set;
+                               set = 1;
                                goto next;
                        }
                for (mp = imodes; mp->name; ++mp)
                        if (CHK(mp->name)) {
                                t.c_iflag &= ~mp->unset;
                                t.c_iflag |= mp->set;
                                goto next;
                        }
                for (mp = imodes; mp->name; ++mp)
                        if (CHK(mp->name)) {
                                t.c_iflag &= ~mp->unset;
                                t.c_iflag |= mp->set;
+                               set = 1;
                                goto next;
                        }
                for (mp = lmodes; mp->name; ++mp)
                        if (CHK(mp->name)) {
                                t.c_lflag &= ~mp->unset;
                                t.c_lflag |= mp->set;
                                goto next;
                        }
                for (mp = lmodes; mp->name; ++mp)
                        if (CHK(mp->name)) {
                                t.c_lflag &= ~mp->unset;
                                t.c_lflag |= mp->set;
+                               set = 1;
                                goto next;
                        }
                for (mp = omodes; mp->name; ++mp)
                        if (CHK(mp->name)) {
                                t.c_oflag &= ~mp->unset;
                                t.c_oflag |= mp->set;
                                goto next;
                        }
                for (mp = omodes; mp->name; ++mp)
                        if (CHK(mp->name)) {
                                t.c_oflag &= ~mp->unset;
                                t.c_oflag |= mp->set;
+                               set = 1;
                                goto next;
                        }
                for (cp = cchars1; cp->name; ++cp) {
                                goto next;
                        }
                for (cp = cchars1; cp->name; ++cp) {
@@ -256,6 +270,7 @@ ccfound:            if (!*++argv)
                                    (*argv)[1] & 037;
                        else
                                t.c_cc[cp->sub] = **argv;
                                    (*argv)[1] & 037;
                        else
                                t.c_cc[cp->sub] = **argv;
+                       set = 1;
                        goto next;
                }
 
                        goto next;
                }
 
@@ -273,9 +288,9 @@ ccfound:            if (!*++argv)
 next:          continue;
        }
 
 next:          continue;
        }
 
-       if (tcsetattr(ctl, 0, &t) < 0)
+       if (set && tcsetattr(ctl, 0, &t) < 0)
                err("tcsetattr: %s", strerror(errno));
                err("tcsetattr: %s", strerror(errno));
-       if (ioctl(ctl, TIOCSWINSZ, &win) < 0)
+       if (wset && ioctl(ctl, TIOCSWINSZ, &win) < 0)
                warn("TIOCSWINSZ: %s", strerror(errno));
        exit(0);
 }
                warn("TIOCSWINSZ: %s", strerror(errno));
        exit(0);
 }