Collin Funk <[email protected]> writes:

> I've attatched some patches covering the simpler cases.

Here are a few more. I'll push tomorrow.

Collin

>From 7554cc40cda65a2dde0bc0a1af1741addf91d289 Mon Sep 17 00:00:00 2001
Message-ID: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 20:45:51 -0800
Subject: [PATCH 1/7] maint: mv: reduce variable scope

* src/mv.c (do_move, main): Declare variables where they are used
instead of at the start of a block.
---
 src/mv.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/src/mv.c b/src/mv.c
index cf1ac56e8..4f01d6546 100644
--- a/src/mv.c
+++ b/src/mv.c
@@ -229,15 +229,11 @@ do_move (char const *source, char const *dest,
       if (dir_to_remove != nullptr)
         {
           struct rm_options rm_options;
-          enum RM_status status;
-          char const *dir[2];
-
           rm_option_init (&rm_options);
           rm_options.verbose = x->verbose;
-          dir[0] = dir_to_remove;
-          dir[1] = nullptr;
+          char const *dir[2] = { dir_to_remove, nullptr };
 
-          status = rm ((void *) dir, &rm_options);
+          enum RM_status status = rm ((void *) dir, &rm_options);
           affirm (VALID_STATUS (status));
           if (status == RM_ERROR)
             ok = false;
@@ -315,8 +311,6 @@ If you specify more than one of -i, -f, -n, only the final one takes effect.\n\
 int
 main (int argc, char **argv)
 {
-  int c;
-  bool ok;
   bool make_backups = false;
   char const *backup_suffix = nullptr;
   char *version_control_string = nullptr;
@@ -324,8 +318,6 @@ main (int argc, char **argv)
   bool remove_trailing_slashes = false;
   char const *target_directory = nullptr;
   bool no_target_directory = false;
-  int n_files;
-  char **file;
   bool selinux_enabled = (0 < is_selinux_enabled ());
 
   initialize_main (&argc, &argv);
@@ -341,6 +333,7 @@ main (int argc, char **argv)
   /* Try to disable the ability to unlink a directory.  */
   priv_set_remove_linkdir ();
 
+  int c;
   while ((c = getopt_long (argc, argv, "bfint:uvS:TZ", long_options, nullptr))
          != -1)
     {
@@ -412,8 +405,8 @@ main (int argc, char **argv)
         }
     }
 
-  n_files = argc - optind;
-  file = argv + optind;
+  int n_files = argc - optind;
+  char **file = argv + optind;
 
   if (n_files <= !target_directory)
     {
@@ -516,6 +509,7 @@ main (int argc, char **argv)
 
   hash_init ();
 
+  bool ok;
   if (target_directory)
     {
       /* Initialize the hash table only if we'll need it.
-- 
2.52.0

>From 5798094394b10bf23a7239bc37cb91036b6b578d Mon Sep 17 00:00:00 2001
Message-ID: <5798094394b10bf23a7239bc37cb91036b6b578d.1765345901.git.collin.fu...@gmail.com>
In-Reply-To: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
References: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 21:05:22 -0800
Subject: [PATCH 2/7] maint: cp: reduce variable scope

* src/cp.c (re_protect): Initialize variables where they are declared.
(make_dir_parents_private, do_copy, main): Declare variables where they
are used instead of at the start of the block.
---
 src/cp.c | 67 ++++++++++++++++++++++++--------------------------------
 1 file changed, 29 insertions(+), 38 deletions(-)

diff --git a/src/cp.c b/src/cp.c
index 42a89acd7..f931a7ad6 100644
--- a/src/cp.c
+++ b/src/cp.c
@@ -330,10 +330,8 @@ re_protect (char const *const_dst_name, char const *dst_src_name,
 
       if (x->preserve_timestamps)
         {
-          struct timespec timespec[2];
-
-          timespec[0] = get_stat_atime (&p->st);
-          timespec[1] = get_stat_mtime (&p->st);
+          struct timespec timespec[2] = { get_stat_atime (&p->st),
+                                          get_stat_mtime (&p->st) };
 
           if (utimensat (dst_dirfd, relname, timespec, 0))
             {
@@ -407,10 +405,6 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
                           struct dir_attr **attr_list, bool *new_dst,
                           const struct cp_options *x)
 {
-  struct stat stats;
-  char *dir;		/* A copy of CONST_DIR we can change.  */
-  char *src;		/* Source name in DIR.  */
-  char *dst_dir;	/* Leading directory of DIR.  */
   idx_t dirlen = dir_len (const_dir);
 
   *attr_list = nullptr;
@@ -420,11 +414,15 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
   if (dirlen <= src_offset)
     return true;
 
+  /* A copy of CONST_DIR we can change.  */
+  char *dir;
   ASSIGN_STRDUPA (dir, const_dir);
 
-  src = dir + src_offset;
+  /* Source name in DIR.  */
+  char *src = dir + src_offset;
 
-  dst_dir = alloca (dirlen + 1);
+  /* Leading directory of DIR.  */
+  char *dst_dir = alloca (dirlen + 1);
   memcpy (dst_dir, dir, dirlen);
   dst_dir[dirlen] = '\0';
   char const *dst_reldir = dst_dir + src_offset;
@@ -433,13 +431,13 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
 
   /* XXX: If all dirs are present at the destination,
      no permissions or security contexts will be updated.  */
+  struct stat stats;
   if (fstatat (dst_dirfd, dst_reldir, &stats, 0) != 0)
     {
       /* A parent of CONST_DIR does not exist.
          Make all missing intermediate directories. */
-      char *slash;
+      char *slash = src;
 
-      slash = src;
       while (*slash == '/')
         slash++;
       dst_reldir = slash;
@@ -447,10 +445,9 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
       while ((slash = strchr (slash, '/')))
         {
           struct dir_attr *new;
-          bool missing_dir;
 
           *slash = '\0';
-          missing_dir = fstatat (dst_dirfd, dst_reldir, &stats, 0) != 0;
+          bool missing_dir = fstatat (dst_dirfd, dst_reldir, &stats, 0) != 0;
 
           if (missing_dir || x->preserve_ownership || x->preserve_mode
               || x->preserve_timestamps)
@@ -486,33 +483,30 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
 
           if (missing_dir)
             {
-              mode_t src_mode;
-              mode_t omitted_permissions;
-              mode_t mkdir_mode;
-
               /* This component does not exist.  We must set
                  *new_dst and new->st.st_mode inside this loop because,
                  for example, in the command 'cp --parents ../a/../b/c e_dir',
                  make_dir_parents_private creates only e_dir/../a if
                  ./b already exists. */
               *new_dst = true;
-              src_mode = new->st.st_mode;
+              mode_t src_mode = new->st.st_mode;
 
               /* If the ownership or special mode bits might change,
                  omit some permissions at first, so unauthorized users
                  cannot nip in before the file is ready.  */
-              omitted_permissions = (src_mode
-                                     & (x->preserve_ownership
-                                        ? S_IRWXG | S_IRWXO
-                                        : x->preserve_mode
-                                        ? S_IWGRP | S_IWOTH
-                                        : 0));
+              mode_t omitted_permissions = (src_mode
+                                            & (x->preserve_ownership
+                                               ? S_IRWXG | S_IRWXO
+                                               : x->preserve_mode
+                                               ? S_IWGRP | S_IWOTH
+                                               : 0));
 
               /* POSIX says mkdir's behavior is implementation-defined when
                  (src_mode & ~S_IRWXUGO) != 0.  However, common practice is
                  to ask mkdir to copy all the CHMOD_MODE_BITS, letting mkdir
                  decide what to do with S_ISUID | S_ISGID | S_ISVTX.  */
-              mkdir_mode = x->explicit_no_preserve_mode ? S_IRWXUGO : src_mode;
+              mode_t mkdir_mode = (x->explicit_no_preserve_mode
+                                   ? S_IRWXUGO : src_mode);
               mkdir_mode &= CHMOD_MODE_BITS & ~omitted_permissions;
               if (mkdirat (dst_dirfd, dst_reldir, mkdir_mode) != 0)
                 {
@@ -613,10 +607,6 @@ static bool
 do_copy (int n_files, char **file, char const *target_directory,
          bool no_target_directory, struct cp_options *x)
 {
-  struct stat sb;
-  bool new_dst = false;
-  bool ok = true;
-
   if (n_files <= !target_directory)
     {
       if (n_files <= 0)
@@ -627,8 +617,11 @@ do_copy (int n_files, char **file, char const *target_directory,
       usage (EXIT_FAILURE);
     }
 
+  struct stat sb;
   sb.st_mode = 0;
   int target_dirfd = AT_FDCWD;
+  bool new_dst = false;
+  bool ok = true;
   if (no_target_directory)
     {
       if (target_directory)
@@ -782,10 +775,6 @@ do_copy (int n_files, char **file, char const *target_directory,
     }
   else /* !target_directory */
     {
-      char const *source = file[0];
-      char const *dest = file[1];
-      bool unused;
-
       if (parents_option)
         {
           error (0, 0,
@@ -799,6 +788,8 @@ do_copy (int n_files, char **file, char const *target_directory,
          'cp --force --backup foo foo' to 'cp --force foo fooSUFFIX'
          where SUFFIX is determined by any version control options used.  */
 
+      char const *source = file[0];
+      char const *dest = file[1];
       if (x->unlink_dest_after_failed_open
           && x->backup_type != no_backups
           && streq (source, dest)
@@ -818,6 +809,7 @@ do_copy (int n_files, char **file, char const *target_directory,
           x = &x_tmp;
         }
 
+      bool unused;
       ok = copy (source, dest, AT_FDCWD, dest, -new_dst, x, &unused, nullptr);
     }
 
@@ -973,8 +965,6 @@ decode_preserve_arg (char const *arg, struct cp_options *x, bool on_off)
 int
 main (int argc, char **argv)
 {
-  int c;
-  bool ok;
   bool make_backups = false;
   char const *backup_suffix = nullptr;
   char *version_control_string = nullptr;
@@ -995,6 +985,7 @@ main (int argc, char **argv)
   selinux_enabled = (0 < is_selinux_enabled ());
   cp_option_init (&x);
 
+  int c;
   while ((c = getopt_long (argc, argv, "abdfHilLnprst:uvxPRS:TZ",
                            long_opts, nullptr))
          != -1)
@@ -1275,8 +1266,8 @@ main (int argc, char **argv)
 
   hash_init ();
 
-  ok = do_copy (argc - optind, argv + optind,
-                target_directory, no_target_directory, &x);
+  bool ok = do_copy (argc - optind, argv + optind,
+                     target_directory, no_target_directory, &x);
 
   main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
-- 
2.52.0

>From 788ce1c114d2a6d02e7396334bcb20de64cd06d7 Mon Sep 17 00:00:00 2001
Message-ID: <788ce1c114d2a6d02e7396334bcb20de64cd06d7.1765345901.git.collin.fu...@gmail.com>
In-Reply-To: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
References: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 21:13:10 -0800
Subject: [PATCH 3/7] maint: install: reduce variable scope

* src/install.c (change_timestamps): Initialize variables where they are
declared.
(need_copy, setdefaultfilecon, get_ids, mkancesdirs_safe_wd, main):
Declare variables where they are used instead of at the start of the
block.
---
 src/install.c | 49 ++++++++++++++++++++-----------------------------
 1 file changed, 20 insertions(+), 29 deletions(-)

diff --git a/src/install.c b/src/install.c
index 1c27bd87d..93903bb59 100644
--- a/src/install.c
+++ b/src/install.c
@@ -170,17 +170,15 @@ need_copy (char const *src_name, char const *dest_name,
            int dest_dirfd, char const *dest_relname,
            const struct cp_options *x)
 {
-  struct stat src_sb, dest_sb;
-  int src_fd, dest_fd;
-  bool content_match;
-
   if (extra_mode (mode))
     return true;
 
   /* compare files using stat */
+  struct stat src_sb;
   if (stat (src_name, &src_sb) != 0)
     return true;
 
+  struct stat dest_sb;
   if (fstatat (dest_dirfd, dest_relname, &dest_sb, AT_SYMLINK_NOFOLLOW) != 0)
     return true;
 
@@ -216,19 +214,17 @@ need_copy (char const *src_name, char const *dest_name,
   if (selinux_enabled && x->preserve_security_context)
     {
       char *file_scontext_raw = nullptr;
-      char *to_scontext_raw = nullptr;
-      bool scontext_match;
-
       if (getfilecon_raw (src_name, &file_scontext_raw) == -1)
         return true;
 
+      char *to_scontext_raw = nullptr;
       if (getfilecon_raw (dest_name, &to_scontext_raw) == -1)
         {
           freecon (file_scontext_raw);
           return true;
         }
 
-      scontext_match = streq (file_scontext_raw, to_scontext_raw);
+      bool scontext_match = streq (file_scontext_raw, to_scontext_raw);
 
       freecon (file_scontext_raw);
       freecon (to_scontext_raw);
@@ -237,18 +233,18 @@ need_copy (char const *src_name, char const *dest_name,
     }
 
   /* compare files content */
-  src_fd = open (src_name, O_RDONLY | O_BINARY);
+  int src_fd = open (src_name, O_RDONLY | O_BINARY);
   if (src_fd < 0)
     return true;
 
-  dest_fd = openat (dest_dirfd, dest_relname, O_RDONLY | O_BINARY);
+  int dest_fd = openat (dest_dirfd, dest_relname, O_RDONLY | O_BINARY);
   if (dest_fd < 0)
     {
       close (src_fd);
       return true;
     }
 
-  content_match = have_same_content (src_fd, dest_fd);
+  bool content_match = have_same_content (src_fd, dest_fd);
 
   close (src_fd);
   close (dest_fd);
@@ -323,20 +319,21 @@ get_labeling_handle (void)
 static void
 setdefaultfilecon (char const *file)
 {
-  struct stat st;
-  char *scontext_raw = nullptr;
-
   if (selinux_enabled != 1)
     {
       /* Indicate no context found. */
       return;
     }
+
+  struct stat st;
   if (lstat (file, &st) != 0)
     return;
 
   struct selabel_handle *hnd = get_labeling_handle ();
   if (!hnd)
     return;
+
+  char *scontext_raw = nullptr;
   if (selabel_lookup_raw (hnd, &scontext_raw, file, st.st_mode) != 0)
     {
       if (errno != ENOENT && ! ignorable_ctx_err (errno))
@@ -470,9 +467,8 @@ static bool
 change_timestamps (struct stat const *src_sb, char const *dest,
                    int dirfd, char const *relname)
 {
-  struct timespec timespec[2];
-  timespec[0] = get_stat_atime (src_sb);
-  timespec[1] = get_stat_mtime (src_sb);
+  struct timespec timespec[2] = { get_stat_atime (src_sb),
+                                  get_stat_mtime (src_sb) };
 
   if (utimensat (dirfd, relname, timespec, 0))
     {
@@ -548,12 +544,9 @@ strip (char const *name)
 static void
 get_ids (void)
 {
-  struct passwd *pw;
-  struct group *gr;
-
   if (owner_name)
     {
-      pw = getpwnam (owner_name);
+      struct passwd *pw = getpwnam (owner_name);
       if (pw == nullptr)
         {
           uintmax_t tmp;
@@ -571,7 +564,7 @@ get_ids (void)
 
   if (group_name)
     {
-      gr = getgrnam (group_name);
+      struct group *gr = getgrnam (group_name);
       if (gr == nullptr)
         {
           uintmax_t tmp;
@@ -708,13 +701,13 @@ mkancesdirs_safe_wd (char const *from, char *to, struct cp_options *x,
   bool save_working_directory =
     save_always
     || ! (IS_ABSOLUTE_FILE_NAME (from) && IS_ABSOLUTE_FILE_NAME (to));
-  int status = EXIT_SUCCESS;
 
   struct savewd wd;
   savewd_init (&wd);
   if (! save_working_directory)
     savewd_finish (&wd);
 
+  int status = EXIT_SUCCESS;
   if (mkancesdirs (to, &wd, make_ancestor, x) == -1)
     {
       error (0, errno, _("cannot create directory %s"), quoteaf (to));
@@ -795,8 +788,6 @@ install_file_in_dir (char const *from, char const *to_dir,
 int
 main (int argc, char **argv)
 {
-  int optc;
-  int exit_status = EXIT_SUCCESS;
   char const *specified_mode = nullptr;
   bool make_backups = false;
   char const *backup_suffix = nullptr;
@@ -805,8 +796,6 @@ main (int argc, char **argv)
   struct cp_options x;
   char const *target_directory = nullptr;
   bool no_target_directory = false;
-  int n_files;
-  char **file;
   bool strip_program_specified = false;
   char const *scontext = nullptr;
   /* set iff kernel has extra selinux system calls */
@@ -828,6 +817,7 @@ main (int argc, char **argv)
   dir_arg = false;
   umask (0);
 
+  int optc;
   while ((optc = getopt_long (argc, argv, "bcCsDdg:m:o:pt:TvS:Z", long_options,
                               nullptr))
          != -1)
@@ -956,8 +946,8 @@ main (int argc, char **argv)
            _("failed to set default file creation context to %s"),
          quote (scontext));
 
-  n_files = argc - optind;
-  file = argv + optind;
+  int n_files = argc - optind;
+  char **file = argv + optind;
 
   if (n_files <= ! (dir_arg || target_directory))
     {
@@ -1039,6 +1029,7 @@ main (int argc, char **argv)
 
   get_ids ();
 
+  int exit_status = EXIT_SUCCESS;
   if (dir_arg)
     exit_status = savewd_process_files (n_files, file, process_dir, &x);
   else
-- 
2.52.0

>From d24495ce940f587ac53a4952c6416f55356c561c Mon Sep 17 00:00:00 2001
Message-ID: <d24495ce940f587ac53a4952c6416f55356c561c.1765345901.git.collin.fu...@gmail.com>
In-Reply-To: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
References: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 21:37:50 -0800
Subject: [PATCH 4/7] maint: date: reduce variable scope

* src/date.c (batch_convert, main): Declare variables where they are
used instead of at the start of the function.
---
 src/date.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/src/date.c b/src/date.c
index 8959094d4..153bd10a0 100644
--- a/src/date.c
+++ b/src/date.c
@@ -406,11 +406,7 @@ batch_convert (char const *input_filename,
                char const *format, bool format_in_c_locale,
                timezone_t tz, char const *tzstring)
 {
-  bool ok;
   FILE *in_stream;
-  char *line;
-  size_t buflen;
-  struct timespec when;
 
   if (streq (input_filename, "-"))
     {
@@ -424,9 +420,9 @@ batch_convert (char const *input_filename,
         error (EXIT_FAILURE, errno, "%s", quotef (input_filename));
     }
 
-  line = nullptr;
-  buflen = 0;
-  ok = true;
+  char *line = nullptr;
+  size_t buflen = 0;
+  bool ok = true;
   while (true)
     {
       ssize_t line_length = getline (&line, &buflen, in_stream);
@@ -438,6 +434,7 @@ batch_convert (char const *input_filename,
           break;
         }
 
+      struct timespec when;
       if (! parse_datetime2 (&when, line, nullptr,
                              parse_datetime_flags, tz, tzstring))
         {
@@ -466,18 +463,14 @@ batch_convert (char const *input_filename,
 int
 main (int argc, char **argv)
 {
-  int optc;
   char const *datestr = nullptr;
   char const *set_datestr = nullptr;
-  struct timespec when;
   bool set_date = false;
   char const *format = nullptr;
   bool format_in_c_locale = false;
   bool get_resolution = false;
   char *batch_file = nullptr;
   char *reference = nullptr;
-  struct stat refstats;
-  bool ok;
   bool discarded_datestr = false;
   bool discarded_set_datestr = false;
 
@@ -489,6 +482,7 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
+  int optc;
   while ((optc = getopt_long (argc, argv, short_options, long_options, nullptr))
          != -1)
     {
@@ -642,13 +636,14 @@ main (int argc, char **argv)
   char const *tzstring = getenv ("TZ");
   timezone_t tz = tzalloc (tzstring);
 
+  bool ok = true;
   if (batch_file != nullptr)
     ok = batch_convert (batch_file, format_res, format_in_c_locale,
                         tz, tzstring);
   else
     {
       bool valid_date = true;
-      ok = true;
+      struct timespec when;
 
       if (!option_specified_date && !set_date)
         {
@@ -675,6 +670,7 @@ main (int argc, char **argv)
           /* (option_specified_date || set_date) */
           if (reference != nullptr)
             {
+              struct stat refstats;
               if (stat (reference, &refstats) != 0)
                 error (EXIT_FAILURE, errno, "%s", quotef (reference));
               when = get_stat_mtime (&refstats);
-- 
2.52.0

>From da14f41fc694b55f809f5f698d8b6fea2b9c2b60 Mon Sep 17 00:00:00 2001
Message-ID: <da14f41fc694b55f809f5f698d8b6fea2b9c2b60.1765345901.git.collin.fu...@gmail.com>
In-Reply-To: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
References: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 21:43:11 -0800
Subject: [PATCH 5/7] maint: rmdir: reduce variable scope

* src/rmdir.c (remove_parents, main): Declare variables where they are
used instead of at the start of a block.
---
 src/rmdir.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/rmdir.c b/src/rmdir.c
index 9ca3f7ed1..1b2e7e4a5 100644
--- a/src/rmdir.c
+++ b/src/rmdir.c
@@ -112,13 +112,12 @@ ignorable_failure (int error_number, char const *dir)
 static bool
 remove_parents (char *dir)
 {
-  char *slash;
   bool ok = true;
 
   strip_trailing_slashes (dir);
   while (true)
     {
-      slash = strrchr (dir, '/');
+      char *slash = strrchr (dir, '/');
       if (slash == nullptr)
         break;
       /* Remove any characters after the slash, skipping any extra
@@ -197,9 +196,6 @@ Remove the DIRECTORY(ies), if they are empty.\n\
 int
 main (int argc, char **argv)
 {
-  bool ok = true;
-  int optc;
-
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
   setlocale (LC_ALL, "");
@@ -210,6 +206,7 @@ main (int argc, char **argv)
 
   remove_empty_parents = false;
 
+  int optc;
   while ((optc = getopt_long (argc, argv, "pv", longopts, nullptr)) != -1)
     {
       switch (optc)
@@ -236,6 +233,7 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }
 
+  bool ok = true;
   for (; optind < argc; ++optind)
     {
       char *dir = argv[optind];
-- 
2.52.0

>From 3c33b8bf83b891698e5a53dfd0efcb3df0e1a0cd Mon Sep 17 00:00:00 2001
Message-ID: <3c33b8bf83b891698e5a53dfd0efcb3df0e1a0cd.1765345901.git.collin.fu...@gmail.com>
In-Reply-To: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
References: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 21:45:26 -0800
Subject: [PATCH 6/7] maint: printenv: reduce variable scope

* src/printenv.c (main): Declare variables where they are used instead
of at the start of the function. Constify some strings we do not modify.
---
 src/printenv.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/printenv.c b/src/printenv.c
index bc99dd87e..00690d70b 100644
--- a/src/printenv.c
+++ b/src/printenv.c
@@ -80,9 +80,6 @@ If no VARIABLE is specified, print name and value pairs for them all.\n\
 int
 main (int argc, char **argv)
 {
-  char *ep, *ap;
-  bool ok;
-  int optc;
   bool opt_nul_terminate_output = false;
 
   initialize_main (&argc, &argv);
@@ -94,6 +91,7 @@ main (int argc, char **argv)
   initialize_exit_failure (PRINTENV_FAILURE);
   atexit (close_stdout);
 
+  int optc;
   while ((optc = getopt_long (argc, argv, "+iu:0", longopts, nullptr)) != -1)
     {
       switch (optc)
@@ -108,6 +106,7 @@ main (int argc, char **argv)
         }
     }
 
+  bool ok;
   if (optind >= argc)
     {
       for (char **env = environ; *env != nullptr; ++env)
@@ -128,8 +127,8 @@ main (int argc, char **argv)
 
           for (char **env = environ; *env; ++env)
             {
-              ep = *env;
-              ap = argv[i];
+              char const *ep = *env;
+              char const *ap = argv[i];
               while (*ep != '\0' && *ap != '\0' && *ep++ == *ap++)
                 {
                   if (*ep == '=' && *ap == '\0')
-- 
2.52.0

>From bb86730a54336e2441b3bdd86c0818af36b12878 Mon Sep 17 00:00:00 2001
Message-ID: <bb86730a54336e2441b3bdd86c0818af36b12878.1765345901.git.collin.fu...@gmail.com>
In-Reply-To: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
References: <7554cc40cda65a2dde0bc0a1af1741addf91d289.1765345901.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Tue, 9 Dec 2025 21:48:50 -0800
Subject: [PATCH 7/7] maint: groups: reduce variable scope

* src/groups.c (main): Declare variables where they are used instead of
at the start of the function. Convert a comment to GNU style.
---
 src/groups.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/src/groups.c b/src/groups.c
index f01ed7dcc..4a491f587 100644
--- a/src/groups.c
+++ b/src/groups.c
@@ -66,11 +66,6 @@ the current process (which may differ if the groups database has changed).\n"),
 int
 main (int argc, char **argv)
 {
-  int optc;
-  bool ok = true;
-  gid_t rgid, egid;
-  uid_t ruid;
-
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
   setlocale (LC_ALL, "");
@@ -80,8 +75,8 @@ main (int argc, char **argv)
   atexit (close_stdout);
 
   /* Processing the arguments this way makes groups.c behave differently to
-   * groups.sh if one of the arguments is "--".
-   */
+     groups.sh if one of the arguments is "--".  */
+  int optc;
   while ((optc = getopt_long (argc, argv, "", longopts, nullptr)) != -1)
     {
       switch (optc)
@@ -93,6 +88,7 @@ main (int argc, char **argv)
         }
     }
 
+  bool ok = true;
   if (optind == argc)
     {
       /* No arguments.  Divulge the details of the current process. */
@@ -100,17 +96,17 @@ main (int argc, char **argv)
       gid_t NO_GID = -1;
 
       errno = 0;
-      ruid = getuid ();
+      uid_t ruid = getuid ();
       if (ruid == NO_UID && errno)
         error (EXIT_FAILURE, errno, _("cannot get real UID"));
 
       errno = 0;
-      egid = getegid ();
+      gid_t egid = getegid ();
       if (egid == NO_GID && errno)
         error (EXIT_FAILURE, errno, _("cannot get effective GID"));
 
       errno = 0;
-      rgid = getgid ();
+      gid_t rgid = getgid ();
       if (rgid == NO_GID && errno)
         error (EXIT_FAILURE, errno, _("cannot get real GID"));
 
@@ -130,8 +126,9 @@ main (int argc, char **argv)
               ok = false;
               continue;
             }
-          ruid = pwd->pw_uid;
-          rgid = egid = pwd->pw_gid;
+          uid_t ruid = pwd->pw_uid;
+          gid_t rgid = pwd->pw_gid;
+          gid_t egid = rgid;
 
           printf ("%s : ", argv[optind]);
           if (!print_group_list (argv[optind], ruid, rgid, egid, true, ' '))
-- 
2.52.0

Reply via email to