bug-sysutils
[Top][All Lists]
Advanced

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

[Bug-sysutils] added verbose option for rmuser


From: Barry deFreese
Subject: [Bug-sysutils] added verbose option for rmuser
Date: Fri, 04 Jun 2004 08:34:51 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5

OK, I have added a verbose option to rmuser to warn if one of the users specified doesn't exist.

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/rmuser.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/rmuser.c,v
retrieving revision 1.3
diff -u -p -r1.3 rmuser.c
--- src/rmuser.c        27 May 2004 07:50:33 -0000      1.3
+++ src/rmuser.c        4 Jun 2004 16:22:13 -0000
@@ -20,6 +20,7 @@
  *  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <argp.h>
 #include <grp.h>
 #include <pwd.h>
 #include <errno.h>
@@ -34,23 +35,54 @@
 
 #define PRG_NAME "rmuser"
 
-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_("Remove the specified users."
+          "\n"
+          "USERS should be a comma-separated list of users");
+
+static char args_doc[] = N_("USER(S)");
+
+static struct argp_option options[] = {
+       {"verbose", 'v', 0, OPTION_ARG_OPTIONAL,
+         N_("warn if the specified users do not exist"), 0 },
+       { 0, 0, 0, 0, 0, 0 }
+};
+
+struct arguments {
+       char *users;
+       long verbose;
+};
+
+/* parse a single option */
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
 {
-       fprintf(stdout,
-               _("Usage: %s USERS\n"
-                 "Remove the specified users.\n"
-                 "\n"
-                 "USERS should be a comma-separated list of users\n"
-                 "\n"),
-               progname);
-       usage_help();
+       struct arguments *args = state->input;
+
+       switch (key) {
+       case 'v':
+               args->verbose = 1;
+               break;
+
+       case ARGP_KEY_INIT:
+               args->verbose = 0;
+               args->users = NULL;
+               break;
+
+       case ARGP_KEY_ARG:
+               args->users = arg;
+               break;
+
+       case ARGP_KEY_NO_ARGS:
+               argp_usage(state);
+
+       default:
+               return ARGP_ERR_UNKNOWN;
+       }
+       return 0;
 }
 
 /**
@@ -80,10 +112,7 @@ int main(int argc, char *argv[])
 
        int empty = 1;
 
-       int optc;
-       int opt_index;
-
-       int status = 0;
+       error_t status = 0;
 
        char **usrarray = NULL;
 
@@ -98,35 +127,30 @@ int main(int argc, char *argv[])
 
        uid_t i; /* We're scanning <= max(nrofusers), hence uid_t */
 
-       struct option const options[] = {
-               { "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;
 
-       /* Parse the command-line options */
-       while ((optc = getopt_long(argc, argv, "",
-                                  options, &opt_index)) != -1) {
-               switch (optc) {
-               case 'h':
-                       usage();
-                       goto EXIT;
-
-               case 'V':
-                       version();
-                       goto EXIT;
-
-               default:
-                       usage();
-                       status = EINVAL;
-                       goto EXIT;
-               }
+       set_author_information(_("Written by David Weinehall.\n"));
+
+       /* Parse command line */
+       if ((status = argp_parse(&argp, argc, argv, 0, 0, &args))) {
+               if (status != EINVAL)
+                       fprintf(stderr,
+                               _("%s: '%s' failed; %s\n"),
+                               progname, _("argp_parse()"), strerror(status));
+               goto EXIT;
        }
 
        /* Make sure the caller has root privileges */
@@ -145,31 +169,23 @@ int main(int argc, char *argv[])
                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;
-       }
-
        /* Get the list of users to remove */
-       if (!(usrarray = strsplit(argv[argc - 1], ",", 0))) {
+       if (!(usrarray = strsplit(args.users, ",", 0))) {
                fprintf(stderr,
                        _("%s: `%s' failed; %s\n"),
                        progname, "strsplit", strerror(errno));
                status = errno;
                goto EXIT;
        }
+
+       if (args.verbose)
+               for (i = 0; usrarray[i]; i++) {
+                       if (!(getpwnam(usrarray[i])))
+                               fprintf(stderr,
+                                       _("%s: warning: %s '%s' "
+                                         "does not exist ... skipping\n"),
+                                       progname, _("user"), usrarray[i]);
+               }
 
        /* Create filename /etc/passwd.write */
        if (!(pwwname = create_filename(PASSWD_FILE, WRITE_EXT))) {

reply via email to

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