commit:     339297b3247c8850194a48edf1ce03dbfdef337a
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun Jul 14 08:34:31 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun Jul 14 08:34:31 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=339297b3

main: rework terminal-based settings somewhat

As pointed out by slyfox, the result from ioctl was ignored and its
result used anyway.  While at it to fix this, rework the logic somewhat,
such that terminal width and colours are always disabled when we're not
dealing with a TTY.

Bug: https://bugs.gentoo.org/689290#c14
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 main.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/main.c b/main.c
index c5cc4b4..bdbb2a7 100644
--- a/main.c
+++ b/main.c
@@ -780,9 +780,6 @@ int main(int argc, char **argv)
        struct stat st;
        struct winsize winsz;
 
-       ioctl(0, TIOCGWINSZ, &winsz);
-       twidth = winsz.ws_col > 0 ? (int)winsz.ws_col : 80;
-
        warnout = stderr;
        IF_DEBUG(init_coredumps());
        argv0 = argv[0];
@@ -791,13 +788,18 @@ int main(int argc, char **argv)
        bindtextdomain(argv0, CONFIG_EPREFIX "usr/share/locale");
        textdomain(argv0);
 
-       if (fstat(fileno(stdout), &st) != -1)
+       twidth = 0;
+       if (fstat(fileno(stdout), &st) != -1) {
                if (!isatty(fileno(stdout))) {
                        no_colors();
-                       twidth = 0;
+               } else {
+                       if ((getenv("TERM") == NULL) ||
+                                       (strcmp(getenv("TERM"), "dumb") == 0))
+                               no_colors();
+                       if (ioctl(0, TIOCGWINSZ, &winsz) == 0 && winsz.ws_col > 
0)
+                               twidth = (int)winsz.ws_col;
                }
-       if ((getenv("TERM") == NULL) || (strcmp(getenv("TERM"), "dumb") == 0))
-               no_colors();
+       }
 
        initialize_portage_env();
        optind = 0;

Reply via email to