]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: reduce scope of iterator variables
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 5 Feb 2021 10:33:48 +0000 (11:33 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 15 Feb 2021 19:49:14 +0000 (20:49 +0100)
src/systemctl/systemctl-is-active.c
src/systemctl/systemctl-is-enabled.c
src/systemctl/systemctl-list-dependencies.c
src/systemctl/systemctl-list-jobs.c
src/systemctl/systemctl-list-machines.c
src/systemctl/systemctl-list-unit-files.c
src/systemctl/systemctl-list-units.c
src/systemctl/systemctl-show.c
src/systemctl/systemctl-start-unit.c
src/systemctl/systemctl-util.c

index 3d99b0deb60f2b6411bc1654ed32cfa3010e9fdd..d83736e94aa22dd4112280e1b81c46227c22c7ea 100644 (file)
@@ -14,7 +14,7 @@ static int check_unit_generic(int code, const UnitActiveState good_states[], int
         UnitActiveState active_state;
         sd_bus *bus;
         char **name;
-        int r, i;
+        int r;
         bool found = false;
 
         r = acquire_bus(BUS_MANAGER, &bus);
@@ -33,7 +33,7 @@ static int check_unit_generic(int code, const UnitActiveState good_states[], int
                 if (!arg_quiet)
                         puts(unit_active_state_to_string(active_state));
 
-                for (i = 0; i < nb_states; ++i)
+                for (int i = 0; i < nb_states; ++i)
                         if (good_states[i] == active_state)
                                 found = true;
         }
index babd5902c9855330e8107042ca9a65bb7c604088..de1d016bb3b3ebbadcc4e1990cebdf231bfe35af 100644 (file)
@@ -9,7 +9,7 @@
 
 static int show_installation_targets_client_side(const char *name) {
         UnitFileChange *changes = NULL;
-        size_t n_changes = 0, i;
+        size_t n_changes = 0;
         UnitFileFlags flags;
         char **p;
         int r;
@@ -22,7 +22,7 @@ static int show_installation_targets_client_side(const char *name) {
         if (r < 0)
                 return log_error_errno(r, "Failed to get file links for %s: %m", name);
 
-        for (i = 0; i < n_changes; i++)
+        for (size_t i = 0; i < n_changes; i++)
                 if (changes[i].type == UNIT_FILE_UNLINK)
                         printf("  %s\n", changes[i].path);
 
index fbb81d90fa2fa3196a6e0053c2277ae395f7c494..5d19f338d87dedc4b96ad57295e2ac2b4d1ee77f 100644 (file)
@@ -12,11 +12,10 @@ static int list_dependencies_print(const char *name, int level, unsigned branche
         _cleanup_free_ char *n = NULL;
         size_t max_len = MAX(columns(),20u);
         size_t len = 0;
-        int i;
 
         if (!arg_plain) {
 
-                for (i = level - 1; i >= 0; i--) {
+                for (int i = level - 1; i >= 0; i--) {
                         len += 2;
                         if (len > max_len - 3 && !arg_full) {
                                 printf("%s...\n",max_len % 2 ? "" : " ");
index 8b028c013aef23591a940c33baec72c6da4966c3..27eecb548ca9f8f9b349cf66a29a1e91eb27746a 100644 (file)
@@ -58,7 +58,6 @@ struct job_info {
 
 static int output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        const struct job_info *j;
         const char *on, *off;
         int r;
 
@@ -86,7 +85,7 @@ static int output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n
 
         (void) table_set_empty_string(table, "-");
 
-        for (j = jobs; j < jobs + n; j++) {
+        for (const struct job_info *j = jobs; j < jobs + n; j++) {
                 if (streq(j->state, "running"))
                         on = ansi_highlight();
                 else
index 48d0e8bde477673b53b4be38ccbaa05b5bceebe3..63adcec5f34fb4f06cae6346d12ba9c523dc3059 100644 (file)
@@ -33,12 +33,10 @@ void machine_info_clear(struct machine_info *info) {
 }
 
 static void free_machines_list(struct machine_info *machine_infos, int n) {
-        int i;
-
         if (!machine_infos)
                 return;
 
-        for (i = 0; i < n; i++)
+        for (int i = 0; i < n; i++)
                 machine_info_clear(&machine_infos[i]);
 
         free(machine_infos);
@@ -150,7 +148,6 @@ static int get_machine_list(
 
 static int output_machines_list(struct machine_info *machine_infos, unsigned n) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        struct machine_info *m;
         bool state_missing = false;
         int r;
 
@@ -172,7 +169,7 @@ static int output_machines_list(struct machine_info *machine_infos, unsigned n)
 
         (void) table_set_empty_string(table, "-");
 
-        for (m = machine_infos; m < machine_infos + n; m++) {
+        for (struct machine_info *m = machine_infos; m < machine_infos + n; m++) {
                 _cleanup_free_ char *mname = NULL;
                 const char *on_state = "", *on_failed = "";
                 bool circle = false;
index e1bf8766205b1fa4710079d037a299aa0c7f2b19..6da2914eff1b0a355f60f66adecb884faaa5157b 100644 (file)
@@ -136,7 +136,6 @@ static int output_unit_file_list(const UnitFileList *units, unsigned c) {
 int list_unit_files(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         _cleanup_free_ UnitFileList *units = NULL;
-        UnitFileList *unit;
         size_t size = 0;
         unsigned c = 0;
         const char *state;
@@ -265,7 +264,7 @@ int list_unit_files(int argc, char *argv[], void *userdata) {
                 return r;
 
         if (install_client_side())
-                for (unit = units; unit < units + c; unit++)
+                for (UnitFileList *unit = units; unit < units + c; unit++)
                         free(unit->path);
 
         if (c == 0)
index 7f0e79eedd6c34242c5d38d777c9299652d9875f..a413ef6d5b22bbbd7af19d8d870eae125caa62bc 100644 (file)
@@ -351,7 +351,6 @@ static int socket_info_compare(const struct socket_info *a, const struct socket_
 
 static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        struct socket_info *s;
         const char *on, *off;
         int r;
 
@@ -373,7 +372,7 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
         (void) table_set_empty_string(table, "-");
 
         if (cs) {
-                for (s = socket_infos; s < socket_infos + cs; s++) {
+                for (struct socket_info *s = socket_infos; s < socket_infos + cs; s++) {
                         _cleanup_free_ char *j = NULL;
                         const char *path;
 
@@ -432,8 +431,6 @@ int list_sockets(int argc, char *argv[], void *userdata) {
         _cleanup_strv_free_ char **sockets_with_suffix = NULL;
         _cleanup_free_ UnitInfo *unit_infos = NULL;
         _cleanup_free_ struct socket_info *socket_infos = NULL;
-        const UnitInfo *u;
-        struct socket_info *s;
         unsigned cs = 0;
         size_t size = 0;
         int r, n;
@@ -454,9 +451,9 @@ int list_sockets(int argc, char *argv[], void *userdata) {
                 if (n < 0)
                         return n;
 
-                for (u = unit_infos; u < unit_infos + n; u++) {
+                for (const UnitInfo *u = unit_infos; u < unit_infos + n; u++) {
                         _cleanup_strv_free_ char **listening = NULL, **triggered = NULL;
-                        int i, c;
+                        int c;
 
                         if (!endswith(u->id, ".socket"))
                                 continue;
@@ -476,7 +473,7 @@ int list_sockets(int argc, char *argv[], void *userdata) {
                                 goto cleanup;
                         }
 
-                        for (i = 0; i < c; i++)
+                        for (int i = 0; i < c; i++)
                                 socket_infos[cs + i] = (struct socket_info) {
                                         .machine = u->machine,
                                         .id = u->id,
@@ -499,7 +496,7 @@ int list_sockets(int argc, char *argv[], void *userdata) {
 
  cleanup:
         assert(cs == 0 || socket_infos);
-        for (s = socket_infos; s < socket_infos + cs; s++) {
+        for (struct socket_info *s = socket_infos; s < socket_infos + cs; s++) {
                 free(s->type);
                 free(s->path);
                 if (s->own_triggered)
@@ -604,7 +601,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, unsigned n) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        struct timer_info *t;
         const char *on, *off;
         int r;
 
@@ -620,34 +616,34 @@ static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
 
         (void) table_set_empty_string(table, "-");
 
-        if (n > 0) {
-                for (t = timer_infos; t < timer_infos + n; t++) {
-                        _cleanup_free_ char *j = NULL, *activates = NULL;
-                        const char *unit;
-
-                        if (t->machine) {
-                                j = strjoin(t->machine, ":", t->id);
-                                if (!j)
-                                        return log_oom();
-                                unit = j;
-                        } else
-                                unit = t->id;
+        for (struct timer_info *t = timer_infos; t < timer_infos + n; t++) {
+                _cleanup_free_ char *j = NULL, *activates = NULL;
+                const char *unit;
 
-                        activates = strv_join(t->triggered, ", ");
-                        if (!activates)
+                if (t->machine) {
+                        j = strjoin(t->machine, ":", t->id);
+                        if (!j)
                                 return log_oom();
+                        unit = j;
+                } else
+                        unit = t->id;
 
-                        r = table_add_many(table,
-                                           TABLE_TIMESTAMP, t->next_elapse,
-                                           TABLE_TIMESTAMP_RELATIVE, t->next_elapse,
-                                           TABLE_TIMESTAMP, t->last_trigger,
-                                           TABLE_TIMESTAMP_RELATIVE, t->last_trigger,
-                                           TABLE_STRING, unit,
-                                           TABLE_STRING, activates);
-                        if (r < 0)
-                                return table_log_add_error(r);
-                }
+                activates = strv_join(t->triggered, ", ");
+                if (!activates)
+                        return log_oom();
 
+                r = table_add_many(table,
+                                   TABLE_TIMESTAMP, t->next_elapse,
+                                   TABLE_TIMESTAMP_RELATIVE, t->next_elapse,
+                                   TABLE_TIMESTAMP, t->last_trigger,
+                                   TABLE_TIMESTAMP_RELATIVE, t->last_trigger,
+                                   TABLE_STRING, unit,
+                                   TABLE_STRING, activates);
+                if (r < 0)
+                        return table_log_add_error(r);
+        }
+
+        if (n > 0) {
                 on = ansi_highlight();
                 off = ansi_normal();
         } else {
@@ -699,8 +695,6 @@ int list_timers(int argc, char *argv[], void *userdata) {
         _cleanup_strv_free_ char **timers_with_suffix = NULL;
         _cleanup_free_ struct timer_info *timer_infos = NULL;
         _cleanup_free_ UnitInfo *unit_infos = NULL;
-        struct timer_info *t;
-        const UnitInfo *u;
         size_t size = 0;
         int n, c = 0;
         dual_timestamp nw;
@@ -724,7 +718,7 @@ int list_timers(int argc, char *argv[], void *userdata) {
 
                 dual_timestamp_get(&nw);
 
-                for (u = unit_infos; u < unit_infos + n; u++) {
+                for (const UnitInfo *u = unit_infos; u < unit_infos + n; u++) {
                         _cleanup_strv_free_ char **triggered = NULL;
                         dual_timestamp next = DUAL_TIMESTAMP_NULL;
                         usec_t m, last = 0;
@@ -764,7 +758,7 @@ int list_timers(int argc, char *argv[], void *userdata) {
         output_timers_list(timer_infos, c);
 
  cleanup:
-        for (t = timer_infos; t < timer_infos + c; t++)
+        for (struct timer_info *t = timer_infos; t < timer_infos + c; t++)
                 strv_free(t->triggered);
 
         return r;
index 9b04b698be1160de91eb1d7fefbc5361bbbb5d0d..57e708095da849d3241b26e244f3358d2a202c8f 100644 (file)
@@ -1103,7 +1103,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
 
                 } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) {
                         const int32_t *status, *signal;
-                        size_t n_status, n_signal, i;
+                        size_t n_status, n_signal;
 
                         r = sd_bus_message_enter_container(m, 'r', "aiai");
                         if (r < 0)
@@ -1132,7 +1132,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                         fputc('=', stdout);
                                 }
 
-                                for (i = 0; i < n_status; i++) {
+                                for (size_t i = 0; i < n_status; i++) {
                                         if (first)
                                                 first = false;
                                         else
@@ -1141,7 +1141,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
                                         printf("%"PRIi32, status[i]);
                                 }
 
-                                for (i = 0; i < n_signal; i++) {
+                                for (size_t i = 0; i < n_signal; i++) {
                                         const char *str;
 
                                         str = signal_to_string((int) signal[i]);
@@ -1933,7 +1933,6 @@ static int show_all(
 
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         _cleanup_free_ UnitInfo *unit_infos = NULL;
-        const UnitInfo *u;
         unsigned c;
         int r, ret = 0;
 
@@ -1947,7 +1946,7 @@ static int show_all(
 
         typesafe_qsort(unit_infos, c, unit_info_compare);
 
-        for (u = unit_infos; u < unit_infos + c; u++) {
+        for (const UnitInfo *u = unit_infos; u < unit_infos + c; u++) {
                 _cleanup_free_ char *p = NULL;
 
                 p = unit_dbus_path_from_name(u->id);
index b398e77eb281f0f65dcb4da46a6134c19fccdcdf..d08e363c9fa006a959ba1eca13ed9c410b6a840a 100644 (file)
@@ -36,9 +36,7 @@ static const struct {
 };
 
 static const char *verb_to_method(const char *verb) {
-       size_t i;
-
-       for (i = 0; i < ELEMENTSOF(unit_actions); i++)
+       for (size_t i = 0; i < ELEMENTSOF(unit_actions); i++)
                 if (streq_ptr(unit_actions[i].verb, verb))
                         return unit_actions[i].method;
 
@@ -46,9 +44,7 @@ static const char *verb_to_method(const char *verb) {
 }
 
 static const char *verb_to_job_type(const char *verb) {
-       size_t i;
-
-       for (i = 0; i < ELEMENTSOF(unit_actions); i++)
+       for (size_t i = 0; i < ELEMENTSOF(unit_actions); i++)
                 if (streq_ptr(unit_actions[i].verb, verb))
                         return unit_actions[i].job_type;
 
@@ -200,9 +196,7 @@ const struct action_metadata action_table[_ACTION_MAX] = {
 };
 
 enum action verb_to_action(const char *verb) {
-        enum action i;
-
-        for (i = 0; i < _ACTION_MAX; i++)
+        for (enum action i = 0; i < _ACTION_MAX; i++)
                 if (streq_ptr(action_table[i].verb, verb))
                         return i;
 
index 89c342a0b10c0349a447b79f0bafaf3ad1a5cdd2..d1490c906d080a0f79023cd4ebfeb3c3344e682f 100644 (file)
@@ -62,9 +62,7 @@ int acquire_bus(BusFocus focus, sd_bus **ret) {
 }
 
 void release_busses(void) {
-        BusFocus w;
-
-        for (w = 0; w < _BUS_FOCUS_MAX; w++)
+        for (BusFocus w = 0; w < _BUS_FOCUS_MAX; w++)
                 buses[w] = sd_bus_flush_close_unref(buses[w]);
 }
 
@@ -237,7 +235,7 @@ int get_unit_list(
 int expand_unit_names(sd_bus *bus, char **names, const char* suffix, char ***ret, bool *ret_expanded) {
         _cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
         char **name;
-        int r, i;
+        int r;
 
         assert(bus);
         assert(ret);
@@ -272,7 +270,7 @@ int expand_unit_names(sd_bus *bus, char **names, const char* suffix, char ***ret
                 n = strv_length(mangled);
                 allocated = n + 1;
 
-                for (i = 0; i < r; i++) {
+                for (int i = 0; i < r; i++) {
                         if (!GREEDY_REALLOC(mangled, allocated, n+2))
                                 return log_oom();