merge with 1.8.1b
authorJim Meyering <jim@meyering.net>
Tue, 12 Oct 1993 01:52:24 +0000 (01:52 +0000)
committerJim Meyering <jim@meyering.net>
Tue, 12 Oct 1993 01:52:24 +0000 (01:52 +0000)
21 files changed:
lib/getdate.y
old/sh-utils/ChangeLog
src/basename.c
src/date.c
src/dirname.c
src/echo.c
src/env.c
src/expr.c
src/id.c
src/logname.c
src/nice.c
src/pathchk.c
src/printf.c
src/sleep.c
src/stty.c
src/su.c
src/tee.c
src/test.c
src/tty.c
src/uname.c
src/who.c

index e5cf18a..e15a886 100644 (file)
@@ -41,7 +41,7 @@
 #ifdef _AIX /* for Bison */
  #pragma alloca
 #else
-char *alloca ();
+void *alloca ();
 #endif
 #endif
 #endif
index f51c3b4..0cad2f5 100644 (file)
@@ -1,3 +1,38 @@
+Sun Oct 10 14:05:23 1993  Jim Meyering  meyering@comco.com
+
+       * false.sh, groups.sh, nohup.sh, true.sh: Add --help and --version.
+       * src/Makefile.in (false, groups, nohup, true): Substitute the string
+       from version.c for @VERSION@ in *.sh.
+
+Sat Oct  9 23:12:53 1993  Jim Meyering  meyering@comco.com
+
+       * configure.in: Remove AC_UNISTD_H; add unistd.h to AC_HAVE_HEADERS.
+
+Tue Oct  5 22:18:05 1993  Jim Meyering  meyering@comco.com
+
+       * tty.c (main): Detect write error.  Use isatty (rather than
+       testing whether ttyname() is NULL) to determine exit status
+       because ttyname may return NULL when stdin is a terminal device.
+
+       * expr.c (eval7): Rewrite if-else-if-else... sequence to avoid a
+       spurious `function returns without a value' warning.
+
+       * printenv.c (main), tty.c (main): Detect and report write errors.
+
+       * echo.c, patchchk.c, printenv.c, printf.c, sleep.c, stty.c, su.c,
+       tee.c, test.c, tty.c, uname.c, yes.c: Accept --help and --version
+       options.
+
+       * long-options.c: New file.  To let echo, expr, printf, test, and
+       yes accept --help and --version as unobtrusively as possible.
+       * src/Makefile.in [SORUCES]: Add it.
+
+       * configure.in: Add AC_TIME_WITH_SYS_TIME and AC_STAT_MACROS_BROKEN.
+
+       * nice.c (main) [NICE_PRIORITY]: When given an argument, don't
+       try to get current priority.  nice() requires only the delta;
+       we don't need to compute the absolute priority as for setpriority.
+
 Mon Oct 04 22:15:07 1993  Jim Meyering (meyering@comco.com)
 
        * who.c (print_entry, print_heading, who_am_i):
index 01e3d43..ec8cd5a 100644 (file)
@@ -43,7 +43,7 @@ char *program_name;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const long_options[] =
index e97ef43..b80c47f 100644 (file)
@@ -85,7 +85,7 @@ char *program_name;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const long_options[] =
index d6227db..a4209c8 100644 (file)
@@ -32,7 +32,7 @@ char *program_name;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const long_options[] =
index ddf3b17..2e27abe 100644 (file)
@@ -57,6 +57,19 @@ on System V systems with the -E option.
 #  define VALID_ECHO_OPTIONS "n"
 #endif /* !V9_ECHO */
 
+/* The name this program was run with. */
+char *program_name;
+
+void parse_long_options ();
+
+static void
+usage ()
+{
+  fprintf (stderr, "Usage: %s [{--help,--version}] [-ne] [string ...]\n",
+          program_name);
+  exit (1);
+}
+
 /* Print the words in LIST to standard output.  If the first word is
    `-n', then don't print a trailing newline.  We also support the
    echo syntax from Version 9 unix systems. */
@@ -67,6 +80,10 @@ main (argc, argv)
 {
   int display_return = 1, do_v9 = 0;
 
+  program_name = argv[0];
+
+  parse_long_options (argc, argv, usage);
+
 /* System V machines already have a /bin/sh with a v9 behaviour.  We
    use the identical behaviour for these machines so that the
    existing system shell scripts won't barf. */
index 283e88a..b08d483 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -98,7 +98,7 @@ char *program_name;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const longopts[] =
index a9f3769..97f7b39 100644 (file)
@@ -32,7 +32,6 @@
 #include <ctype.h>
 #include <sys/types.h>
 #include <regex.h>
-#include <getopt.h>
 
 #include "system.h"
 #include "version.h"
@@ -81,6 +80,7 @@ void error ();
 char *xstrdup ();
 char *strstr ();
 char *xmalloc ();
+void parse_long_options ();
 
 static VALUE *docolon ();
 static VALUE *eval ();
@@ -99,13 +99,6 @@ static void tostring ();
 static void trace ();
 #endif
 
-static struct option const long_options[] =
-{
-  {"help", no_argument, 0, 'h'},
-  {"version", no_argument, 0, 'v'},
-  {0, 0, 0, 0}
-};
-
 static void
 usage ()
 {
@@ -114,39 +107,6 @@ usage ()
   exit (1);
 }
 
-/* Process long options that precede all other command line arguments.  */
-
-static void
-parse_long_options (argc, argv)
-     int argc;
-     char **argv;
-{
-  int c;
-
-  while ((c = getopt_long (argc, argv, "+", long_options, (int *) 0)) != EOF)
-    {
-      switch (c)
-        {
-       case 'h':
-          usage ();
-
-       case 'v':
-         printf ("%s\n", version_string);
-         exit (0);
-       
-       default:
-         usage ();
-        }
-    }
-
-  /* Restore optind in case it has advanced past a leading `--'.  We can use a
-     simple assignment here because all brances of the above switch statement
-     exit.  Otherwise, we'd have to be careful to decrement only when optind
-     is larger than 1 and the last argument processed was `--'.  */
-
-  optind = 1;
-}
-
 void
 main (argc, argv)
      int argc;
@@ -156,7 +116,7 @@ main (argc, argv)
 
   program_name = argv[0];
 
-  parse_long_options (argc, argv);
+  parse_long_options (argc, argv, usage);
 
   if (argc == 1)
     usage ();
@@ -465,7 +425,8 @@ eval7 ()
 #endif
   if (nomoreargs ())
     error (2, 0, "syntax error");
-  else if (nextarg ("("))
+
+  if (nextarg ("("))
     {
       args++;
       v = eval ();
@@ -474,10 +435,11 @@ eval7 ()
       args++;
       return v;
     }
-  else if (nextarg (")"))
+
+  if (nextarg (")"))
     error (2, 0, "syntax error");
-  else
-    return str_value (*args++);
+
+  return str_value (*args++);
 }
 
 /* Handle match, substr, index, and length keywords.  */
index 7d55d71..bd18ed8 100644 (file)
--- a/src/id.c
+++ b/src/id.c
@@ -85,7 +85,7 @@ static int problems = 0;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const longopts[] =
index 19fea40..82c49fe 100644 (file)
@@ -28,7 +28,7 @@ char *program_name;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const long_options[] =
index 51218d8..28e68d9 100644 (file)
 #include "version.h"
 #include "system.h"
 
+#ifdef NICE_PRIORITY
+#define GET_PRIORITY() nice (0)
+#else
+#define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
+#endif
+
 void error ();
 
 static int isinteger ();
@@ -39,7 +45,7 @@ char *program_name;
 /* If non-zero, display usage information and exit.  */
 static int show_help;
 
-/* If non-zero, print the version on standard error.  */
+/* If non-zero, print the version on standard output and exit.  */
 static int show_version;
 
 static struct option const longopts[] =
@@ -111,27 +117,18 @@ main (argc, argv)
        usage ();
       /* No command given; print the priority. */
       errno = 0;
-#ifndef NICE_PRIORITY
-      current_priority = getpriority (PRIO_PROCESS, 0);
-#else
-      current_priority = nice (0);
-#endif
+      current_priority = GET_PRIORITY ();
       if (current_priority == -1 && errno != 0)
        error (1, errno, "cannot get priority");
       printf ("%d\n", current_priority);
       exit (0);
     }
 
-  errno = 0;
 #ifndef NICE_PRIORITY
-  current_priority = getpriority (PRIO_PROCESS, 0);
-#else
-  current_priority = nice (0);
-#endif
+  errno = 0;
+  current_priority = GET_PRIORITY ();
   if (current_priority == -1 && errno != 0)
     error (1, errno, "cannot get priority");
-
-#ifndef NICE_PRIORITY
   if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
 #else
   if (nice (adjustment) == -1)
index 7302e38..e6da04e 100644 (file)
@@ -41,6 +41,8 @@
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
+
+#include "version.h"
 #include "system.h"
 
 #ifdef _POSIX_VERSION
@@ -96,9 +98,17 @@ static void usage ();
 /* The name this program was run with. */
 char *program_name;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 static struct option const longopts[] =
 {
+  {"help", no_argument, &show_help, 1},
   {"portability", no_argument, NULL, 'p'},
+  {"version", no_argument, &show_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -117,14 +127,27 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'p':
          check_portability = 1;
          break;
+
        default:
          usage ();
        }
     }
 
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (optind == argc)
     usage ();
 
index 0c0da37..e1bd4d3 100644 (file)
 #include <stdio.h>
 #include <ctype.h>
 #include <sys/types.h>
+#include <getopt.h>
+
 #include "system.h"
+#include "version.h"
 
 #if !defined (isascii) || defined (STDC_HEADERS)
 #undef isascii
@@ -68,6 +71,7 @@ unsigned long strtoul ();
 
 char *xmalloc ();
 void error ();
+void parse_long_options ();
 
 static double xstrtod ();
 static int print_esc ();
@@ -79,11 +83,19 @@ static void print_esc_char ();
 static void print_esc_string ();
 static void verify ();
 
+/* The value to return to the calling program.  */
+static int exit_status;
+
 /* The name this program was run with. */
 char *program_name;
 
-/* The value to return to the calling program.  */
-static int exit_status;
+static void
+usage ()
+{
+  fprintf (stderr, "Usage: %s [{--help,--version}] format [argument...]\n",
+          program_name);
+  exit (1);
+}
 
 void
 main (argc, argv)
@@ -96,6 +108,8 @@ main (argc, argv)
   program_name = argv[0];
   exit_status = 0;
 
+  parse_long_options (argc, argv, usage);
+
   if (argc == 1)
     {
       fprintf (stderr, "Usage: %s format [argument...]\n", program_name);
index 1551eea..2858ff4 100644 (file)
@@ -17,6 +17,9 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <getopt.h>
+
+#include "version.h"
 #include "system.h"
 
 void error ();
@@ -26,6 +29,27 @@ static long argdecode ();
 /* The name by which this program was run. */
 char *program_name;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
+static struct option const long_options[] =
+{
+  {"help", no_argument, &show_help, 1},
+  {"version", no_argument, &show_version, 1},
+  {0, 0, 0, 0}
+};
+
+static void
+usage ()
+{
+  fprintf (stderr, "Usage: %s [{--help,--version}] number[smhd]...\n",
+          program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -33,9 +57,31 @@ main (argc, argv)
 {
   int i;
   unsigned seconds = 0;
+  int c;
 
   program_name = argv[0];
 
+  while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
+    {
+      switch (c)
+       {
+       case 0:
+         break;
+
+       default:
+         usage ();
+       }
+    }
+
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (argc == 1)
     {
       fprintf (stderr, "Usage: %s number[smhd]...\n", argv[0]);
index 1e0a59a..ea764ac 100644 (file)
@@ -45,7 +45,9 @@
 #include <varargs.h>
 #define VA_START(args, lastarg) va_start(args)
 #endif
+
 #include "system.h"
+#include "version.h"
 
 #if defined(GWINSZ_BROKEN)     /* Such as for SCO UNIX 3.2.2. */
 #undef TIOCGWINSZ
@@ -365,10 +367,18 @@ static int max_col;
 /* Current position, to know when to wrap. */
 static int current_col;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 static struct option longopts[] =
 {
   {"all", no_argument, NULL, 'a'},
+  {"help", no_argument, &show_help, 1},
   {"save", no_argument, NULL, 'g'},
+  {"version", no_argument, &show_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -411,6 +421,15 @@ wrapf (message, va_alist)
   current_col += buflen;
 }
 
+static void
+usage ()
+{
+  fprintf (stderr,
+      "Usage: %s [{--help,--version}] [-ag] [--all] [--save] [setting...]\n",
+          program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -425,14 +444,35 @@ main (argc, argv)
 
   while ((optc = getopt_long (argc, argv, "ag", longopts, (int *) 0)) != EOF)
     {
-      if (optc == 'a')
-       output_type = all;
-      else if (optc == 'g')
-       output_type = recoverable;
-      else
-       break;
+      switch (optc)
+       {
+       case 0:
+         break;
+
+       case 'a':
+         output_type = all;
+         break;
+
+       case 'g':
+         output_type = recoverable;
+         break;
+
+       default:
+         goto done;
+       }
+    }
+
+done:;
+
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
     }
 
+  if (show_help)
+    usage ();
+
   if (tcgetattr (0, &mode))
     error (1, errno, "standard input");
 
@@ -853,6 +893,9 @@ mode_type_flag (type, mode)
 
     case combination:
       return NULL;
+
+    default:
+      abort ();
     }
 }
 
index c94db90..7252a07 100644 (file)
--- a/src/su.c
+++ b/src/su.c
@@ -73,6 +73,7 @@
 #include <getopt.h>
 #include <sys/types.h>
 #include <pwd.h>
+#include <grp.h>
 #include "system.h"
 
 #ifdef HAVE_SYSLOG_H
@@ -115,6 +116,8 @@ uid_t getuid ();
 #include <shadow.h>
 #endif
 
+#include "version.h"
+
 /* The default PATH for simulated logins to non-superuser accounts.  */
 #define DEFAULT_LOGIN_PATH ":/usr/ucb:/bin:/usr/bin"
 
@@ -153,6 +156,12 @@ extern char **environ;
 /* The name this program was run with.  */
 char *program_name;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 /* If nonzero, pass the `-f' option to the subshell.  */
 static int fast_startup;
 
@@ -166,9 +175,11 @@ static struct option const longopts[] =
 {
   {"command", required_argument, 0, 'c'},
   {"fast", no_argument, &fast_startup, 1},
+  {"help", no_argument, &show_help, 1},
   {"login", no_argument, &simulate_login, 1},
   {"preserve-environment", no_argument, &change_environment, 0},
   {"shell", required_argument, 0, 's'},
+  {"version", no_argument, &show_version, 1},
   {0, 0, 0, 0}
 };
 
@@ -196,26 +207,42 @@ main (argc, argv)
        {
        case 0:
          break;
+
        case 'c':
          command = optarg;
          break;
+
        case 'f':
          fast_startup = 1;
          break;
+
        case 'l':
          simulate_login = 1;
          break;
+
        case 'm':
        case 'p':
          change_environment = 0;
          break;
+
        case 's':
          shell = optarg;
          break;
+
        default:
          usage ();
        }
     }
+
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (optind < argc && !strcmp (argv[optind], "-"))
     {
       simulate_login = 1;
index 941507f..b107ee0 100644 (file)
--- a/src/tee.c
+++ b/src/tee.c
@@ -21,7 +21,9 @@
 #include <sys/types.h>
 #include <signal.h>
 #include <getopt.h>
+
 #include "system.h"
+#include "version.h"
 
 char *xmalloc ();
 void error ();
@@ -38,13 +40,31 @@ static int ignore_interrupts;
 /* The name that this program was run with. */
 char *program_name;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 static struct option const long_options[] =
 {
   {"append", no_argument, NULL, 'a'},
+  {"help", no_argument, &show_help, 1},
   {"ignore-interrupts", no_argument, NULL, 'i'},
+  {"version", no_argument, &show_version, 1},
   {NULL, 0, NULL, 0}
 };
 
+static void
+usage ()
+{
+         fprintf (stderr, "\
+Usage: %s [{--help,--version}] [-ai] [--append]\n\
+       [--ignore-interrupts] [file...]\n",
+                  program_name);
+  exit (1);
+}
+
 void
 main (argc, argv)
      int argc;
@@ -62,20 +82,31 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'a':
          append = 1;
          break;
+
        case 'i':
          ignore_interrupts = 1;
          break;
+
        default:
-         fprintf (stderr, "\
-Usage: %s [-ai] [--append] [--ignore-interrupts] [file...]\n",
-                  program_name);
-         exit (1);
+         usage ();
        }
     }
 
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (ignore_interrupts)
     {
 #ifdef _POSIX_VERSION
index b2cb92d..d905c63 100644 (file)
@@ -993,6 +993,18 @@ posixtest ()
   return (value);
 }
 
+#if defined (TEST_STANDALONE)
+void parse_long_options ();
+
+static void
+usage ()
+{
+  fprintf (stderr, "Usage: %s [{--help,--version}] [expr]\n",
+          program_name);
+  exit (1);
+}
+#endif /* TEST_STANDALONE */
+
 /*
  * [:
  *     '[' expr ']'
@@ -1026,6 +1038,8 @@ test_command (margc, margv)
 
   if (margv[0] && strcmp (margv[0], "[") == 0)
     {
+      parse_long_options (margc, margv, usage);
+
       --margc;
 
       if (margc < 2)
@@ -1041,6 +1055,7 @@ test_command (margc, margv)
   if (pos >= argc)
     test_exit (SHELL_BOOLEAN (FALSE));
 
+  parse_long_options (argc, argv, usage);
   value = posixtest ();
 
   if (pos != argc)
index 79e1759..1839ae1 100644 (file)
--- a/src/tty.c
+++ b/src/tty.c
 
 /* Displays "not a tty" if stdin is not a terminal.
    Displays nothing if -s option is given.
-   Exit status 0 if stdin is a tty, 1 if not, 2 if usage error.
+   Exit status 0 if stdin is a tty, 1 if not, 2 if usage error,
+   3 if write error.
 
- Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
  Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
 
 #include <stdio.h>
 #include <getopt.h>
 #include <sys/types.h>
+
 #include "system.h"
+#include "version.h"
+
+void error ();
 
 static void usage ();
 
@@ -34,10 +39,18 @@ char *program_name;
 /* If nonzero, return an exit status but produce no output. */
 static int silent;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 static struct option const longopts[] =
 {
+  {"help", no_argument, &show_help, 1},
   {"silent", no_argument, NULL, 's'},
   {"quiet", no_argument, NULL, 's'},
+  {"version", no_argument, &show_version, 1},
   {NULL, 0, NULL, 0}
 };
 
@@ -56,14 +69,27 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 's':
          silent = 1;
          break;
+
        default:
          usage ();
        }
     }
 
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (optind != argc)
     usage ();
 
@@ -74,9 +100,12 @@ main (argc, argv)
        puts (tty);
       else
        puts ("not a tty");
+
+      if (ferror (stdout) || fclose (stdout) == EOF)
+       error (3, errno, "standard output");
     }
 
-  exit (tty == NULL);
+  exit (isatty (0) ? 0 : 1);
 }
 
 static void
index c247aec..366e1e3 100644 (file)
@@ -32,7 +32,9 @@
 #include <sys/types.h>
 #include <sys/utsname.h>
 #include <getopt.h>
+
 #include "system.h"
+#include "version.h"
 
 void error ();
 
@@ -61,13 +63,20 @@ static unsigned char toprint;
 /* The name this program was run with, for error messages. */
 char *program_name;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 static struct option const long_options[] =
 {
-  {"sysname", no_argument, NULL, 's'},
+  {"help", no_argument, &show_help, 1},
+  {"machine", no_argument, NULL, 'm'},
   {"nodename", no_argument, NULL, 'n'},
   {"release", no_argument, NULL, 'r'},
-  {"version", no_argument, NULL, 'v'},
-  {"machine", no_argument, NULL, 'm'},
+  {"sysname", no_argument, NULL, 's'},
+  {"version", no_argument, &show_version, 1},
   {"all", no_argument, NULL, 'a'},
   {NULL, 0, NULL, 0}
 };
@@ -88,30 +97,48 @@ main (argc, argv)
     {
       switch (c)
        {
+       case 0:
+         break;
+
        case 's':
          toprint |= PRINT_SYSNAME;
          break;
+
        case 'n':
          toprint |= PRINT_NODENAME;
          break;
+
        case 'r':
          toprint |= PRINT_RELEASE;
          break;
+
        case 'v':
          toprint |= PRINT_VERSION;
          break;
+
        case 'm':
          toprint |= PRINT_MACHINE;
          break;
+
        case 'a':
          toprint = PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
            PRINT_VERSION | PRINT_MACHINE;
          break;
+
        default:
          usage ();
        }
     }
 
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (optind != argc)
     usage ();
 
index 1edec4e..15305a3 100644 (file)
--- a/src/who.c
+++ b/src/who.c
@@ -40,7 +40,9 @@
 #ifndef _POSIX_SOURCE
 #include <sys/param.h>
 #endif
+
 #include "system.h"
+#include "version.h"
 
 #ifndef UTMP_FILE
 #ifdef _PATH_UTMP              /* 4.4BSD.  */
@@ -75,6 +77,12 @@ static void who_am_i ();
 /* The name this program was run with. */
 char *program_name;
 
+/* If non-zero, display usage information and exit.  */
+static int show_help;
+
+/* If non-zero, print the version on standard output and exit.  */
+static int show_version;
+
 /* If nonzero, display only a list of usernames and count of
    the users logged on.
    Ignored for `who am i'. */
@@ -95,10 +103,12 @@ static int include_mesg;
 static struct option const longopts[] =
 {
   {"count", no_argument, NULL, 'q'},
+  {"help", no_argument, &show_help, 1},
   {"idle", no_argument, NULL, 'u'},
   {"heading", no_argument, NULL, 'H'},
   {"message", no_argument, NULL, 'T'},
   {"mesg", no_argument, NULL, 'T'},
+  {"version", no_argument, &show_version, 1},
   {"writable", no_argument, NULL, 'T'},
   {NULL, 0, NULL, 0}
 };
@@ -118,6 +128,9 @@ main (argc, argv)
     {
       switch (optc)
        {
+       case 0:
+         break;
+
        case 'm':
          my_line_only = 1;
          break;
@@ -148,6 +161,15 @@ main (argc, argv)
        }
     }
 
+  if (show_version)
+    {
+      printf ("%s\n", version_string);
+      exit (0);
+    }
+
+  if (show_help)
+    usage ();
+
   if (chdir ("/dev"))
     error (1, errno, "cannot change directory to /dev");