]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: groups: reduce variable scope
authorCollin Funk <collin.funk1@gmail.com>
Wed, 10 Dec 2025 05:48:50 +0000 (21:48 -0800)
committerCollin Funk <collin.funk1@gmail.com>
Thu, 11 Dec 2025 02:50:53 +0000 (18:50 -0800)
* 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

index f01ed7dccb231de04a6bf06e05015c33a1d80e8a..4a491f587720645c1ed8ad1d78ba54f770427214 100644 (file)
@@ -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, ' '))