]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/ttyutils: return terminal lines too
authorKarel Zak <kzak@redhat.com>
Mon, 12 Jun 2017 09:38:38 +0000 (11:38 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 12 Jun 2017 09:38:38 +0000 (11:38 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/ttyutils.h
lib/ttyutils.c
libsmartcols/src/table.c

index 7278d3615d9acf3162e908d04d71b4e42dbf9c3d..a9baab34bfe2a30225ab14cabba9c94921429a5e 100644 (file)
@@ -50,6 +50,7 @@ struct chardata {
                (ptr)->capslock = 0;         \
        } while (0)
 
+extern int get_terminal_dimension(int *cols, int *lines);
 extern int get_terminal_width(int default_width);
 extern int get_terminal_name(const char **path, const char **name,
                             const char **number);
index 613e5af65dd923d2b1f8d61f8159753078d1e99f..6a1e0e0f36992233dd01d4395afe8d7dd5ce0163 100644 (file)
 #include "c.h"
 #include "ttyutils.h"
 
-int get_terminal_width(int default_width)
+
+static int get_env_int(const char *name)
 {
-       int width = 0;
+       const char *cp = getenv(name);
+
+       if (cp) {
+               char *end = NULL;
+               long x;
+
+               errno = 0;
+               x = strtol(cp, &end, 10);
+
+               if (errno == 0 && end && *end == '\0' && end > cp &&
+                   x > 0 && x <= INT_MAX)
+                       return x;
+       }
+
+       return -1;
+}
+
+int get_terminal_dimension(int *cols, int *lines)
+{
+       int c = 0, l = 0;
 
 #if defined(TIOCGWINSZ)
        struct winsize  w_win;
-       if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &w_win) == 0)
-               width = w_win.ws_col;
+       if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &w_win) == 0) {
+               c = w_win.ws_col;
+               l = w_win.ws_row;
+       }
 #elif defined(TIOCGSIZE)
        struct ttysize  t_win;
-       if (ioctl (STDOUT_FILENO, TIOCGSIZE, &t_win) == 0)
-               width = t_win.ts_cols;
+       if (ioctl (STDOUT_FILENO, TIOCGSIZE, &t_win) == 0) {
+               c = t_win.ts_cols;
+               l = t_win.ts_lines;
+       }
 #endif
 
-       if (width <= 0) {
-               const char *cp = getenv("COLUMNS");
+       if (cols && c <= 0)
+               c = get_env_int("COLUMNS");
+       if (lines && l <= 0)
+               l = get_env_int("LINES");
 
-               if (cp) {
-                       char *end = NULL;
-                       long c;
+       if (cols)
+               *cols = c;
+       if (lines)
+               *lines = l;
+       return 0;
+}
 
-                       errno = 0;
-                       c = strtol(cp, &end, 10);
+int get_terminal_width(int default_width)
+{
+       int width = 0;
 
-                       if (errno == 0 && end && *end == '\0' && end > cp &&
-                           c > 0 && c <= INT_MAX)
-                               width = c;
-               }
-       }
+       get_terminal_dimension(&width, NULL);
 
        return width > 0 ? width : default_width;
 }
@@ -93,13 +119,17 @@ int get_terminal_name(const char **path,
 int main(void)
 {
        const char *path, *name, *num;
+       int c, l;
 
        if (get_terminal_name(&path, &name, &num) == 0) {
                fprintf(stderr, "tty path:   %s\n", path);
                fprintf(stderr, "tty name:   %s\n", name);
                fprintf(stderr, "tty number: %s\n", num);
        }
-       fprintf(stderr,         "tty width:  %d\n", get_terminal_width(0));
+       get_terminal_dimension(&c, &l);
+       fprintf(stderr,         "tty cols:   %d\n", c);
+       fprintf(stderr,         "tty lines:  %d\n", l);
+
 
        return EXIT_SUCCESS;
 }
index a64a6b2a2a7b8c0da4a54b1d373e7dbfa165a42a..149648f05190956f5aaafe143e7a892380883975 100644 (file)
@@ -490,6 +490,10 @@ FILE *scols_table_get_stream(const struct libscols_table *tb)
  * The @reduce must be smaller than terminal width, otherwise it's silently
  * ignored. The reduction is not applied when STDOUT_FILENO is not terminal.
  *
+ * Note that after output initialization (scols_table_print_* calls) the width
+ * will be reduced, this behavior affects subsequenced scols_table_get_termwidth()
+ * calls.
+ *
  * Returns: 0, a negative value in case of an error.
  */
 int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce)