]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: reduce var. scope and shorten the code a bit
authorDavid Tardon <dtardon@redhat.com>
Sat, 23 Jul 2022 15:07:52 +0000 (17:07 +0200)
committerDavid Tardon <dtardon@redhat.com>
Mon, 25 Jul 2022 11:08:12 +0000 (13:08 +0200)
src/systemctl/systemctl-list-units.c

index 5b0ce3854d209e17f91f446738cf4eb60c21ddb6..a6596ddd0eaa56f48fccad69ae0867492ee5f2a6 100644 (file)
@@ -350,7 +350,6 @@ static int socket_info_compare(const struct socket_info *a, const struct socket_
 
 static int output_sockets_list(struct socket_info *socket_infos, size_t cs) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        const char *on, *off;
         int r;
 
         table = table_new("listen", "type", "unit", "activates");
@@ -401,14 +400,7 @@ static int output_sockets_list(struct socket_info *socket_infos, size_t cs) {
                                 r = table_add_cell(table, NULL, TABLE_STRV, s->triggered);
                         if (r < 0)
                                 return table_log_add_error(r);
-
                 }
-
-                on = ansi_highlight();
-                off = ansi_normal();
-        } else {
-                on = ansi_highlight_red();
-                off = ansi_normal();
         }
 
         r = output_table(table);
@@ -416,6 +408,11 @@ static int output_sockets_list(struct socket_info *socket_infos, size_t cs) {
                 return r;
 
         if (arg_legend != 0) {
+                const char *on, *off;
+
+                on = cs > 0 ? ansi_highlight() : ansi_highlight_red();
+                off = ansi_normal();
+
                 printf("\n%s%zu sockets listed.%s\n", on, cs, off);
                 if (!arg_all)
                         printf("Pass --all to see loaded but inactive sockets, too.\n");
@@ -599,7 +596,6 @@ static int timer_info_compare(const struct timer_info *a, const struct timer_inf
 
 static int output_timers_list(struct timer_info *timer_infos, size_t n) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        const char *on, *off;
         int r;
 
         assert(timer_infos || n == 0);
@@ -641,19 +637,16 @@ static int output_timers_list(struct timer_info *timer_infos, size_t n) {
                         return table_log_add_error(r);
         }
 
-        if (n > 0) {
-                on = ansi_highlight();
-                off = ansi_normal();
-        } else {
-                on = ansi_highlight_red();
-                off = ansi_normal();
-        }
-
         r = output_table(table);
         if (r < 0)
                 return r;
 
         if (arg_legend != 0) {
+                const char *on, *off;
+
+                on = n > 0 ? ansi_highlight() : ansi_highlight_red();
+                off = ansi_normal();
+
                 printf("\n%s%zu timers listed.%s\n", on, n, off);
                 if (!arg_all)
                         printf("Pass --all to see loaded but inactive timers, too.\n");