test, echo, printf: don't accept option abbreviation
authorEric Blake <ebb9@byu.net>
Wed, 14 Jan 2009 04:59:35 +0000 (21:59 -0700)
committerEric Blake <ebb9@byu.net>
Wed, 14 Jan 2009 21:32:13 +0000 (14:32 -0700)
* src/test.c (main): Directly parse accepted options, thus
avoiding abbreviations.
* src/echo.c (main): Likewise.
* src/printf.c (main): Likewise.

src/echo.c
src/printf.c
src/test.c

index c4b7ca9..91cce86 100644 (file)
@@ -124,9 +124,20 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  if (allow_options)
-    parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                       usage, AUTHORS, (char const *) NULL);
+  /* We directly parse options, rather than use parse_long_options, in
+     order to avoid accepting abbreviations.  */
+  if (allow_options && argc == 2)
+    {
+      if (STREQ (argv[1], "--help"))
+       usage (EXIT_SUCCESS);
+
+      if (STREQ (argv[1], "--version"))
+       {
+         version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+                      (char *) NULL);
+         exit (EXIT_SUCCESS);
+       }
+    }
 
   --argc;
   ++argv;
index c509951..63351f0 100644 (file)
@@ -1,5 +1,5 @@
 /* printf - format and print data
-   Copyright (C) 1990-2008 Free Software Foundation, Inc.
+   Copyright (C) 1990-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -645,8 +645,20 @@ main (int argc, char **argv)
 
   posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
 
-  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                     usage, AUTHORS, (char const *) NULL);
+  /* We directly parse options, rather than use parse_long_options, in
+     order to avoid accepting abbreviations.  */
+  if (argc == 2)
+    {
+      if (STREQ (argv[1], "--help"))
+       usage (EXIT_SUCCESS);
+
+      if (STREQ (argv[1], "--version"))
+       {
+         version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+                      (char *) NULL);
+         exit (EXIT_SUCCESS);
+       }
+    }
 
   /* The above handles --help and --version.
      Since there is no other invocation of getopt, handle `--' here.  */
index 14d20fd..c56ab9e 100644 (file)
@@ -2,7 +2,7 @@
 
 /* Modified to run with the GNU shell by bfox. */
 
-/* Copyright (C) 1987-2005, 2007-2008 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2005, 2007-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -819,16 +819,25 @@ main (int margc, char **margv)
   if (LBRACKET)
     {
       /* Recognize --help or --version, but only when invoked in the
-        "[" form, and when the last argument is not "]".  POSIX
-        allows "[ --help" and "[ --version" to have the usual GNU
-        behavior, but it requires "test --help" and "test --version"
-        to exit silently with status 0.  */
-      if (margc < 2 || !STREQ (margv[margc - 1], "]"))
+        "[" form, when the last argument is not "]".  Use direct
+        parsing, rather than parse_long_options, to avoid accepting
+        abbreviations.  POSIX allows "[ --help" and "[ --version" to
+        have the usual GNU behavior, but it requires "test --help"
+        and "test --version" to exit silently with status 0.  */
+      if (margc == 2)
        {
-         parse_long_options (margc, margv, PROGRAM_NAME, PACKAGE_NAME, Version,
-                             usage, AUTHORS, (char const *) NULL);
-         test_syntax_error (_("missing `]'"), NULL);
+         if (STREQ (margv[1], "--help"))
+           usage (EXIT_SUCCESS);
+
+         if (STREQ (margv[1], "--version"))
+           {
+             version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
+                          (char *) NULL);
+             test_exit (EXIT_SUCCESS);
+           }
        }
+      if (margc < 2 || !STREQ (margv[margc - 1], "]"))
+       test_syntax_error (_("missing `]'"), NULL);
 
       --margc;
     }