From: Todd C. Miller Date: Wed, 21 Jan 2004 23:55:39 +0000 (+0000) Subject: Allow non-exclusive flags when invoked as sudoedit. X-Git-Tag: SUDO_1_6_8~216 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d1798a06b4180004e82e03e230e82970570acbca;p=sudo Allow non-exclusive flags when invoked as sudoedit. Pretty print the long usage() line to not wrap (assumes 80 char display) --- diff --git a/sudo.c b/sudo.c index 986ce2e69..d8292180a 100644 --- a/sudo.c +++ b/sudo.c @@ -665,16 +665,14 @@ parse_args(argc, argv) NewArgv = argv + 1; NewArgc = argc - 1; - /* Check to see if we were invoked as "sudoedit". */ + /* FIrst, check to see if we were invoked as "sudoedit". */ if (strcmp(getprogname(), "sudoedit") == 0) { rval = MODE_EDIT; - if (NewArgc == 0 || NewArgv[0][0] == '-') - usage(1); - return(rval); - } + excl = 'e'; + } else + rval = MODE_RUN; - rval = MODE_RUN; - if (NewArgc == 0) { /* no options and no command */ + if (NewArgc == 0 && rval == MODE_RUN) { /* no options and no command */ rval |= (MODE_IMPLIED_SHELL | MODE_SHELL); return(rval); } @@ -736,6 +734,7 @@ parse_args(argc, argv) rval = MODE_EDIT; if (excl && excl != 'e') usage_excl(1); + excl = 'e'; break; case 'v': rval = MODE_VALIDATE; @@ -743,6 +742,13 @@ parse_args(argc, argv) usage_excl(1); excl = 'v'; break; + case 'i': + rval |= (MODE_LOGIN_SHELL | MODE_SHELL); + def_env_reset = TRUE; + if (excl && excl != 'i') + usage_excl(1); + excl = 'i'; + break; case 'k': rval = MODE_INVALIDATE; if (excl && excl != 'k') @@ -779,10 +785,6 @@ parse_args(argc, argv) usage_excl(1); excl = 'h'; break; - case 'i': - rval |= MODE_LOGIN_SHELL; - def_env_reset = TRUE; - /* FALLTHROUGH */ case 's': rval |= MODE_SHELL; if (excl && excl != 's') @@ -1081,22 +1083,53 @@ static void usage(exit_val) int exit_val; { - if (strcmp(getprogname(), "sudoedit") == 0) { - fprintf(stderr, "usage: sudoedit file [...]\n"); - exit(exit_val); - } - - fprintf(stderr, "usage: sudo -K | -L | -V | -h | -k | -l | -v\n"); - fprintf(stderr, "usage: sudo [-HPSb]%s%s [-p prompt] [-u username|#uid]\n { -e file [...] | -i | -s | }\n", + char **p; + int linelen, linemax, ulen; + static char *uvec[] = { + " [-HPSb]", #ifdef HAVE_BSD_AUTH_H " [-a auth_type]", -#else - "", #endif #ifdef HAVE_LOGIN_CAP_H - " [-c class|-]"); -#else - ""); + " [-c class|-]", #endif + " [-p prompt]", + " [-u username|#uid]", + " { -e file [...] | -i | -s | }", + NULL + }; + + /* + * For sudoedit, replace the last entry in the usage vector. + * For sudo, print the secondary usage. + */ + if (strcmp(getprogname(), "sudoedit") == 0) { + /* Replace the last entry in the usage vector. */ + for (p = uvec; p[1] != NULL; p++) + continue; + *p = " file [...]"; + } else { + fprintf(stderr, "usage: %s -K | -L | -V | -h | -k | -l | -v\n", + getprogname()); + } + + /* + * Print the main usage and wrap lines as needed. + * Assumes an 80-character wide terminal, which is kind of bogus... + */ + ulen = (int)strlen(getprogname()) + 7; + linemax = 80; + linelen = linemax - ulen; + printf("usage: %s", getprogname()); + for (p = uvec; *p != NULL; p++) { + if (linelen == linemax || (linelen -= strlen(*p)) >= 0) { + fputs(*p, stdout); + } else { + p--; + linelen = linemax; + printf("\n%*s", ulen, ""); + } + } + putchar('\n'); exit(exit_val); }