Hi, How about reducing some more variable scoping like Bruno's patch in Gnulib [1]?
I've attatched some patches covering the simpler cases. Collin [1] https://lists.gnu.org/archive/html/bug-gnulib/2025-12/msg00039.html
>From 597dfa0d310cc3c0449d08cd9d6d475473af5dad Mon Sep 17 00:00:00 2001 Message-ID: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 14:55:18 -0800 Subject: [PATCH 01/10] maint: basename: initialize values while declaring * src/basename.c (remove_suffix): Initialize values on the same line as their declarations. --- src/basename.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/basename.c b/src/basename.c index 13eaf64dd..3455ee65c 100644 --- a/src/basename.c +++ b/src/basename.c @@ -83,11 +83,8 @@ Examples:\n\ static void remove_suffix (char *name, char const *suffix) { - char *np; - char const *sp; - - np = name + strlen (name); - sp = suffix + strlen (suffix); + char *np = name + strlen (name); + char const *sp = suffix + strlen (suffix); while (np > name && sp > suffix) if (*--np != *--sp) -- 2.52.0
>From efd9a48ca508879a8b286d3b96118c3fb4942914 Mon Sep 17 00:00:00 2001 Message-ID: <efd9a48ca508879a8b286d3b96118c3fb4942914.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 14:59:19 -0800 Subject: [PATCH 02/10] maint: logname: reduce variable scope * src/logname.c (main): Declare variables where they are used instead of at the start of the function. --- src/logname.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/logname.c b/src/logname.c index 5a725e448..0bdf298fb 100644 --- a/src/logname.c +++ b/src/logname.c @@ -49,8 +49,6 @@ Print the user's login name.\n\ int main (int argc, char **argv) { - char *cp; - initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); @@ -71,7 +69,7 @@ main (int argc, char **argv) /* POSIX requires using getlogin (or equivalent code) and prohibits using a fallback technique. */ - cp = getlogin (); + char const *cp = getlogin (); if (! cp) error (EXIT_FAILURE, 0, _("no login name")); -- 2.52.0
>From b8abde0fe4a4be7d987cf56c4d5ff594e2dad3c2 Mon Sep 17 00:00:00 2001 Message-ID: <b8abde0fe4a4be7d987cf56c4d5ff594e2dad3c2.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:03:00 -0800 Subject: [PATCH 03/10] maint: whoami: reduce variable scope * src/whoami.c (main): Declare variables where they are used instead of at the start of the function. --- src/whoami.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/whoami.c b/src/whoami.c index 9313eaf3e..47c7c55b8 100644 --- a/src/whoami.c +++ b/src/whoami.c @@ -55,8 +55,6 @@ Same as id -un.\n\ int main (int argc, char **argv) { - struct passwd *pw; - uid_t uid; uid_t NO_UID = -1; initialize_main (&argc, &argv); @@ -78,8 +76,8 @@ main (int argc, char **argv) } errno = 0; - uid = geteuid (); - pw = uid == NO_UID && errno ? nullptr : getpwuid (uid); + uid_t uid = geteuid (); + struct passwd *pw = uid == NO_UID && errno ? nullptr : getpwuid (uid); if (!pw) error (EXIT_FAILURE, errno, _("cannot find name for user ID %ju"), (uintmax_t) uid); -- 2.52.0
>From dd2a113f5fdc4ff611e57dff1b75555668cb451f Mon Sep 17 00:00:00 2001 Message-ID: <dd2a113f5fdc4ff611e57dff1b75555668cb451f.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:06:24 -0800 Subject: [PATCH 04/10] maint: nproc: reduce variable scope * src/nproc.c (main): Declare variables where they are used instead of at the start of the function. --- src/nproc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nproc.c b/src/nproc.c index ec196297a..795458aa0 100644 --- a/src/nproc.c +++ b/src/nproc.c @@ -74,7 +74,7 @@ which may be less than the number of online processors\n\ int main (int argc, char **argv) { - unsigned long nproc, ignore = 0; + unsigned long ignore = 0; initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); @@ -116,7 +116,7 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } - nproc = num_processors (mode); + unsigned long nproc = num_processors (mode); if (ignore < nproc) nproc -= ignore; -- 2.52.0
>From 2880332ada4d0239a313d42f686d4162e1bb4424 Mon Sep 17 00:00:00 2001 Message-ID: <2880332ada4d0239a313d42f686d4162e1bb4424.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:11:55 -0800 Subject: [PATCH 05/10] maint: hostid: reduce variable scope * src/hostid.c (main): Declare variables where they are used instead of at the start of the function. --- src/hostid.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/hostid.c b/src/hostid.c index ff30079a7..92c252847 100644 --- a/src/hostid.c +++ b/src/hostid.c @@ -52,8 +52,6 @@ Print the numeric identifier (in hexadecimal) for the current host.\n\ int main (int argc, char **argv) { - unsigned int id; - initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); @@ -72,12 +70,10 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } - id = gethostid (); - /* POSIX says gethostid returns a "32-bit identifier" but is silent whether it's sign-extended. Turn off any sign-extension. This is a no-op unless unsigned int is wider than 32 bits. */ - id &= 0xffffffff; + unsigned int id = gethostid () & 0xffffffff; printf ("%08x\n", id); -- 2.52.0
>From 8af2d813b9a8e8741dd1c28b143b231d783d7221 Mon Sep 17 00:00:00 2001 Message-ID: <8af2d813b9a8e8741dd1c28b143b231d783d7221.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:17:21 -0800 Subject: [PATCH 06/10] maint: dirname: reduce variable scope * src/dirname.c (main): Declare variables where they are used instead of at the start of the function. Prefer idx_t to size_t. --- src/dirname.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/dirname.c b/src/dirname.c index 5d5b437eb..0b960c114 100644 --- a/src/dirname.c +++ b/src/dirname.c @@ -76,10 +76,7 @@ Examples:\n\ int main (int argc, char **argv) { - static char const dot = '.'; bool use_nuls = false; - char const *result; - size_t len; initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -118,11 +115,12 @@ main (int argc, char **argv) for (; optind < argc; optind++) { - result = argv[optind]; - len = dir_len (result); + char const *result = argv[optind]; + idx_t len = dir_len (result); if (! len) { + static char const dot = '.'; result = ˙ len = 1; } -- 2.52.0
>From 881ac237befe005593e99060f47867da32448d5d Mon Sep 17 00:00:00 2001 Message-ID: <881ac237befe005593e99060f47867da32448d5d.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:27:29 -0800 Subject: [PATCH 07/10] maint: pwd: reduce variable scope * src/pwd.c (find_dir_entry, robust_getcwd): Declare variables where they are used. (logical_getcwd, main): Likewise. Constify the result of getenv, since the result cannot be modified. --- src/pwd.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/pwd.c b/src/pwd.c index 50f98cff2..a969b3738 100644 --- a/src/pwd.c +++ b/src/pwd.c @@ -151,36 +151,29 @@ static void find_dir_entry (struct stat *dot_sb, struct file_name *file_name, size_t parent_height) { - DIR *dirp; - int fd; - struct stat parent_sb; - bool use_lstat; - bool found; - - dirp = opendir (".."); + DIR *dirp = opendir (".."); if (dirp == nullptr) error (EXIT_FAILURE, errno, _("cannot open directory %s"), quote (nth_parent (parent_height))); - fd = dirfd (dirp); + int fd = dirfd (dirp); if ((0 <= fd ? fchdir (fd) : chdir ("..")) < 0) error (EXIT_FAILURE, errno, _("failed to chdir to %s"), quote (nth_parent (parent_height))); + struct stat parent_sb; if ((0 <= fd ? fstat (fd, &parent_sb) : stat (".", &parent_sb)) < 0) error (EXIT_FAILURE, errno, _("failed to stat %s"), quote (nth_parent (parent_height))); /* If parent and child directory are on different devices, then we can't rely on d_ino for useful i-node numbers; use lstat instead. */ - use_lstat = (parent_sb.st_dev != dot_sb->st_dev); + bool use_lstat = (parent_sb.st_dev != dot_sb->st_dev); - found = false; + bool found = false; while (true) { struct dirent const *dp; - struct stat ent_sb; - ino_t ino; errno = 0; if ((dp = readdir_ignoring_dot_and_dotdot (dirp)) == nullptr) @@ -198,8 +191,9 @@ find_dir_entry (struct stat *dot_sb, struct file_name *file_name, break; } - ino = D_INO (dp); + ino_t ino = D_INO (dp); + struct stat ent_sb; if (ino == NOT_AN_INODE_NUMBER || use_lstat) { if (lstat (dp->d_name, &ent_sb) < 0) @@ -268,12 +262,12 @@ robust_getcwd (struct file_name *file_name) size_t height = 1; struct dev_ino dev_ino_buf; struct dev_ino *root_dev_ino = get_root_dev_ino (&dev_ino_buf); - struct stat dot_sb; if (root_dev_ino == nullptr) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), quoteaf ("/")); + struct stat dot_sb; if (stat (".", &dot_sb) < 0) error (EXIT_FAILURE, errno, _("failed to stat %s"), quoteaf (".")); @@ -294,18 +288,15 @@ robust_getcwd (struct file_name *file_name) /* Return PWD from the environment if it is acceptable for 'pwd -L' output, otherwise nullptr. */ -static char * +static char const * logical_getcwd (void) { - struct stat st1; - struct stat st2; - char *wd = getenv ("PWD"); - char *p; + char const *wd = getenv ("PWD"); /* Textual validation first. */ if (!wd || wd[0] != '/') return nullptr; - p = wd; + char const *p = wd; while ((p = strstr (p, "/."))) { if (!p[2] || p[2] == '/' @@ -315,6 +306,8 @@ logical_getcwd (void) } /* System call validation. */ + struct stat st1; + struct stat st2; if (stat (wd, &st1) == 0 && stat (".", &st2) == 0 && psame_inode (&st1, &st2)) return wd; return nullptr; @@ -324,7 +317,6 @@ logical_getcwd (void) int main (int argc, char **argv) { - char *wd; /* POSIX requires a default of -L, but most scripts expect -P. Currently shells default to -L, while stand-alone pwd implementations default to -P. */ @@ -366,7 +358,7 @@ main (int argc, char **argv) if (logical) { - wd = logical_getcwd (); + char const *wd = logical_getcwd (); if (wd) { puts (wd); @@ -374,7 +366,7 @@ main (int argc, char **argv) } } - wd = xgetcwd (); + char *wd = xgetcwd (); if (wd != nullptr) { puts (wd); -- 2.52.0
>From d213c625ac5b5365236c4b112cc3b3fa70e685db Mon Sep 17 00:00:00 2001 Message-ID: <d213c625ac5b5365236c4b112cc3b3fa70e685db.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:35:48 -0800 Subject: [PATCH 08/10] maint: sync: reduce variable scope * src/sync.c (sync_arg, main): Declare variables where they are used instead of at the start of the function. --- src/sync.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/sync.c b/src/sync.c index 645afd4cd..032b65f1c 100644 --- a/src/sync.c +++ b/src/sync.c @@ -87,9 +87,7 @@ or their containing file systems.\n\ static bool sync_arg (enum sync_mode mode, char const *file) { - bool ret = true; int open_flags = O_RDONLY | O_NONBLOCK; - int fd; #if defined _AIX || defined __CYGWIN__ /* AIX 7.1, CYGWIN 2.9.0, fsync requires write access to file. */ @@ -99,7 +97,7 @@ sync_arg (enum sync_mode mode, char const *file) /* Note O_PATH might be supported with syncfs(), though as of Linux 3.18 is not. */ - fd = open (file, open_flags); + int fd = open (file, open_flags); if (fd < 0) { /* Use the O_RDONLY errno, which is significant @@ -114,6 +112,7 @@ sync_arg (enum sync_mode mode, char const *file) } } + bool ret = true; /* We used O_NONBLOCK above to not hang with fifos, so reset that here. */ int fdflags = fcntl (fd, F_GETFL); @@ -167,10 +166,7 @@ sync_arg (enum sync_mode mode, char const *file) int main (int argc, char **argv) { - int c; - bool args_specified; bool arg_data = false, arg_file_system = false; - enum sync_mode mode; bool ok = true; initialize_main (&argc, &argv); @@ -181,6 +177,7 @@ main (int argc, char **argv) atexit (close_stdout); + int c; while ((c = getopt_long (argc, argv, "df", long_options, nullptr)) != -1) { @@ -203,7 +200,7 @@ main (int argc, char **argv) } } - args_specified = optind < argc; + bool args_specified = optind < argc; if (arg_data && arg_file_system) error (EXIT_FAILURE, 0, @@ -212,6 +209,7 @@ main (int argc, char **argv) if (!args_specified && arg_data) error (EXIT_FAILURE, 0, _("--data needs at least one argument")); + enum sync_mode mode; if (! args_specified || (arg_file_system && ! HAVE_SYNCFS)) mode = MODE_SYNC; else if (arg_file_system) -- 2.52.0
>From 5af9ffed32578583b5e4010857a0d51c0df5fb1c Mon Sep 17 00:00:00 2001 Message-ID: <5af9ffed32578583b5e4010857a0d51c0df5fb1c.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:47:24 -0800 Subject: [PATCH 09/10] maint: nohup: reduce variable scope * src/nohup.c (main): Declare variables where they are used instead of at the start of the function. --- src/nohup.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/nohup.c b/src/nohup.c index 6218986cb..86582b227 100644 --- a/src/nohup.c +++ b/src/nohup.c @@ -81,14 +81,6 @@ To save output to FILE, use '%s COMMAND > FILE'.\n"), int main (int argc, char **argv) { - int out_fd = STDOUT_FILENO; - int saved_stderr_fd = STDERR_FILENO; - bool ignoring_input; - bool redirecting_stdout; - bool stdout_is_closed; - bool redirecting_stderr; - int exit_internal_failure; - initialize_main (&argc, &argv); set_program_name (argv[0]); setlocale (LC_ALL, ""); @@ -99,8 +91,8 @@ main (int argc, char **argv) for env, exec, nice, time, and xargs where it requires internal failure give something in the range 1-125. For consistency with other tools, fail with EXIT_CANCELED unless POSIXLY_CORRECT. */ - exit_internal_failure = (getenv ("POSIXLY_CORRECT") - ? POSIX_NOHUP_FAILURE : EXIT_CANCELED); + int exit_internal_failure = (getenv ("POSIXLY_CORRECT") + ? POSIX_NOHUP_FAILURE : EXIT_CANCELED); initialize_exit_failure (exit_internal_failure); atexit (close_stdout); @@ -114,10 +106,10 @@ main (int argc, char **argv) usage (exit_internal_failure); } - ignoring_input = isatty (STDIN_FILENO); - redirecting_stdout = isatty (STDOUT_FILENO); - stdout_is_closed = (!redirecting_stdout && errno == EBADF); - redirecting_stderr = isatty (STDERR_FILENO); + bool ignoring_input = isatty (STDIN_FILENO); + bool redirecting_stdout = isatty (STDOUT_FILENO); + bool stdout_is_closed = (!redirecting_stdout && errno == EBADF); + bool redirecting_stderr = isatty (STDERR_FILENO); /* If standard input is a tty, replace it with /dev/null if possible. Note that it is deliberately opened for *writing*, @@ -135,6 +127,7 @@ main (int argc, char **argv) First try nohup.out, then $HOME/nohup.out. If standard error is a tty and standard output is closed, open nohup.out or $HOME/nohup.out without redirecting anything. */ + int out_fd = STDOUT_FILENO; if (redirecting_stdout || (redirecting_stderr && stdout_is_closed)) { char *in_home = nullptr; @@ -179,6 +172,7 @@ main (int argc, char **argv) } /* If standard error is a tty, redirect it. */ + int saved_stderr_fd = STDERR_FILENO; if (redirecting_stderr) { /* Save a copy of stderr before redirecting, so we can use the original -- 2.52.0
>From cc3cb220c347e49de6c096782391776047d8872b Mon Sep 17 00:00:00 2001 Message-ID: <cc3cb220c347e49de6c096782391776047d8872b.1765151708.git.collin.fu...@gmail.com> In-Reply-To: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> References: <597dfa0d310cc3c0449d08cd9d6d475473af5dad.1765151708.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 7 Dec 2025 15:48:15 -0800 Subject: [PATCH 10/10] maint: env: reduce variable scope * src/env.c (extract_varname, parse_signal_action_params) (parse_block_signal_params, set_signal_proc_mask) (list_signal_handling, main): Declare variables where they are used instead of at the start of the function. --- src/env.c | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/env.c b/src/env.c index e06ca5568..a5d8537c8 100644 --- a/src/env.c +++ b/src/env.c @@ -218,15 +218,13 @@ scan_varname (char const *str) static char * extract_varname (char const *str) { - idx_t i; - char const *p; + char const *p = scan_varname (str); - p = scan_varname (str); if (!p) return nullptr; /* -2 and +2 (below) account for the '${' prefix. */ - i = p - str - 2; + idx_t i = p - str - 2; if (i >= vnlen) { @@ -543,9 +541,6 @@ parse_split_string (char const *str, int *orig_optind, static void parse_signal_action_params (char const *arg, bool set_default) { - char *opt_sig; - char *optarg_writable; - if (! arg) { /* Without an argument, reset all signals. @@ -556,9 +551,9 @@ parse_signal_action_params (char const *arg, bool set_default) return; } - optarg_writable = xstrdup (arg); + char *optarg_writable = xstrdup (arg); - opt_sig = strtok (optarg_writable, ","); + char *opt_sig = strtok (optarg_writable, ","); while (opt_sig) { int signum = operand2sig (opt_sig); @@ -624,9 +619,6 @@ reset_signal_handlers (void) static void parse_block_signal_params (char const *arg, bool block) { - char *opt_sig; - char *optarg_writable; - if (! arg) { /* Without an argument, reset all signals. */ @@ -645,9 +637,9 @@ parse_block_signal_params (char const *arg, bool block) if (! arg) return; - optarg_writable = xstrdup (arg); + char *optarg_writable = xstrdup (arg); - opt_sig = strtok (optarg_writable, ","); + char *opt_sig = strtok (optarg_writable, ","); while (opt_sig) { int signum = operand2sig (opt_sig); @@ -678,7 +670,6 @@ set_signal_proc_mask (void) { /* Get the existing signal mask */ sigset_t set; - char const *debug_act; sigemptyset (&set); @@ -687,6 +678,8 @@ set_signal_proc_mask (void) for (int i = 1; i <= SIGNUM_BOUND; i++) { + char const *debug_act = nullptr; + if (sigismember (&block_signals, i)) { sigaddset (&set, i); @@ -697,10 +690,6 @@ set_signal_proc_mask (void) sigdelset (&set, i); debug_act = "UNBLOCK"; } - else - { - debug_act = nullptr; - } if (dev_debug && debug_act) { @@ -720,7 +709,6 @@ static void list_signal_handling (void) { sigset_t set; - char signame[SIG2STR_MAX]; sigemptyset (&set); if (sigprocmask (0, nullptr, &set)) @@ -739,6 +727,7 @@ list_signal_handling (void) if (! *ignored && ! *blocked) continue; + char signame[SIG2STR_MAX]; if (sig2str (i, signame) != 0) snprintf (signame, sizeof signame, "SIG%d", i); fprintf (stderr, "%-10s (%2d): %s%s%s\n", signame, i, @@ -760,7 +749,6 @@ initialize_signals (void) int main (int argc, char **argv) { - int optc; bool ignore_environment = false; bool opt_nul_terminate_output = false; char const *newdir = nullptr; @@ -777,6 +765,7 @@ main (int argc, char **argv) initialize_signals (); + int optc; while ((optc = getopt_long (argc, argv, shortopts, longopts, nullptr)) != -1) { switch (optc) -- 2.52.0
