chiark / gitweb /
util: unify line caching and column caching
authorLennart Poettering <lennart@poettering.net>
Thu, 18 Oct 2012 22:06:47 +0000 (00:06 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Oct 2012 22:07:55 +0000 (00:07 +0200)
src/cgtop/cgtop.c
src/journal/journalctl.c
src/shared/util.c
src/shared/util.h

index ee421e383b2eec9e2314a006d79daee71033a32a..f2e62761f14b6b7cef761975bd43491b7f4f62cc 100644 (file)
@@ -443,9 +443,9 @@ static int display(Hashmap *a) {
 
         qsort(array, n, sizeof(Group*), group_compare);
 
-        rows = fd_lines(STDOUT_FILENO);
-        if (rows <= 0)
-                rows = 25;
+        rows = lines();
+        if (rows <= 10)
+                rows = 10;
 
         path_columns = columns() - 42;
         if (path_columns < 10)
@@ -653,7 +653,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        signal(SIGWINCH, columns_cache_reset);
+        signal(SIGWINCH, columns_lines_cache_reset);
 
         while (!quit) {
                 Hashmap *c;
index f4b651855771a42b7708cb1607ba43668f7b273d..d1338d2b7c5fd83af61e09ff5d4de3af7e52ad41 100644 (file)
@@ -197,7 +197,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case 'f':
                         arg_follow = true;
-                        signal(SIGWINCH, columns_cache_reset);
                         break;
 
                 case 'o':
@@ -834,6 +833,8 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 goto finish;
 
+        signal(SIGWINCH, columns_lines_cache_reset);
+
         if (arg_action == ACTION_NEW_ID128) {
                 r = generate_new_id128();
                 goto finish;
index 527a5800fec407d6cc096f66d22dc22cbfa6d7cf..ef30cb2dab15b2dfade9be5dde7d7e101fa9f31a 100644 (file)
@@ -73,6 +73,7 @@ int saved_argc = 0;
 char **saved_argv = NULL;
 
 static volatile unsigned cached_columns = 0;
+static volatile unsigned cached_lines = 0;
 
 size_t page_size(void) {
         static __thread size_t pgsz = 0;
@@ -3812,20 +3813,6 @@ unsigned columns(void) {
         return c;
 }
 
-/* intended to be used as a SIGWINCH sighandler */
-void columns_cache_reset(int signum) {
-        cached_columns = 0;
-}
-
-bool on_tty(void) {
-        static int cached_on_tty = -1;
-
-        if (_unlikely_(cached_on_tty < 0))
-                cached_on_tty = isatty(STDOUT_FILENO) > 0;
-
-        return cached_on_tty;
-}
-
 int fd_lines(int fd) {
         struct winsize ws;
         zero(ws);
@@ -3840,23 +3827,40 @@ int fd_lines(int fd) {
 }
 
 unsigned lines(void) {
-        static __thread int parsed_lines = 0;
         const char *e;
+        unsigned l;
 
-        if (_likely_(parsed_lines > 0))
-                return parsed_lines;
+        if (_likely_(cached_lines > 0))
+                return cached_lines;
 
+        l = 0;
         e = getenv("LINES");
         if (e)
-                parsed_lines = atoi(e);
+                safe_atou(e, &l);
 
-        if (parsed_lines <= 0)
-                parsed_lines = fd_lines(STDOUT_FILENO);
+        if (l <= 0)
+                l = fd_lines(STDOUT_FILENO);
 
-        if (parsed_lines <= 0)
-                parsed_lines = 25;
+        if (l <= 0)
+                l = 24;
 
-        return parsed_lines;
+        cached_lines = l;
+        return cached_lines;
+}
+
+/* intended to be used as a SIGWINCH sighandler */
+void columns_lines_cache_reset(int signum) {
+        cached_columns = 0;
+        cached_lines = 0;
+}
+
+bool on_tty(void) {
+        static int cached_on_tty = -1;
+
+        if (_unlikely_(cached_on_tty < 0))
+                cached_on_tty = isatty(STDOUT_FILENO) > 0;
+
+        return cached_on_tty;
 }
 
 int running_in_chroot(void) {
index e19f76c1ea5b79eee1fd00dffd6effbfef72299d..affb66998c1977c78b53bd8b142f2c2cf1b0bcc2 100644 (file)
@@ -384,11 +384,11 @@ int status_welcome(void);
 
 int fd_columns(int fd);
 unsigned columns(void);
-void columns_cache_reset(int _unused_ signum);
-bool on_tty(void);
-
 int fd_lines(int fd);
 unsigned lines(void);
+void columns_lines_cache_reset(int _unused_ signum);
+
+bool on_tty(void);
 
 int running_in_chroot(void);