]> granicus.if.org Git - shadow/commitdiff
Split the processing of options out of main().
authornekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Fri, 28 Dec 2007 10:41:22 +0000 (10:41 +0000)
committernekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Fri, 28 Dec 2007 10:41:22 +0000 (10:41 +0000)
ChangeLog
src/groupadd.c

index edd2f08e284d0b571c24b0e7f937c89ed7a859e2..90163853f51a20c98c07b91366213d956e00a519 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,7 @@
        the return code was E_SUCCESS, fail_exit() wouldn't have exited. Fix
        the scope of #idef WITH_AUDIT.
        * src/groupadd.c: Avoid implicit brackets.
+       * src/groupadd.c: Split the processing of options out of main().
 
 2007-12-27  Nicolas François  <nicolas.francois@centraliens.net>
 
index aba4e361d588d1d14532fed404f60ff95557d17a..2e4237432b0a5218bdf409c94ee12344f0e4b638 100644 (file)
@@ -353,9 +353,89 @@ static gid_t get_gid (const char *gidstr)
 }
 
 /*
- * main - groupadd command
+ * process_args - parse the command line options
+ *
+ *     It will not return if an error is encountered.
  */
+static void process_args (int argc, char **argv)
+{
+       char *cp;
+       int option_index = 0;
+       int c;
+       static struct option long_options[] = {
+               {"force", no_argument, NULL, 'f'},
+               {"gid", required_argument, NULL, 'g'},
+               {"help", no_argument, NULL, 'h'},
+               {"key", required_argument, NULL, 'K'},
+               {"non-unique", required_argument, NULL, 'o'},
+               {NULL, 0, NULL, '\0'}
+       };
+
+       while ((c =
+               getopt_long (argc, argv, "fg:hK:o", long_options,
+                            &option_index)) != -1) {
+               switch (c) {
+               case 'f':
+                       /*
+                        * "force" - do nothing, just exit(0), if the
+                        * specified group already exists. With -g, if
+                        * specified gid already exists, choose another
+                        * (unique) gid (turn off -g). Based on the RedHat's
+                        * patch from shadow-utils-970616-9.
+                        */
+                       fflg++;
+                       break;
+               case 'g':
+                       gflg++;
+                       group_id = get_gid (optarg);
+                       break;
+               case 'h':
+                       usage ();
+                       break;
+               case 'K':
+                       /*
+                        * override login.defs defaults (-K name=value)
+                        * example: -K GID_MIN=100 -K GID_MAX=499
+                        * note: -K GID_MIN=10,GID_MAX=499 doesn't work yet
+                        */
+                       cp = strchr (optarg, '=');
+                       if (!cp) {
+                               fprintf (stderr,
+                                        _
+                                        ("%s: -K requires KEY=VALUE\n"),
+                                        Prog);
+                               exit (E_BAD_ARG);
+                       }
+                       /* terminate name, point to value */
+                       *cp++ = '\0';
+                       if (putdef_str (optarg, cp) < 0) {
+                               exit (E_BAD_ARG);
+                       }
+                       break;
+               case 'o':
+                       oflg++;
+                       break;
+               default:
+                       usage ();
+               }
+       }
+
+       if (oflg && !gflg) {
+               usage ();
+       }
 
+       if (optind != argc - 1) {
+               usage ();
+       }
+
+       group_name = argv[optind];
+
+       check_new_name ();
+}
+
+/*
+ * main - groupadd command
+ */
 int main (int argc, char **argv)
 {
 #ifdef USE_PAM
@@ -377,82 +457,10 @@ int main (int argc, char **argv)
 
        OPENLOG ("groupadd");
 
-       {
-               /*
-                * Parse the command line options.
-                */
-               char *cp;
-               int option_index = 0;
-               int c;
-               static struct option long_options[] = {
-                       {"force", no_argument, NULL, 'f'},
-                       {"gid", required_argument, NULL, 'g'},
-                       {"help", no_argument, NULL, 'h'},
-                       {"key", required_argument, NULL, 'K'},
-                       {"non-unique", required_argument, NULL, 'o'},
-                       {NULL, 0, NULL, '\0'}
-               };
-
-               while ((c =
-                       getopt_long (argc, argv, "fg:hK:o", long_options,
-                                    &option_index)) != -1) {
-                       switch (c) {
-                       case 'f':
-                               /*
-                                * "force" - do nothing, just exit(0), if the
-                                * specified group already exists. With -g, if
-                                * specified gid already exists, choose another
-                                * (unique) gid (turn off -g). Based on the RedHat's
-                                * patch from shadow-utils-970616-9.
-                                */
-                               fflg++;
-                               break;
-                       case 'g':
-                               gflg++;
-                               group_id = get_gid (optarg);
-                               break;
-                       case 'h':
-                               usage ();
-                               break;
-                       case 'K':
-                               /*
-                                * override login.defs defaults (-K name=value)
-                                * example: -K GID_MIN=100 -K GID_MAX=499
-                                * note: -K GID_MIN=10,GID_MAX=499 doesn't work yet
-                                */
-                               cp = strchr (optarg, '=');
-                               if (!cp) {
-                                       fprintf (stderr,
-                                                _
-                                                ("%s: -K requires KEY=VALUE\n"),
-                                                Prog);
-                                       exit (E_BAD_ARG);
-                               }
-                               /* terminate name, point to value */
-                               *cp++ = '\0';
-                               if (putdef_str (optarg, cp) < 0) {
-                                       exit (E_BAD_ARG);
-                               }
-                               break;
-                       case 'o':
-                               oflg++;
-                               break;
-                       default:
-                               usage ();
-                       }
-               }
-       }
-
-       if (oflg && !gflg) {
-               usage ();
-       }
-
-       if (optind != argc - 1) {
-               usage ();
-       }
-
-       group_name = argv[argc - 1];
-       check_new_name ();
+       /*
+        * Parse the command line options.
+        */
+       process_args (argc, argv);
 
 #ifdef USE_PAM
        retval = PAM_SUCCESS;