bug-sysutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-sysutils] Updated argp patch for vipw


From: Barry deFreese
Subject: [Bug-sysutils] Updated argp patch for vipw
Date: Sat, 29 May 2004 06:58:17 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5

Same for vipw.

BTW, it just dawned on me that I have not been sending ChangeLog entries. Should I be??

Thanks,

--
Barry deFreese
Debian 3.0r1 "Woody"
GNU/Hurd
Registered Linux "Newbie" #302256 - Hurd H4XX0r wannabe

"Programming today is a race between software engineers striving
to build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots. So far, the Universe is
winning." Rich Cook.



Index: src/cppw.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/cppw.c,v
retrieving revision 1.3
diff -u -p -r1.3 cppw.c
--- src/cppw.c  27 May 2004 07:50:33 -0000      1.3
+++ src/cppw.c  29 May 2004 14:45:21 -0000
@@ -20,6 +20,7 @@
  *  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,31 +35,83 @@
 #define PRG_NAME3 "cpsp"
 #define PRG_NAME4 "cpsg"
 
-extern int optind;
-extern char *optarg;
 extern const char *progname;
 
-/**
- * Show usage information
- */
-static void usage(void)
+const char *argp_program_bug_address = PACKAGE_BUGREPORT;
+
+static char args_doc[] = "FILE";
+
+static char doc[] =
+       N_("Safely copy /etc/{passwd,shadow,group,gshadow} to FILE.\n"
+          "\n"
+          "The program can be called as any of cppw, cpgr, "
+          "cpsp, or cpsg,\n"
+          "and will behave accordingly.\n");
+
+static struct argp_option options[] = {
+       {"group", 'g', 0, 0,
+         N_("copy to /etc/group or its shadow\n"), 0},
+       {"passwd", 'p', 0, 0,
+         N_("copy to /etc/passwd or its shadow\n"), 0},
+       {"shadow", 's', 0, 0,
+         N_("copy to the shadow instead of passwd/group\n"), 0},
+       { 0, 0, 0, 0, 0, 0}
+};
+
+struct arguments {
+       char *args;
+       long shadow;
+       long passwd;
+       long group;
+       long opt_file;
+};
+
+/* Parse a single option */
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
 {
-       fprintf(stdout,
-               _("Usage: %s [OPTION]... FILE\n"
-                 "Safely copy /etc/{passwd,shadow,group,gshadow} to FILE.\n"
-                 "\n"
-                 "The program can be called as any of `%s', `%s', "
-                 "`%s', or `%s',\n"
-                 "and will behave accordingly.\n"
-                 "\n"
-                 "  -g, --group            copy to /etc/group "
-                 "or its shadow\n"
-                 "  -p, --passwd           copy to /etc/passwd "
-                 "or its shadow\n"
-                 "  -s, --shadow           copy to the shadow instead of "
-                 "passwd/group\n"),
-               progname, PRG_NAME, PRG_NAME2, PRG_NAME3, PRG_NAME4);
-       usage_help();
+       struct arguments *args = state->input;
+
+       switch(key) {
+       case 's':
+               args->shadow = 1;
+               break;
+
+       case 'p':
+               if (args->passwd == 0) {
+                       argp_error(state, "-p cannot be combined with -g\n");
+               } else {
+                       args->passwd = 1;
+                       args->group = 0;
+                       break;
+               }
+
+       case 'g':
+               if (args->group == 0) {
+                       argp_error(state, "-p cannot be combined with -g\n");
+               } else {
+                       args->group = 1;
+                       args->passwd = 0;
+                       break;
+               }
+                       
+       case ARGP_KEY_INIT:
+               args->shadow = -1;
+               args->passwd = -1;
+               args->group = -1;
+               break;
+
+       case ARGP_KEY_ARG:
+               if (state->arg_num >= 2)
+                       argp_usage(state);
+
+               args->args = arg;
+
+               break;
+
+       default:
+               return ARGP_ERR_UNKNOWN;
+       }
+       return 0;
 }
 
 /**
@@ -70,9 +123,6 @@ static void usage(void)
  */
 int main(int argc, char *argv[])
 {
-       int optc;
-       int opt_index;
-
        int status = 0;
 
        char *bname = NULL;
@@ -84,83 +134,39 @@ int main(int argc, char *argv[])
        int mode = F_DEFAULT;
        int newmode = F_DEFAULT;
 
-       struct option const options[] = {
-               { "group", no_argument, 0, 'g' },
-               { "passwd", no_argument, 0, 'p' },
-               { "shadow", no_argument, 0, 's' },
-               { "help", no_argument, 0, 'h' },
-               { "version", no_argument, 0, 'V' },
-               { 0, 0, 0, 0 }
+       errno = 0;
+
+       /* argp parser */
+       struct argp argp = {
+               options, parse_opt, args_doc, doc, NULL, NULL, NULL
        };
 
-       errno = 0;
+       struct arguments args;
+
+       argp_program_version_hook = version;
 
        /* Initialise support for locales, and set the program-name */
        if ((status = init_locales(PRG_NAME)))
                goto EXIT;
 
+       set_author_information(_("Written by David Weinhall.\n"));
+
+       /* Parse command line */
+       argp_parse(&argp, argc, argv, 0, 0, &args);
+
        /* Setup base-mode of operation based on program-name */
        if (!strcmp(argv[0], PRG_NAME)) {
                mode = F_PASSWD;
-               shadowcopy = 0;
+               args.shadow = 0;
        } else if (!strcmp(argv[0], PRG_NAME2)) {
                mode = F_GROUP;
-               shadowcopy = 0;
+               args.shadow = 0;
        } else if (!strcmp(argv[0], PRG_NAME3)) {
                mode = F_PASSWD;
-               shadowcopy = 1;
+               args.shadow = 1;
        } else if (!strcmp(argv[0], PRG_NAME4)) {
                mode = F_GROUP;
-               shadowcopy = 1;
-       }
-
-       /* Parse the command-line options */
-       while ((optc = getopt_long(argc, argv, "gps",
-                                  options, &opt_index)) != EOF) {
-               switch (optc) {
-               case 'g':
-                       if (newmode != F_PASSWD) {
-                               newmode = F_GROUP;
-                               break;
-                       }
-
-                       fprintf(stderr,
-                               _("%s: `-p' cannot be combined with `-g'\n"
-                                 "Try `%s --help' for more information.\n"),
-                               progname, progname);
-                       status = EINVAL;
-                       goto EXIT;
-
-               case 'p':
-                       if (newmode != F_GROUP) {
-                               newmode = F_PASSWD;
-                               break;
-                       }
-
-                       fprintf(stderr,
-                               _("%s: `-p' cannot be combined with `-g'\n"
-                                 "Try `%s --help' for more information.\n"),
-                               progname, progname);
-                       status = EINVAL;
-                       goto EXIT;
-
-               case 's':
-                       shadowcopy = 1;
-                       break;
-
-               case 'h':
-                       usage();
-                       goto EXIT;
-
-               case 'V':
-                       version();
-                       goto EXIT;
-
-               default:
-                       usage();
-                       status = EINVAL;
-                       goto EXIT;
-               }
+               args.shadow = 1;
        }
 
        /* If a different mode than default has been requesed, change mode */
@@ -197,23 +203,6 @@ int main(int argc, char *argv[])
                                progname, "is_root", strerror(errno));
                }
 
-               goto EXIT;
-       }
-
-       /* Make sure we get the correct number of arguments */
-       if (optind == argc) {
-               fprintf(stderr,
-                       _("%s: too few arguments\n"
-                         "Try `%s --help' for more information.\n"),
-                       progname, progname);
-               status = EINVAL;
-               goto EXIT;
-       } else if ((argc - optind) > 1) {
-               fprintf(stderr,
-                       _("%s: too many arguments\n"
-                         "Try `%s --help' for more information.\n"),
-                       progname, progname);
-               status = EINVAL;
                goto EXIT;
        }
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]