[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-sysutils] Updated argp patch for cppw.c
From: |
Barry deFreese |
Subject: |
[Bug-sysutils] Updated argp patch for cppw.c |
Date: |
Sat, 29 May 2004 06:56:42 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5 |
David,
OK, I changed cppw to use argp_error instead of trapping the return code.
Thank you,
--
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 29 May 2004 14:44:56 -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,80 @@
#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) {
+ 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:
+ args->args = arg;
+ argp_usage(state);
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
+}
/**
* The program's main-function
@@ -71,9 +122,6 @@ static void usage(void)
*/
int main(int argc, char *argv[])
{
- int optc;
- int opt_index;
-
int status = 0;
int waitstat = 0;
@@ -86,38 +134,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 +209,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 +219,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 +242,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;
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Bug-sysutils] Updated argp patch for cppw.c,
Barry deFreese <=