strv: declare iterator of FOREACH_STRING() in the loop

Same idea as 03677889f0.

No functional change intended. The type of the iterator is generally changed to
be 'const char*' instead of 'char*'. Despite the type commonly used, modifying
the string was not allowed.

I adjusted the naming of some short variables for clarity and reduced the scope
of some variable declarations in code that was being touched anyway.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2022-03-23 11:50:18 +01:00
parent 5b39139582
commit 5980d46304
53 changed files with 66 additions and 146 deletions
+4 -4
View File
@@ -318,7 +318,7 @@ static int run_editor(char **paths) {
if (r == 0) {
char **editor_args = NULL;
size_t n_editor_args = 0, i = 1, argc;
const char **args, *editor, *p;
const char **args, *editor;
argc = strv_length(paths)/2 + 1;
@@ -356,9 +356,9 @@ static int run_editor(char **paths) {
if (n_editor_args > 0)
execvp(args[0], (char* const*) args);
FOREACH_STRING(p, "editor", "nano", "vim", "vi") {
args[0] = p;
execvp(p, (char* const*) args);
FOREACH_STRING(name, "editor", "nano", "vim", "vi") {
args[0] = name;
execvp(name, (char* const*) args);
/* We do not fail if the editor doesn't exist because we want to try each one of them
* before failing. */
if (errno != ENOENT) {
+12 -13
View File
@@ -18,9 +18,8 @@
int talk_initctl(char rl) {
#if HAVE_SYSV_COMPAT
struct init_request request;
_cleanup_close_ int fd = -1;
const char *p;
const char *path;
int r;
/* Try to switch to the specified SysV runlevel. Returns == 0 if the operation does not apply on this
@@ -29,19 +28,19 @@ int talk_initctl(char rl) {
if (rl == 0)
return 0;
FOREACH_STRING(p, "/run/initctl", "/dev/initctl") {
fd = open(p, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd >= 0 || errno != ENOENT)
FOREACH_STRING(_path, "/run/initctl", "/dev/initctl") {
path = _path;
fd = open(path, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd < 0 && errno != ENOENT)
return log_error_errno(errno, "Failed to open %s: %m", path);
if (fd >= 0)
break;
}
if (fd < 0) {
if (errno == ENOENT)
return 0;
if (fd < 0)
return 0;
return log_error_errno(errno, "Failed to open initctl fifo: %m");
}
request = (struct init_request) {
struct init_request request = {
.magic = INIT_MAGIC,
.sleeptime = 0,
.cmd = INIT_CMD_RUNLVL,
@@ -50,7 +49,7 @@ int talk_initctl(char rl) {
r = loop_write(fd, &request, sizeof(request), false);
if (r < 0)
return log_error_errno(r, "Failed to write to %s: %m", p);
return log_error_errno(r, "Failed to write to %s: %m", path);
return 1;
#else