]> granicus.if.org Git - shadow/commitdiff
* src/expiry.c, man/expiry.1.xml: Add support for long options.
authornekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Sun, 6 Nov 2011 18:39:30 +0000 (18:39 +0000)
committernekral-guest <nekral-guest@5a98b0ae-9ef6-0310-add3-de5d479b70d7>
Sun, 6 Nov 2011 18:39:30 +0000 (18:39 +0000)
* src/expiry.c, man/expiry.1.xml: Add -h/--help option

ChangeLog
man/expiry.1.xml
src/expiry.c

index 7cd4dd04262b18c5f813d865f5fa8298c7171b99..5884dd7eb7c9df576a468c0d96b04b1bc7ef034b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2011-10-30  Nicolas François  <nicolas.francois@centraliens.net>
+
+       * src/expiry.c, man/expiry.1.xml: Add support for long options.
+       * src/expiry.c, man/expiry.1.xml: Add -h/--help option
+
 2011-10-30  Nicolas François  <nicolas.francois@centraliens.net>
 
        * src/chfn.c, man/chfn.1.xml: Add support for long options.
index eebaddc42c8ca724c07b93ac58399616f4d00aa5..35f659fc621b5c3e64e2ebab178be0bb0592f638 100644 (file)
@@ -2,7 +2,7 @@
 <!--
    Copyright (c) 1990 - 1994, Julianne Frances Haugh
    Copyright (c) 1999       , Ben Collins
-   Copyright (c) 2007 - 2008, Nicolas François
+   Copyright (c) 2007 - 2011, Nicolas François
    All rights reserved.
   
    Redistribution and use in source and binary forms, with or without
@@ -53,8 +53,9 @@
   <refsynopsisdiv id='synopsis'>
     <cmdsynopsis>
       <command>expiry</command>
-      <arg choice='opt'>-c </arg>
-      <arg choice='opt'>-f </arg>
+      <arg choice='plain'>
+       <replaceable>option</replaceable>
+      </arg>
     </cmdsynopsis>
   </refsynopsisdiv>
 
     </para>
   </refsect1>
 
+  <refsect1 id='options'>
+    <title>OPTIONS</title>
+    <para>
+      The options which apply to the <command>expiry</command> command are:
+    </para>
+    <variablelist remap='IP'>
+      <varlistentry>
+       <term><option>-c</option>, <option>--check</option></term>
+       <listitem>
+         <para>Check the password expiration of the current user.</para>
+       </listitem>
+      </varlistentry>
+      <varlistentry>
+       <term><option>-f</option>, <option>--force</option></term>
+       <listitem>
+         <para>
+           Force a password change if the current user has an expired
+           password.
+         </para>
+       </listitem>
+      </varlistentry>
+      <varlistentry>
+       <term><option>-h</option>, <option>--help</option></term>
+       <listitem>
+         <para>Display help message and exit.</para>
+       </listitem>
+      </varlistentry>
+    </variablelist>
+  </refsect1>
+
   <refsect1 id='files'>
     <title>FILES</title>
     <variablelist>
index 04513f3ba29ed77d5143659cf53197e7533f9708..643791ee494380cda7c0bb3ea5f9fa43823ca4f5 100644 (file)
 #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;
 }