]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/tty: Pass default width to get_terminal_width()
authorBoris Egorov <egorov@linux.com>
Tue, 5 Jan 2016 16:17:58 +0000 (22:17 +0600)
committerKarel Zak <kzak@redhat.com>
Wed, 6 Jan 2016 11:22:07 +0000 (12:22 +0100)
Almost any code calling get_terminal_width() checks returned width for
non-positive values and sets it to some default value (say, 80). So,
let's pass this default value directly to the function.

[kzak@redhat.com: - get_terminal_width() refactoring]

Signed-off-by: Karel Zak <kzak@redhat.com>
disk-utils/fdisk-list.c
include/ttyutils.h
lib/ttyutils.c
libsmartcols/src/table_print.c
misc-utils/blkid.c
misc-utils/kill.c
text-utils/column.c

index 2e491ca41b9ba16fc7bfce2a04c232f22938a990..604d9439169cff843f823bf0e01c7df546e1910a 100644 (file)
@@ -405,9 +405,7 @@ void list_available_columns(FILE *out)
        if (!cxt)
                return;
 
-       termwidth = get_terminal_width();
-       if (termwidth <= 0)
-               termwidth = 80;
+       termwidth = get_terminal_width(80);
 
        fprintf(out, _("\nAvailable columns (for -o):\n"));
 
index e842f9f0d0e2998eb6ee75dca273269ea08c9a61..200e9a565eb4c0b272406c4522e48067e424307d 100644 (file)
@@ -50,7 +50,7 @@ struct chardata {
                (ptr)->capslock = 0;         \
        } while (0)
 
-extern int get_terminal_width(void);
+extern int get_terminal_width(int default_width);
 extern int get_terminal_name(int fd, const char **path, const char **name,
                             const char **number);
 
index ea551e26c811771f5a12f5dc7495444bac2eadd9..1a1d8bc388c683860e8af48e4742a71d503182e4 100644 (file)
@@ -9,37 +9,37 @@
 #include "c.h"
 #include "ttyutils.h"
 
-int get_terminal_width(void)
+int get_terminal_width(int default_width)
 {
-#ifdef TIOCGSIZE
-       struct ttysize  t_win;
-#endif
-#ifdef TIOCGWINSZ
-       struct winsize  w_win;
-#endif
-        const char     *cp;
+       int width = 0;
 
-#ifdef TIOCGSIZE
-       if (ioctl (STDIN_FILENO, TIOCGSIZE, &t_win) == 0)
-               return t_win.ts_cols;
-#endif
-#ifdef TIOCGWINSZ
+#if defined(TIOCGWINSZ)
+       struct winsize  w_win;
        if (ioctl (STDIN_FILENO, TIOCGWINSZ, &w_win) == 0)
-               return w_win.ws_col;
+               width = w_win.ws_col;
+#elif defined(TIOCGSIZE)
+       struct ttysize  t_win;
+       if (ioctl (STDIN_FILENO, TIOCGSIZE, &t_win) == 0)
+               width = t_win.ts_cols;
 #endif
-        cp = getenv("COLUMNS");
-       if (cp) {
-               char *end = NULL;
-               long c;
 
-               errno = 0;
-               c = strtol(cp, &end, 10);
+       if (width <= 0) {
+               const char *cp = getenv("COLUMNS");
+
+               if (cp) {
+                       char *end = NULL;
+                       long c;
+
+                       errno = 0;
+                       c = strtol(cp, &end, 10);
 
-               if (errno == 0 && end && *end == '\0' && end > cp &&
-                   c > 0 && c <= INT_MAX)
-                       return c;
+                       if (errno == 0 && end && *end == '\0' && end > cp &&
+                           c > 0 && c <= INT_MAX)
+                               width = c;
+               }
        }
-       return 0;
+
+       return width > 0 ? width : default_width;
 }
 
 int get_terminal_name(int fd,
@@ -88,7 +88,7 @@ int main(void)
                fprintf(stderr, "tty name:   %s\n", name);
                fprintf(stderr, "tty number: %s\n", num);
        }
-       fprintf(stderr,         "tty width:  %d\n", get_terminal_width());
+       fprintf(stderr,         "tty width:  %d\n", get_terminal_width(0));
 
        return EXIT_SUCCESS;
 }
index eca61bc04b8ff716f7d6b45f8b2be614a7097fd9..daab10a82978d05466e123f47d314146e1bdfbd9 100644 (file)
@@ -1009,9 +1009,7 @@ int scols_print_table(struct libscols_table *tb)
                scols_table_set_symbols(tb, NULL);      /* use default */
 
        tb->is_term = isatty(STDOUT_FILENO) ? 1 : 0;
-       tb->termwidth = tb->is_term ? get_terminal_width() : 0;
-       if (tb->termwidth <= 0)
-               tb->termwidth = 80;
+       tb->termwidth = tb->is_term ? get_terminal_width(80) : 0;
        tb->termwidth -= tb->termreduce;
 
        bufsz = tb->termwidth;
index c0be45746f45ba041e2ebb3665c0167916cba8a4..eca35aa889d2509a3ac0422045b62ed51630bdc8 100644 (file)
@@ -154,9 +154,7 @@ static void pretty_print_line(const char *device, const char *fs_type,
        int len, w;
 
        if (term_width < 0) {
-               term_width = get_terminal_width();
-               if (term_width <= 0)
-                       term_width = 80;
+               term_width = get_terminal_width(80);
        }
        if (term_width > 80) {
                term_width -= 80;
@@ -192,7 +190,7 @@ static void pretty_print_dev(blkid_dev dev)
        if (dev == NULL) {
                pretty_print_line("device", "fs_type", "label",
                                  "mount point", "UUID");
-               for (len=get_terminal_width()-1; len > 0; len--)
+               for (len=get_terminal_width(0)-1; len > 0; len--)
                        fputc('-', stdout);
                fputc('\n', stdout);
                return;
index 01730dbc5328598b0f1d643479db7165ca227e3a..92b2ae9cb3fb41a85f461c5f00edad4380929722 100644 (file)
@@ -218,11 +218,7 @@ static void print_all_signals(FILE *fp, int pretty)
        }
 
        /* pretty print */
-       width = get_terminal_width();
-       if (width == 0)
-               width = KILL_OUTPUT_WIDTH;
-       else
-               width -= 1;
+       width = get_terminal_width(KILL_OUTPUT_WIDTH + 1) - 1;
        for (n = 0; n < ARRAY_SIZE(sys_signame); n++)
                pretty_print_signal(fp, width, &lpos,
                                    sys_signame[n].val, sys_signame[n].name);
index 60d12312337bbbd9fa96347bf8d5d587a6f3611f..4411d2d262611f5973cd26d22e312a6e528e32ac 100644 (file)
@@ -142,9 +142,7 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       termwidth = get_terminal_width();
-       if (termwidth <= 0)
-               termwidth = 80;
+       termwidth = get_terminal_width(80);
        colsep = mbs_to_wcs("  ");
 
        while ((ch = getopt_long(argc, argv, "hVc:s:txo:", longopts, NULL)) != -1)