bug-sysutils
[Top][All Lists]
Advanced

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

Re: [Bug-sysutils] argp patch for vipw


From: Barry deFreese
Subject: Re: [Bug-sysutils] argp patch for vipw
Date: Wed, 26 May 2004 15:34:47 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5

address@hidden wrote:

Quoting Barry deFreese <address@hidden>:

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

At a cursory glance, this seems to look just the way I want things.  I'll
merge it into my tree this evening.

(For other list-readers, some explanations may be in order: me and Barry has
had quite some off-list discussions and sent stuff back and forth, hence the
call to some functions that does not exist in the CVS-tree, etc.  I have no
possibility to check in things at the moment, and I will be unable to until
Thursday, so expect a big checkin this weekend, at the very least including
wall, lastlog, new options to a few programs, Barry's argp-changes, and
major code cleanup.)


Regards: David Weinehall

OK, here is another patch for vipw that should be cleaner. And it builds!! Though it doesn't appear to be executing the code properly?? It fails when it should but giving it valid options it does nothing??

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/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  26 May 2004 23:20:53 -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,92 @@
 #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 args_doc[] = "";
+
+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':
+               args->shadow = 1;
+               break;
+
+       case 'p':
+               if (args->passwd == 0) {
+                       fprintf(stderr,
+                               _("%s: `-p' cannot be combined with `-g'\n"
+                                 "Try `%s --help' for more information.\n"),
+                               progname, progname);
+                       return ARGP_ERR_UNKNOWN;
+               } else {
+                       args->passwd = 1;
+                       args->group = 0;
+                       break;
+               }
+
+       case 'g':
+               if (args->group == 0) {
+                       fprintf(stderr,
+                               _("%s: `-p' cannot be combined with `-g'\n"
+                                 "Try `%s --help' for more information.\n"),
+                               progname, progname);
+                       return ARGP_ERR_UNKNOWN;
+               } 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:
+               args->args = arg;
+               argp_usage(state);
+               break;
+
+       case ARGP_KEY_NO_ARGS:
+               argp_usage(state);
+               break;
+
+       default:
+               return ARGP_ERR_UNKNOWN;
+       }
+       return 0;
+}                                      
 
 /**
  * The program's main-function
@@ -71,9 +134,6 @@ static void usage(void)
  */
 int main(int argc, char *argv[])
 {
-       int optc;
-       int opt_index;
-
        int status = 0;
        int waitstat = 0;
 
@@ -86,38 +146,42 @@ int main(int argc, char *argv[])
        char *cmd = NULL;
        char *c;
 
-       int shadowedit = 0;
        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;
-               shadowedit = 0;
+               args.shadow = 0;
        } else if (!strcmp(argv[0], PRG_NAME2)) {
                mode = F_GROUP;
-               shadowedit = 0;
+               args.shadow = 0;
        } else if (!strcmp(argv[0], PRG_NAME3)) {
                mode = F_PASSWD;
-               shadowedit = 1;
+               args.shadow = 1;
        } else if (!strcmp(argv[0], PRG_NAME4)) {
                mode = F_GROUP;
-               shadowedit = 1;
+               args.shadow = 1;
        }
 
        /* Find a sensible editor to use */
@@ -157,48 +221,6 @@ int main(int argc, char *argv[])
                }
        }
 
-       /* 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;
-               }
-       }
-
        /* If a different mode than default has been requesed, change mode */
        if (newmode != F_DEFAULT)
                mode = newmode;
@@ -209,12 +231,12 @@ int main(int argc, char *argv[])
 
        /* Create the proper file-name to copy to */
        if (mode == F_PASSWD) {
-               if (shadowedit)
+               if (args.shadow)
                        fname = SHADOW_FILE;
                else
                        fname = PASSWD_FILE;
        } else {
-               if (shadowedit)
+               if (args.shadow)
                        fname = GSHADOW_FILE;
                else
                        fname = GROUP_FILE;
@@ -232,16 +254,6 @@ int main(int argc, char *argv[])
                                _("%s: `%s' failed; %s\n"),
                                progname, "is_root", strerror(errno));
                }
-               goto EXIT;
-       }
-
-       /* Make sure we get the correct number of arguments */
-       if (argc > optind) {
-               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]