* src/expiry.c, man/expiry.1.xml: Add support for long options.

* src/expiry.c, man/expiry.1.xml: Add -h/--help option
This commit is contained in:
nekral-guest
2011-11-06 18:39:30 +00:00
parent 0530588266
commit b9163f6348
3 changed files with 113 additions and 15 deletions

View File

@@ -38,15 +38,20 @@
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
#include "defines.h"
#include "prototypes.h"
/*@-exitarg@*/
#include "exitcodes.h"
/* Global variables */
const char *Prog;
static bool cflg = false;
/* local function prototypes */
static RETSIGTYPE catch_signals (int);
static void usage (void);
static /*@noreturn@*/void usage (int status);
static void process_flags (int argc, char **argv);
/*
* catch_signals - signal catcher
@@ -59,10 +64,72 @@ static RETSIGTYPE catch_signals (unused int sig)
/*
* usage - print syntax message and exit
*/
static void usage (void)
static /*@noreturn@*/void usage (int status)
{
fputs (_("Usage: expiry {-f|-c}\n"), stderr);
exit (10);
FILE *usageout = (E_SUCCESS != status) ? stderr : stdout;
(void) fprintf (usageout,
_("Usage: %s [options]\n"
"\n"
"Options:\n"),
Prog);
(void) fputs (_(" -c, --check check the user's password expiration\n"), usageout);
(void) fputs (_(" -f, --force force password change if the user's password\n"
" is expired\n"), usageout);
(void) fputs (_(" -h, --help display this help message and exit\n"), usageout);
(void) fputs ("\n", usageout);
exit (status);
}
/*
* process_flags - parse the command line options
*
* It will not return if an error is encountered.
*/
static void process_flags (int argc, char **argv)
{
bool fflg = false;
int c;
static struct option long_options[] = {
{"check", no_argument, NULL, 'c'},
{"force", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, '\0'}
};
while ((c = getopt_long (argc, argv, "cfh",
long_options, NULL)) != -1) {
switch (c) {
case 'c':
cflg = true;
break;
case 'f':
fflg = true;
break;
case 'h':
usage (E_SUCCESS);
/*@notreached@*/break;
default:
usage (E_USAGE);
}
}
if (! (cflg || fflg)) {
usage (E_USAGE);
}
if (cflg && fflg) {
fprintf (stderr,
_("%s: options %s and %s conflict\n"),
Prog, "-c", "-f");
usage (E_USAGE);
}
if (argc != optind) {
fprintf (stderr,
_("%s: unexpected argument: %s\n"),
Prog, argv[optind]);
usage (E_USAGE);
}
}
/*
@@ -100,11 +167,7 @@ int main (int argc, char **argv)
OPENLOG ("expiry");
if ( (argc != 2)
|| ( (strcmp (argv[1], "-f") != 0)
&& (strcmp (argv[1], "-c") != 0))) {
usage ();
}
process_flags (argc, argv);
/*
* Get user entries for /etc/passwd and /etc/shadow
@@ -122,8 +185,7 @@ int main (int argc, char **argv)
/*
* If checking accounts, use agecheck() function.
*/
if (strcmp (argv[1], "-c") == 0) {
if (cflg) {
/*
* Print out number of days until expiration.
*/
@@ -143,6 +205,6 @@ int main (int argc, char **argv)
*/
expire (pwd, spwd);
exit (0);
return E_SUCCESS;
}