tree-wide: replace for loop with FOREACH_ELEMENT or FOREACH_ARRAY macros (#34893)

This commit is contained in:
Integral
2024-10-26 07:10:22 +09:00
committed by GitHub
parent f7078de515
commit ddb8a639d5
46 changed files with 249 additions and 269 deletions
+3 -3
View File
@@ -130,9 +130,9 @@ Architecture uname_architecture(void) {
assert_se(uname(&u) >= 0);
for (size_t i = 0; i < ELEMENTSOF(arch_map); i++)
if (streq(arch_map[i].machine, u.machine))
return cached = arch_map[i].arch;
FOREACH_ELEMENT(entry, arch_map)
if (streq(entry->machine, u.machine))
return cached = entry->arch;
assert_not_reached();
return _ARCHITECTURE_INVALID;
+3 -3
View File
@@ -71,13 +71,13 @@ int proc_cmdline_filter_pid1_args(char **argv, char ***ret) {
}
/* long option, e.g. --foo */
for (size_t i = 0; i < ELEMENTSOF(options); i++) {
const char *q = startswith(a + 2, options[i].name);
FOREACH_ELEMENT(option, options) {
const char *q = startswith(a + 2, option->name);
if (!q || !IN_SET(q[0], '=', '\0'))
continue;
/* Found matching option, updating the state if necessary. */
if (q[0] == '\0' && options[i].has_arg == required_argument)
if (q[0] == '\0' && option->has_arg == required_argument)
state = required_argument;
break;
+16 -17
View File
@@ -556,7 +556,7 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
/* The result of this function can be parsed with parse_sec */
for (size_t i = 0; i < ELEMENTSOF(table); i++) {
FOREACH_ELEMENT(i, table) {
int k = 0;
size_t n;
bool done = false;
@@ -568,20 +568,20 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
if (t < accuracy && something)
break;
if (t < table[i].usec)
if (t < i->usec)
continue;
if (l <= 1)
break;
a = t / table[i].usec;
b = t % table[i].usec;
a = t / i->usec;
b = t % i->usec;
/* Let's see if we should shows this in dot notation */
if (t < USEC_PER_MINUTE && b > 0) {
signed char j = 0;
for (usec_t cc = table[i].usec; cc > 1; cc /= 10)
for (usec_t cc = i->usec; cc > 1; cc /= 10)
j++;
for (usec_t cc = accuracy; cc > 1; cc /= 10) {
@@ -596,7 +596,7 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
a,
j,
b,
table[i].suffix);
i->suffix);
t = 0;
done = true;
@@ -609,7 +609,7 @@ char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
"%s"USEC_FMT"%s",
p > buf ? " " : "",
a,
table[i].suffix);
i->suffix);
t = b;
}
@@ -795,12 +795,12 @@ static int parse_timestamp_impl(
goto from_tm;
}
for (size_t i = 0; i < ELEMENTSOF(day_nr); i++) {
k = startswith_no_case(t, day_nr[i].name);
FOREACH_ELEMENT(day, day_nr) {
k = startswith_no_case(t, day->name);
if (!k || *k != ' ')
continue;
weekday = day_nr[i].nr;
weekday = day->nr;
t = k + 1;
break;
}
@@ -1122,12 +1122,12 @@ static const char* extract_multiplier(const char *p, usec_t *ret) {
assert(p);
assert(ret);
for (size_t i = 0; i < ELEMENTSOF(table); i++) {
FOREACH_ELEMENT(i, table) {
char *e;
e = startswith(p, table[i].suffix);
e = startswith(p, i->suffix);
if (e) {
*ret = table[i].usec;
*ret = i->usec;
return e;
}
}
@@ -1300,17 +1300,16 @@ static const char* extract_nsec_multiplier(const char *p, nsec_t *ret) {
{ "ns", 1ULL },
{ "", 1ULL }, /* default is nsec */
};
size_t i;
assert(p);
assert(ret);
for (i = 0; i < ELEMENTSOF(table); i++) {
FOREACH_ELEMENT(i, table) {
char *e;
e = startswith(p, table[i].suffix);
e = startswith(p, i->suffix);
if (e) {
*ret = table[i].nsec;
*ret = i->nsec;
return e;
}
}
+10 -10
View File
@@ -83,10 +83,10 @@ static Virtualization detect_vm_cpuid(void) {
log_debug("Virtualization found, CPUID=%s", sig.text);
for (size_t i = 0; i < ELEMENTSOF(vm_table); i++)
FOREACH_ELEMENT(vm, vm_table)
if (memcmp_nn(sig.text, sizeof(sig.text),
vm_table[i].sig, sizeof(vm_table[i].sig)) == 0)
return vm_table[i].id;
vm->sig, sizeof(vm->sig)) == 0)
return vm->id;
log_debug("Unknown virtualization with CPUID=%s. Add to vm_table[]?", sig.text);
return VIRTUALIZATION_VM_OTHER;
@@ -202,10 +202,10 @@ static Virtualization detect_vm_dmi_vendor(void) {
return r;
}
for (size_t i = 0; i < ELEMENTSOF(dmi_vendor_table); i++)
if (startswith(s, dmi_vendor_table[i].vendor)) {
FOREACH_ELEMENT(dmi_vendor, dmi_vendor_table)
if (startswith(s, dmi_vendor->vendor)) {
log_debug("Virtualization %s found in DMI (%s)", s, *vendor);
return dmi_vendor_table[i].id;
return dmi_vendor->id;
}
}
log_debug("No virtualization found in DMI vendor table.");
@@ -658,14 +658,14 @@ static Virtualization detect_container_files(void) {
{ "/.dockerenv", VIRTUALIZATION_DOCKER },
};
for (size_t i = 0; i < ELEMENTSOF(container_file_table); i++) {
if (access(container_file_table[i].file_path, F_OK) >= 0)
return container_file_table[i].id;
FOREACH_ELEMENT(file, container_file_table) {
if (access(file->file_path, F_OK) >= 0)
return file->id;
if (errno != ENOENT)
log_debug_errno(errno,
"Checking if %s exists failed, ignoring: %m",
container_file_table[i].file_path);
file->file_path);
}
return VIRTUALIZATION_NONE;