bug-sysutils
[Top][All Lists]
Advanced

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

[Bug-sysutils] argp patch for vipw


From: Barry deFreese
Subject: [Bug-sysutils] argp patch for vipw
Date: Mon, 24 May 2004 19:18:50 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5

OK, here's another attempt at a patch. I have a couple of questions about the code that I need to work out but hopefully this is more along the lines of what you are looking for as far as spacing, etc.

BTW, got my assignment papers today, signed 'em and stuck them back in the mail. :-)

Thanks as always,

--
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.



? build
? config.rpath
? sysutils_bdd_20040521.diff
? sysutils_bdd_20040522.diff
? sysutils_vipw_bdd_20040524.diff
? etc/Makefile.in
? etc/pam.d/Makefile.in
? m4/intmax.m4
? m4/longdouble.m4
? m4/longlong.m4
? m4/nls.m4
? m4/po.m4
? m4/printf-posix.m4
? m4/signed.m4
? m4/size_max.m4
? m4/wchar_t.m4
? m4/wint_t.m4
? m4/xsize.m4
? man/Makefile.in
? man/sv/Makefile.in
? po/Makevars.template
? po/sysutils.pot
? src/getopt.c
? src/getopt1.c
Index: src/vipw.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/vipw.c,v
retrieving revision 1.2
diff -u -p -r1.2 vipw.c
--- src/vipw.c  20 May 2004 10:16:30 -0000      1.2
+++ src/vipw.c  25 May 2004 03:03:20 -0000
@@ -20,6 +20,7 @@
  *  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <argp.h>
 #include <wait.h>
 #include <errno.h>
 #include <stdio.h>
@@ -37,30 +38,82 @@
 #define PRG_NAME3 "visp"
 #define PRG_NAME4 "visg"
 
-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 doc[] =
+       N_("Safely edit /etc/{passwd,shadow,group,gshadow}.\n"
+          "\n"
+          "The program can be called as any of vipw, vigr, "
+          "visp, or visg,\n"
+          "and will behave accordingly.\n");
+
+static struct argp_option options[] = {
+       {"shadow", 's', 0, 0, 
+         N_("edit the shadow instead of passwd/group\n"), 0},
+       {"passwd", 'p', 0, 0,
+         N_("edit /etc/passwd or its shadow\n", 0},
+       {"group", 'g', 0, 0,
+         N_("edit /etc/group or its shadow\n", 0},
+       { 0, 0, 0, 0, 0, 0}
+};
+
+struct arguments {
+       char *args;
+       long shadow;
+       long passwd;
+       long group;
+};
+
+/* Parse a single option */
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
 {
-       fprintf(stdout,
-               _("Usage: %s [OPTION]...\n"
-                 "Safely edit /etc/{passwd,shadow,group,gshadow}.\n"
-                 "\n"
-                 "The program can be called as any of `%s', `%s', "
-                 "`%s', or `%s',\n"
-                 "and will behave accordingly.\n"
-                 "\n"
-                 "  -s, --shadow           edit the shadow instead "
-                 "of passwd/group\n"
-                 "  -p, --passwd           edit /etc/passwd or its shadow\n"
-                 "  -g, --group            edit /etc/group or its shadow\n"),
-               progname, PRG_NAME, PRG_NAME2, PRG_NAME3, PRG_NAME4);
-       usage_help();
-}
+       struct arguments *args = state->input;
+       
+       switch(key) {
+       case 's':
+               shadowedit = 1;
+               break;
+
+       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 '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 ARGP_KEY_INIT:
+               arguments->shadow = 0;
+               arguments->passwd = 0;
+               arguments->group = 0;
+               break;
+       
+       case ARGP_KEY_NO_ARGS:
+               argp_usage(state);
+               break;
+
+       default:
+               return ARGP_ERR_UNKNOWN;
+       }
+                                       
 
 /**
  * The program's main-function
@@ -71,9 +124,6 @@ static void usage(void)
  */
 int main(int argc, char *argv[])
 {
-       int optc;
-       int opt_index;
-
        int status = 0;
        int waitstat = 0;
 
@@ -90,21 +140,26 @@ int main(int argc, char *argv[])
        int mode = F_DEFAULT;
        int newmode = F_DEFAULT;
 
-       struct option const options[] = {
-               { "passwd", no_argument, 0, 'p' },
-               { "group", no_argument, 0, 'g' },
-               { "shadow", no_argument, 0, 's' },
-               { "help", no_argument, 0, 'h' },
-               { "version", no_argument, 0, 'V' },
-               { 0, 0, 0, 0 }
+       /* argp parser */
+       struct argp argp = {
+               options, parse_opt, args_doc, doc, NULL, NULL, NULL
        };
 
+       struct arguments args;
+
+       argp_program_version_hook = version;
+
        errno = 0;
 
        /* 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;
@@ -152,48 +207,6 @@ int main(int argc, char *argv[])
                                  "Please define the environment "
                                  "variable $EDITOR.\n"),
                                progname);
-                       status = EINVAL;
-                       goto EXIT;
-               }
-       }
-
-       /* Parse the command-line options */
-       while ((optc = getopt_long(argc, argv, "pgs",
-                                  options, &opt_index)) != EOF) {
-               switch (optc) {
-               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 '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 's':
-                       shadowedit = 1;
-                       break;
-               case 'h':
-                       usage();
-                       goto EXIT;
-               case 'V':
-                       version();
-                       goto EXIT;
-               default:
-                       usage();
                        status = EINVAL;
                        goto EXIT;
                }

reply via email to

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