[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-sysutils] argp patch for cppw.c
From: |
Barry deFreese |
Subject: |
[Bug-sysutils] argp patch for cppw.c |
Date: |
Thu, 27 May 2004 18:42:32 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5 |
OK, here are the argp changes for cppw.c. It builds and seems to work
properly. I'm getting a little better.. :-)
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 28 May 2004 02:31:03 -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,91 @@
#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) {
+ fprintf(stderr,
+ _("%s: `-p' cannot be combined with `-g'\n"
+ "Try `%s --help' for more information.\n"),
+ progname, progname);
+ return -1;
+ } 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 -1;
+ } 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,10 +131,8 @@ static void usage(void)
*/
int main(int argc, char *argv[])
{
- int optc;
- int opt_index;
-
int status = 0;
+ int parse_rc = 0; /* Return code from parse_opt() */
char *bname = NULL;
char *cname = NULL;
@@ -84,83 +143,41 @@ 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 */
+ parse_rc = argp_parse(&argp, argc, argv, 0, 0, &args);
+ if (parse_rc != 0)
+ goto EXIT;
+
/* 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 +214,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;
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Bug-sysutils] argp patch for cppw.c,
Barry deFreese <=