1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-25 10:04:04 +03:00

systemctl: reduce scope of iterator variables

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-02-05 11:33:48 +01:00
parent cb31470f40
commit deaf4b863b
10 changed files with 50 additions and 71 deletions

View File

@ -14,7 +14,7 @@ static int check_unit_generic(int code, const UnitActiveState good_states[], int
UnitActiveState active_state; UnitActiveState active_state;
sd_bus *bus; sd_bus *bus;
char **name; char **name;
int r, i; int r;
bool found = false; bool found = false;
r = acquire_bus(BUS_MANAGER, &bus); 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) if (!arg_quiet)
puts(unit_active_state_to_string(active_state)); 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) if (good_states[i] == active_state)
found = true; found = true;
} }

View File

@ -9,7 +9,7 @@
static int show_installation_targets_client_side(const char *name) { static int show_installation_targets_client_side(const char *name) {
UnitFileChange *changes = NULL; UnitFileChange *changes = NULL;
size_t n_changes = 0, i; size_t n_changes = 0;
UnitFileFlags flags; UnitFileFlags flags;
char **p; char **p;
int r; int r;
@ -22,7 +22,7 @@ static int show_installation_targets_client_side(const char *name) {
if (r < 0) if (r < 0)
return log_error_errno(r, "Failed to get file links for %s: %m", name); 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) if (changes[i].type == UNIT_FILE_UNLINK)
printf(" %s\n", changes[i].path); printf(" %s\n", changes[i].path);

View File

@ -12,11 +12,10 @@ static int list_dependencies_print(const char *name, int level, unsigned branche
_cleanup_free_ char *n = NULL; _cleanup_free_ char *n = NULL;
size_t max_len = MAX(columns(),20u); size_t max_len = MAX(columns(),20u);
size_t len = 0; size_t len = 0;
int i;
if (!arg_plain) { if (!arg_plain) {
for (i = level - 1; i >= 0; i--) { for (int i = level - 1; i >= 0; i--) {
len += 2; len += 2;
if (len > max_len - 3 && !arg_full) { if (len > max_len - 3 && !arg_full) {
printf("%s...\n",max_len % 2 ? "" : " "); printf("%s...\n",max_len % 2 ? "" : " ");

View 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) { static int output_jobs_list(sd_bus *bus, const struct job_info* jobs, unsigned n, bool skipped) {
_cleanup_(table_unrefp) Table *table = NULL; _cleanup_(table_unrefp) Table *table = NULL;
const struct job_info *j;
const char *on, *off; const char *on, *off;
int r; 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, "-"); (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")) if (streq(j->state, "running"))
on = ansi_highlight(); on = ansi_highlight();
else else

View 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) { static void free_machines_list(struct machine_info *machine_infos, int n) {
int i;
if (!machine_infos) if (!machine_infos)
return; return;
for (i = 0; i < n; i++) for (int i = 0; i < n; i++)
machine_info_clear(&machine_infos[i]); machine_info_clear(&machine_infos[i]);
free(machine_infos); free(machine_infos);
@ -150,7 +148,6 @@ static int get_machine_list(
static int output_machines_list(struct machine_info *machine_infos, unsigned n) { static int output_machines_list(struct machine_info *machine_infos, unsigned n) {
_cleanup_(table_unrefp) Table *table = NULL; _cleanup_(table_unrefp) Table *table = NULL;
struct machine_info *m;
bool state_missing = false; bool state_missing = false;
int r; int r;
@ -172,7 +169,7 @@ static int output_machines_list(struct machine_info *machine_infos, unsigned n)
(void) table_set_empty_string(table, "-"); (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; _cleanup_free_ char *mname = NULL;
const char *on_state = "", *on_failed = ""; const char *on_state = "", *on_failed = "";
bool circle = false; bool circle = false;

View 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) { int list_unit_files(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ UnitFileList *units = NULL; _cleanup_free_ UnitFileList *units = NULL;
UnitFileList *unit;
size_t size = 0; size_t size = 0;
unsigned c = 0; unsigned c = 0;
const char *state; const char *state;
@ -265,7 +264,7 @@ int list_unit_files(int argc, char *argv[], void *userdata) {
return r; return r;
if (install_client_side()) if (install_client_side())
for (unit = units; unit < units + c; unit++) for (UnitFileList *unit = units; unit < units + c; unit++)
free(unit->path); free(unit->path);
if (c == 0) if (c == 0)

View 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) { static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
_cleanup_(table_unrefp) Table *table = NULL; _cleanup_(table_unrefp) Table *table = NULL;
struct socket_info *s;
const char *on, *off; const char *on, *off;
int r; int r;
@ -373,7 +372,7 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
(void) table_set_empty_string(table, "-"); (void) table_set_empty_string(table, "-");
if (cs) { 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; _cleanup_free_ char *j = NULL;
const char *path; 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_strv_free_ char **sockets_with_suffix = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL; _cleanup_free_ UnitInfo *unit_infos = NULL;
_cleanup_free_ struct socket_info *socket_infos = NULL; _cleanup_free_ struct socket_info *socket_infos = NULL;
const UnitInfo *u;
struct socket_info *s;
unsigned cs = 0; unsigned cs = 0;
size_t size = 0; size_t size = 0;
int r, n; int r, n;
@ -454,9 +451,9 @@ int list_sockets(int argc, char *argv[], void *userdata) {
if (n < 0) if (n < 0)
return n; 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; _cleanup_strv_free_ char **listening = NULL, **triggered = NULL;
int i, c; int c;
if (!endswith(u->id, ".socket")) if (!endswith(u->id, ".socket"))
continue; continue;
@ -476,7 +473,7 @@ int list_sockets(int argc, char *argv[], void *userdata) {
goto cleanup; goto cleanup;
} }
for (i = 0; i < c; i++) for (int i = 0; i < c; i++)
socket_infos[cs + i] = (struct socket_info) { socket_infos[cs + i] = (struct socket_info) {
.machine = u->machine, .machine = u->machine,
.id = u->id, .id = u->id,
@ -499,7 +496,7 @@ int list_sockets(int argc, char *argv[], void *userdata) {
cleanup: cleanup:
assert(cs == 0 || socket_infos); 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->type);
free(s->path); free(s->path);
if (s->own_triggered) 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) { static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
_cleanup_(table_unrefp) Table *table = NULL; _cleanup_(table_unrefp) Table *table = NULL;
struct timer_info *t;
const char *on, *off; const char *on, *off;
int r; int r;
@ -620,34 +616,34 @@ static int output_timers_list(struct timer_info *timer_infos, unsigned n) {
(void) table_set_empty_string(table, "-"); (void) table_set_empty_string(table, "-");
if (n > 0) { for (struct timer_info *t = timer_infos; t < timer_infos + n; t++) {
for (t = timer_infos; t < timer_infos + n; t++) { _cleanup_free_ char *j = NULL, *activates = NULL;
_cleanup_free_ char *j = NULL, *activates = NULL; const char *unit;
const char *unit;
if (t->machine) { if (t->machine) {
j = strjoin(t->machine, ":", t->id); j = strjoin(t->machine, ":", t->id);
if (!j) if (!j)
return log_oom();
unit = j;
} else
unit = t->id;
activates = strv_join(t->triggered, ", ");
if (!activates)
return log_oom(); return log_oom();
unit = j;
} else
unit = t->id;
r = table_add_many(table, activates = strv_join(t->triggered, ", ");
TABLE_TIMESTAMP, t->next_elapse, if (!activates)
TABLE_TIMESTAMP_RELATIVE, t->next_elapse, return log_oom();
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);
}
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(); on = ansi_highlight();
off = ansi_normal(); off = ansi_normal();
} else { } else {
@ -699,8 +695,6 @@ int list_timers(int argc, char *argv[], void *userdata) {
_cleanup_strv_free_ char **timers_with_suffix = NULL; _cleanup_strv_free_ char **timers_with_suffix = NULL;
_cleanup_free_ struct timer_info *timer_infos = NULL; _cleanup_free_ struct timer_info *timer_infos = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL; _cleanup_free_ UnitInfo *unit_infos = NULL;
struct timer_info *t;
const UnitInfo *u;
size_t size = 0; size_t size = 0;
int n, c = 0; int n, c = 0;
dual_timestamp nw; dual_timestamp nw;
@ -724,7 +718,7 @@ int list_timers(int argc, char *argv[], void *userdata) {
dual_timestamp_get(&nw); 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; _cleanup_strv_free_ char **triggered = NULL;
dual_timestamp next = DUAL_TIMESTAMP_NULL; dual_timestamp next = DUAL_TIMESTAMP_NULL;
usec_t m, last = 0; usec_t m, last = 0;
@ -764,7 +758,7 @@ int list_timers(int argc, char *argv[], void *userdata) {
output_timers_list(timer_infos, c); output_timers_list(timer_infos, c);
cleanup: 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); strv_free(t->triggered);
return r; return r;

View 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")) { } else if (endswith(name, "ExitStatus") && streq(contents, "aiai")) {
const int32_t *status, *signal; 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"); r = sd_bus_message_enter_container(m, 'r', "aiai");
if (r < 0) if (r < 0)
@ -1132,7 +1132,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
fputc('=', stdout); fputc('=', stdout);
} }
for (i = 0; i < n_status; i++) { for (size_t i = 0; i < n_status; i++) {
if (first) if (first)
first = false; first = false;
else else
@ -1141,7 +1141,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
printf("%"PRIi32, status[i]); printf("%"PRIi32, status[i]);
} }
for (i = 0; i < n_signal; i++) { for (size_t i = 0; i < n_signal; i++) {
const char *str; const char *str;
str = signal_to_string((int) signal[i]); 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_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL; _cleanup_free_ UnitInfo *unit_infos = NULL;
const UnitInfo *u;
unsigned c; unsigned c;
int r, ret = 0; int r, ret = 0;
@ -1947,7 +1946,7 @@ static int show_all(
typesafe_qsort(unit_infos, c, unit_info_compare); 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; _cleanup_free_ char *p = NULL;
p = unit_dbus_path_from_name(u->id); p = unit_dbus_path_from_name(u->id);

View File

@ -36,9 +36,7 @@ static const struct {
}; };
static const char *verb_to_method(const char *verb) { static const char *verb_to_method(const char *verb) {
size_t i; for (size_t i = 0; i < ELEMENTSOF(unit_actions); i++)
for (i = 0; i < ELEMENTSOF(unit_actions); i++)
if (streq_ptr(unit_actions[i].verb, verb)) if (streq_ptr(unit_actions[i].verb, verb))
return unit_actions[i].method; 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) { static const char *verb_to_job_type(const char *verb) {
size_t i; for (size_t i = 0; i < ELEMENTSOF(unit_actions); i++)
for (i = 0; i < ELEMENTSOF(unit_actions); i++)
if (streq_ptr(unit_actions[i].verb, verb)) if (streq_ptr(unit_actions[i].verb, verb))
return unit_actions[i].job_type; 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 verb_to_action(const char *verb) {
enum action i; for (enum action i = 0; i < _ACTION_MAX; i++)
for (i = 0; i < _ACTION_MAX; i++)
if (streq_ptr(action_table[i].verb, verb)) if (streq_ptr(action_table[i].verb, verb))
return i; return i;

View File

@ -62,9 +62,7 @@ int acquire_bus(BusFocus focus, sd_bus **ret) {
} }
void release_busses(void) { void release_busses(void) {
BusFocus w; for (BusFocus w = 0; w < _BUS_FOCUS_MAX; w++)
for (w = 0; w < _BUS_FOCUS_MAX; w++)
buses[w] = sd_bus_flush_close_unref(buses[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) { 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; _cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
char **name; char **name;
int r, i; int r;
assert(bus); assert(bus);
assert(ret); assert(ret);
@ -272,7 +270,7 @@ int expand_unit_names(sd_bus *bus, char **names, const char* suffix, char ***ret
n = strv_length(mangled); n = strv_length(mangled);
allocated = n + 1; allocated = n + 1;
for (i = 0; i < r; i++) { for (int i = 0; i < r; i++) {
if (!GREEDY_REALLOC(mangled, allocated, n+2)) if (!GREEDY_REALLOC(mangled, allocated, n+2))
return log_oom(); return log_oom();