[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-sysutils] argp patch for lsage
From: |
Barry deFreese |
Subject: |
[Bug-sysutils] argp patch for lsage |
Date: |
Mon, 31 May 2004 19:24:07 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5 |
David,
OK, here is a patch for lsage. I change the functionality a little bit
about how it handles no arguments (i.e. get info for the current user).
Please let me know what you think.
Everything works except "lsage ALL". get_all_users is failing on me
from sysutils.c. This happens in my stuff and the clean tree in both
lsage and lsuser. Before I go trying to debug that, can you test and
see if you get similar results or if this is still part of my funky
problem? I am getting that same 'function' failed: Function not
implemented crap??? :-(
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.
Index: src/lsage.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/lsage.c,v
retrieving revision 1.2
diff -u -p -r1.2 lsage.c
--- src/lsage.c 27 May 2004 07:50:33 -0000 1.2
+++ src/lsage.c 1 Jun 2004 03:10:26 -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>
@@ -33,38 +34,91 @@
#define PRG_NAME "lsage"
-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_("List password expiry information for the specified "
+ "users."
+ "\n"
+ "USERS should be a comma-separated list of users, "
+ "or the word ALL to show expiry information for all users."
+ "\n"
+ "If no username is specified, "
+ "the information for the current user is shown.");
+
+static char args_doc[] = N_("[USER(s)]");
+
+static struct argp_option options[] = {
+ {"attr", 'a', N_("ATTRS"), OPTION_ARG_OPTIONAL,
+ N_("display only the attributes specified "
+ "by a comma-separated list;\n"
+ "valid attributes are:\n"
+ "min, max, warn, inact, lstchg, expire, and locked"), 0 },
+ {"colon", 'c', 0, OPTION_ARG_OPTIONAL,
+ N_("display the attributes as colon-separated records"), 0 },
+ {"stanza", 'f', 0, OPTION_ARG_OPTIONAL,
+ N_("display the attributes as stanzas"), 0 },
+ { 0, 0, 0, 0, 0, 0 }
+};
+
+/* Available arguments */
+struct arguments {
+ char *username;
+ char *attrs;
+ long colon;
+ long stanza;
+ long normal;
+ char *users;
+};
+
+/* Parse a single option */
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
- fprintf(stdout,
- _("Usage: %s [OPTION]... [USERS]\n"
- "List password expiry information for the specified "
- "users.\n"
- "\n"
- "USERS should be a comma-separated list of users,\n"
- "or the word ALL to show expiry information for all users.\n"
- "\n"
- "If no username is specified, "
- "the information for the current user is shown.\n"
- "\n"
- " -a, --attr=[ATTRS] display only the attributes "
- "specified\n"
- " by a comma-separated list; "
- "valid attributes are:\n"
- " %s, %s, %s, %s, %s, %s, and %s\n"
- " -c, --colon display the attributes as "
- "colon-separated records\n"
- " -f, --stanza display the attributes "
- "as stanzas\n"),
- progname, AS_MIN, AS_MAX, AS_WARN, AS_INACT, AS_LSTCHG,
- AS_EXPIRE, AS_LOCKED);
- usage_help();
+ struct arguments *args = state->input;
+
+ switch (key) {
+ case 'a':
+ args->attrs = arg;
+ break;
+
+ case 'c':
+ args->colon = 1;
+ args->normal = 0;
+ break;
+
+ case 'f':
+ args->stanza = 1;
+ args->normal = 0;
+ break;
+
+ case ARGP_KEY_INIT:
+ args->username = NULL;
+ args->attrs = NULL;
+ args->colon = 0;
+ args->stanza = 0;
+ args->normal = 1;
+ args->users = NULL;
+ break;
+
+ case ARGP_KEY_NO_ARGS:
+ if (!(args->users = get_username(getuid())))
+ argp_error(state, "unable to get current user name");
+
+ break;
+
+ case ARGP_KEY_ARG:
+ if (args->users)
+ argp_usage(state);
+
+ args->users = arg;
+ break;
+
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+ return 0;
}
/**
@@ -76,15 +130,10 @@ static void usage(void)
*/
int main(int argc, char *argv[])
{
- int optc;
- int opt_index;
-
int status = 0;
int isadmin = 0;
- int mode = M_NORMAL;
-
unsigned int attr = A_ALL;
char **usrarray = NULL;
@@ -94,124 +143,94 @@ int main(int argc, char *argv[])
uid_t i; /* We're scanning <= max(nrofusers), hence uid_t */
- struct option const options[] = {
- { "attr", optional_argument, 0, 'a' },
- { "colon", no_argument, 0, M_COLON },
- { "stanza", no_argument, 0, M_STANZA },
- { "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, "a::cf",
- options, &opt_index)) != -1) {
- char **attrarray = NULL;
-
- switch (optc) {
- case 'a':
- attr = A_NAME;
+ set_author_information(_("Written by David Weinehall.\n"));
- if (!optarg)
- break;
+ /* Parse command line */
+ argp_parse(&argp, argc, argv, 0, 0, &args);
- if (!(attrarray = strsplit(optarg, ",", 0))) {
- fprintf(stderr,
- _("%s: `%s' failed; %s\n"),
- progname, "strsplit", strerror(errno));
- status = errno;
- goto EXIT;
- }
+ char **attrarray = NULL;
- for (i = 0; attrarray[i]; i++) {
- if (!strcmp(attrarray[i], AS_MIN)) {
- attr |= A_MIN;
- } else if (!strcmp(attrarray[i], AS_MAX)) {
- attr |= A_MAX;
- } else if (!strcmp(attrarray[i], AS_WARN)) {
- attr |= A_WARN;
- } else if (!strcmp(attrarray[i], AS_INACT)) {
- attr |= A_INACT;
- } else if (!strcmp(attrarray[i], AS_LSTCHG)) {
- attr |= A_LSTCHG;
- } else if (!strcmp(attrarray[i], AS_EXPIRE)) {
- attr |= A_EXPIRE;
- } else if (!strcmp(attrarray[i], AS_LOCKED)) {
- attr |= A_LOCKED;
- } else if (!strcmp(attrarray[i], AS_A_LOCKED)) {
- attr |= A_LOCKED;
- } else {
- fprintf(stderr,
- _("%s: invalid attributes "
- "for `--attr'\n"
- "Valid attributes are:\n"
- "- `%s'\n"
- "- `%s'\n"
- "- `%s'\n"
- "- `%s'\n"
- "- `%s'\n"
- "- `%s'\n"
- "Try `%s --help' for more "
- "information.\n"),
- progname, AS_ID,
- AS_PGRP, AS_GROUPS, AS_HOME,
- AS_SHELL, AS_GECOS, progname);
- status = EINVAL;
- strfreev(attrarray);
- goto EXIT;
- }
- }
+ if (args.attrs) {
+ attr = A_NAME;
- strfreev(attrarray);
- break;
+ if (!(attrarray = strsplit(args.attrs, ",", 0))) {
+ fprintf(stderr,
+ _("%s: `%s' failed; %s\n"),
+ progname, "strsplit", strerror(errno));
+ status = errno;
+ goto EXIT;
+ }
- case M_COLON:
- case M_STANZA:
- if (mode != M_NORMAL && mode != (char)optc) {
+ for (i = 0; attrarray[i]; i++) {
+ if (!strcmp(attrarray[i], AS_MIN)) {
+ attr |= A_MIN;
+ } else if (!strcmp(attrarray[i], AS_MAX)) {
+ attr |= A_MAX;
+ } else if (!strcmp(attrarray[i], AS_WARN)) {
+ attr |= A_WARN;
+ } else if (!strcmp(attrarray[i], AS_INACT)) {
+ attr |= A_INACT;
+ } else if (!strcmp(attrarray[i], AS_LSTCHG)) {
+ attr |= A_LSTCHG;
+ } else if (!strcmp(attrarray[i], AS_EXPIRE)) {
+ attr |= A_EXPIRE;
+ } else if (!strcmp(attrarray[i], AS_LOCKED)) {
+ attr |= A_LOCKED;
+ } else if (!strcmp(attrarray[i], AS_A_LOCKED)) {
+ attr |= A_LOCKED;
+ } else {
fprintf(stderr,
- _("%s: `-c' and `-f' cannot be "
- "combined\n"
+ _("%s: invalid attributes "
+ "for `--attr'\n"
+ "Valid attributes are:\n"
+ "- `%s'\n"
+ "- `%s'\n"
+ "- `%s'\n"
+ "- `%s'\n"
+ "- `%s'\n"
+ "- `%s'\n"
"Try `%s --help' for more "
"information.\n"),
- progname, progname);
+ progname, AS_ID,
+ AS_PGRP, AS_GROUPS, AS_HOME,
+ AS_SHELL, AS_GECOS, progname);
status = EINVAL;
+ strfreev(attrarray);
goto EXIT;
}
+ }
- mode = (char)optc;
- break;
-
- case 'h':
- usage();
- goto EXIT;
-
- case 'V':
- version();
- goto EXIT;
+ strfreev(attrarray);
+ }
- default:
- usage();
+ if (args.stanza) {
+ if (args.colon && args.stanza) {
+ fprintf(stderr,
+ _("%s: `-c' and `-f' cannot be "
+ "combined\n"
+ "Try `%s --help' for more "
+ "information.\n"),
+ progname, progname);
status = EINVAL;
goto EXIT;
}
}
- /* Make sure we get the correct number of arguments */
- if ((argc - optind) > 1) {
- fprintf(stderr,
- _("%s: Too many arguments\n"
- "Try `%s --help' for more information.\n"),
- progname, progname);
- status = EINVAL;
- goto EXIT;
- }
-
/* Check if the user is a user-administrator */
if ((status = is_useradmin())) {
if (status == EPERM) {
@@ -229,8 +248,8 @@ int main(int argc, char *argv[])
/* Unless the user requests information about himself,
* or is a user-administrator, deny the request
*/
- if (optind != argc && !isadmin) {
- int retval = is_caller(argv[argc - 1]);
+ if (!args.users && !isadmin) {
+ int retval = is_caller(args.users);
if (retval == -1) {
status = errno;
@@ -255,23 +274,8 @@ int main(int argc, char *argv[])
* user-entries. Since the latter is probably the most common,
* the latter has been chosen.
*/
- if (optind == argc) {
- char *tmp = NULL;
- if (!(tmp = get_username(getuid()))) {
- status = errno;
- } else if (!(usrarray = strsplit(tmp, ",", 0))) {
- fprintf(stderr,
- _("%s: `%s' failed; %s\n"),
- progname, "strsplit", strerror(errno));
- status = errno;
- }
-
- free(tmp);
-
- if (status)
- goto EXIT;
- } else if (!strcmp(argv[argc - 1], "ALL")) {
+ if (args.users && !strcmp(args.users, "ALL")) {
char *tmp = NULL;
if (!(tmp = get_all_users())) {
@@ -294,7 +298,23 @@ int main(int argc, char *argv[])
if (status)
goto EXIT;
} else {
- if (!(usrarray = strsplit(argv[argc - 1], ",", 0))) {
+ if ((status = is_valid_namelist(args.users))) {
+ if (status == EINVAL) {
+ fprintf(stderr,
+ _("%s: the specified list of %s "
+ "contains invalid characters\n"),
+ progname, _("users"));
+ } else {
+ fprintf(stderr,
+ _("%s: '%s' failed; %s\n"),
+ progname, "is_valid_namelist",
+ strerror(errno));
+ }
+
+ goto EXIT;
+ }
+
+ if (!(usrarray = strsplit(args.users, ",", 0))) {
fprintf(stderr,
_("%s: `%s' failed; %s\n"),
progname, "strsplit", strerror(errno));
@@ -344,7 +364,7 @@ int main(int argc, char *argv[])
}
/* Output all information */
- if (mode == M_NORMAL) {
+ if (args.normal) {
fprintf(stdout, "%s", sp->sp_namp);
if (attr & A_MIN)
@@ -382,7 +402,7 @@ int main(int argc, char *argv[])
" %s=%s",
_("locked"),
locked ? _("yes") : _("no"));
- } else if (mode == M_COLON) {
+ } else if (args.colon) {
fprintf(stdout,
"#%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
_("name"),
@@ -465,7 +485,7 @@ int main(int argc, char *argv[])
fprintf(stdout, "\n");
- if (mode == M_STANZA)
+ if (args.stanza)
fprintf(stdout, "\n");
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Bug-sysutils] argp patch for lsage,
Barry deFreese <=