various: use FOREACH_ARRAY more

Prompted by #30622
This commit is contained in:
Mike Yuan 2023-12-25 17:57:01 +08:00 committed by Yu Watanabe
parent 287a5f1cff
commit 38617c516a
3 changed files with 16 additions and 14 deletions

View file

@ -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)

View file

@ -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;
}
}

View file

@ -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/");