From 38617c516a7132cbf43a7ce75c1e9c037e513c5e Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Mon, 25 Dec 2023 17:57:01 +0800 Subject: [PATCH] various: use FOREACH_ARRAY more Prompted by #30622 --- src/core/dbus-job.c | 15 ++++++++------- src/shared/compare-operator.c | 11 ++++++----- src/shared/mount-setup.c | 4 ++-- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c index 56ad88570f1..d5b50e85183 100644 --- a/src/core/dbus-job.c +++ b/src/core/dbus-job.c @@ -87,22 +87,23 @@ int bus_job_method_get_waiting_jobs(sd_bus_message *message, void *userdata, sd_ if (r < 0) return r; - for (int i = 0; i < n; i++) { + FOREACH_ARRAY(i, list, n) { _cleanup_free_ char *unit_path = NULL, *job_path = NULL; + Job *job = *i; - job_path = job_dbus_path(list[i]); + job_path = job_dbus_path(job); if (!job_path) return -ENOMEM; - unit_path = unit_dbus_path(list[i]->unit); + unit_path = unit_dbus_path(job->unit); if (!unit_path) return -ENOMEM; r = sd_bus_message_append(reply, "(usssoo)", - list[i]->id, - list[i]->unit->id, - job_type_to_string(list[i]->type), - job_state_to_string(list[i]->state), + job->id, + job->unit->id, + job_type_to_string(job->type), + job_state_to_string(job->state), job_path, unit_path); if (r < 0) diff --git a/src/shared/compare-operator.c b/src/shared/compare-operator.c index 233e9c6b006..6df4be8333b 100644 --- a/src/shared/compare-operator.c +++ b/src/shared/compare-operator.c @@ -6,6 +6,7 @@ #include "string-util.h" CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags flags) { + static const struct { CompareOperator op; const char *str; @@ -40,19 +41,19 @@ CompareOperator parse_compare_operator(const char **s, CompareOperatorParseFlags * parse_compare_operator() are use on the same string? */ return _COMPARE_OPERATOR_INVALID; - for (size_t i = 0; i < ELEMENTSOF(table); i++) { + FOREACH_ARRAY(i, table, ELEMENTSOF(table)) { const char *e; - if (table[i].need_mask != 0 && !FLAGS_SET(flags, table[i].need_mask)) + if (i->need_mask != 0 && !FLAGS_SET(flags, i->need_mask)) continue; - e = startswith(*s, table[i].str); + e = startswith(*s, i->str); if (e) { - if (table[i].valid_mask != 0 && !FLAGS_SET(flags, table[i].valid_mask)) + if (i->valid_mask != 0 && !FLAGS_SET(flags, i->valid_mask)) return _COMPARE_OPERATOR_INVALID; *s = e; - return table[i].op; + return i->op; } } diff --git a/src/shared/mount-setup.c b/src/shared/mount-setup.c index d9de2789a58..a602f6f1e25 100644 --- a/src/shared/mount-setup.c +++ b/src/shared/mount-setup.c @@ -135,8 +135,8 @@ bool mount_point_is_api(const char *path) { /* Checks if this mount point is considered "API", and hence * should be ignored */ - for (size_t i = 0; i < ELEMENTSOF(mount_table); i++) - if (path_equal(path, mount_table[i].where)) + FOREACH_ARRAY(i, mount_table, ELEMENTSOF(mount_table)) + if (path_equal(path, i->where)) return true; return path_startswith(path, "/sys/fs/cgroup/");