]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
all: use consistent diagnostics for unknown long options
authorBernhard Voelker <mail@bernhard-voelker.de>
Tue, 28 Nov 2017 16:32:29 +0000 (17:32 +0100)
committerBernhard Voelker <mail@bernhard-voelker.de>
Wed, 29 Nov 2017 12:27:02 +0000 (13:27 +0100)
Previously, e.g. cksum failed to output the offending unknown long
option:
  $ cksum --unknown-opt
  cksum: invalid option -- '-'
  Try 'cksum --help' for more information.
i.e., it tried to diagnose '-' as short option.
Instead, it should diagnose the unknown long option:
  $ cksum --unknown-opt
  cksum: unrecognized option '--unknown-opt'
  Try 'cksum --help' for more information.

* src/cksum.c (long_options): Add struct with null entry only.
(main): Use it in the getopt_long call.
* src/dd.c: Likewise.
* src/hostid.c: Likewise.
* src/hostname.c: Likewise.
* src/link.c: Likewise.
* src/logname.c: Likewise.
* src/nohup.c: Likewise.
* src/sleep.c: Likewise.
* src/tsort.c: Likewise.
* src/unlink.c: Likewise.
* src/uptime.c: Likewise.
* src/users.c: Likewise.
* src/whoami.c: Likewise.
* src/yes.c: Likewise.
* NEWS (Improvements): Mention the fix.

15 files changed:
NEWS
src/cksum.c
src/dd.c
src/hostid.c
src/hostname.c
src/link.c
src/logname.c
src/nohup.c
src/sleep.c
src/tsort.c
src/unlink.c
src/uptime.c
src/users.c
src/whoami.c
src/yes.c

diff --git a/NEWS b/NEWS
index d5060d1c8e8e565200a54e9df562cb07eef48503..2a2515b7ef563658c74b61a241617409978e313b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   tail --bytes=NUM will efficiently seek to the end of block devices,
   rather than reading from the start.
 
+  Utilities which do not support long options (other than the default --help
+  and --version), e.g. cksum and sleep, now use more consistent error diagnostic
+  for unknown long options.
+
 ** Build-related
 
   Default man pages are now distributed which are used if perl is
index 0877ce519c22cc1171e60ab60355216647404356..deceb4dd4999aa1b0f834b3eebe95366f3c0c702 100644 (file)
@@ -107,6 +107,11 @@ main (void)
 # include "die.h"
 # include "error.h"
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 /* Number of bytes to read at once.  */
 # define BUFLEN (1 << 16)
 
@@ -291,7 +296,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   have_read_stdin = false;
index 0ff618c963532ec891dccc2099a435d4c8b01d6c..7b3d2fd37748532e33b77a9323663a2458bbb87e 100644 (file)
--- a/src/dd.c
+++ b/src/dd.c
   proper_name ("David MacKenzie"), \
   proper_name ("Stuart Kemp")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
    present.  */
 #ifndef SA_NOCLDSTOP
@@ -2394,7 +2399,7 @@ main (int argc, char **argv)
                       usage, AUTHORS, (char const *) NULL);
   close_stdout_required = false;
 
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   /* Initialize translation table to identity translation. */
index 8766edac3941c2005709263e0044de26f620156a..c676bdcd8a9ddd4bf6f17a1a2971488360dc49c5 100644 (file)
 
 #define AUTHORS proper_name ("Jim Meyering")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -66,7 +71,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (optind < argc)
index 0a57cb409e4323eaa49837f5751c3bf6566fe97c..560eb7f437f41c314840646c2bfc8a69ca70e1c1 100644 (file)
 
 #define AUTHORS proper_name ("Jim Meyering")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 #if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \
      defined HAVE_SYS_SYSTEMINFO_H
 # include <sys/systeminfo.h>
@@ -83,7 +88,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (argc == optind + 1)
index 89711c1fddd452f0d56dc7bf7e52b1a6a7441e29..3b9c776c1d6213ee1972670450ada676b645f981 100644 (file)
 
 #define AUTHORS proper_name ("Michael Stone")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -69,7 +74,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (argc < optind + 2)
index 3529559efc0d94ee475adf73ef0448ed557cdd1a..db5836590f05a69aae27a49f3566702f9ae43c07 100644 (file)
 
 #define AUTHORS proper_name ("FIXME: unknown")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -64,7 +69,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (optind < argc)
index 162e7cd50bc89ac03809a2167ded3b1f08883792..7bf32d24a59131b873adf0555065194de407ac58 100644 (file)
 
 #define AUTHORS proper_name ("Jim Meyering")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 /* Exit statuses.  */
 enum
   {
@@ -101,7 +106,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
     usage (exit_internal_failure);
 
   if (argc <= optind)
index de09ff3582d1064b426afd1e9f13df8ba2bfe325..889719f0182bd7fe455b30ac2e97325b3bba4f67 100644 (file)
   proper_name ("Jim Meyering"), \
   proper_name ("Paul Eggert")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -111,7 +116,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (argc == 1)
index 73a876c384dd63f8d2919a26e1c1e76c678bcd30..cb84393e9c312f8199d72d21c23eb489529efd16 100644 (file)
 
 #define AUTHORS proper_name ("Mark Kettenis")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 /* Token delimiters when reading from a file.  */
 #define DELIM " \t\n"
 
@@ -553,7 +558,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (1 < argc - optind)
index 3d8bb0bfdcdc56fdc654185da95860fe53ac38e0..f173d323ed5be6c972c263bf1c4742cf4c74c7f5 100644 (file)
 
 #define AUTHORS proper_name ("Michael Stone")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -68,7 +73,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (argc < optind + 1)
index cafcda204be8cffb3ee5ba9358cc2b81b99d62c5..d1951d50f894fcce1f6dc4adeb11cb034c3da199 100644 (file)
   proper_name ("David MacKenzie"), \
   proper_name ("Kaveh Ghazi")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 static void
 print_uptime (size_t n, const STRUCT_UTMP *this)
 {
@@ -236,7 +241,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   switch (argc - optind)
index 7b1d8cdba411999624fe26587d57b35d9349f989..4bdebff44afbe26ac4f4f594924b330bdeb07573 100644 (file)
   proper_name ("Joseph Arceneaux"), \
   proper_name ("David MacKenzie")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 static int
 userid_compare (const void *v_a, const void *v_b)
 {
@@ -130,7 +135,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   switch (argc - optind)
index 161674c67cf189d6e9eb64d0e28b3b5fdffd605c..69f3a026202319908de75b2148b3e36107ef3408 100644 (file)
 
 #define AUTHORS proper_name ("Richard Mlynarik")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -72,7 +77,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   if (optind != argc)
index 0439d0e329ba02ce326fa6006932ce3e5fa4431b..8b1caa4c152bb666c1b43d35bf52749c7c6322ec 100644 (file)
--- a/src/yes.c
+++ b/src/yes.c
 
 #define AUTHORS proper_name ("David MacKenzie")
 
+static struct option const long_options[] =
+{
+  {NULL, 0, NULL, 0}
+};
+
 void
 usage (int status)
 {
@@ -69,7 +74,7 @@ main (int argc, char **argv)
 
   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
                       usage, AUTHORS, (char const *) NULL);
-  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
+  if (getopt_long (argc, argv, "+", long_options, NULL) != -1)
     usage (EXIT_FAILURE);
 
   char **operands = argv + optind;