]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
env: adjust diagnostics provided for shebang usage
authorPádraig Brady <P@draigBrady.com>
Wed, 27 Jun 2018 06:52:38 +0000 (23:52 -0700)
committerPádraig Brady <P@draigBrady.com>
Wed, 27 Jun 2018 09:38:59 +0000 (02:38 -0700)
* src/env.c (main): Don't process '-' specially since
that causes an issue on the openbsd getopt implementation
where a lone '-' is now processed as an option, and anyway
it doesn't particuarly help diagnosing common shebang
usage issues.  Also don't restrict the extra diagnostics
for shebang usage to the case with 3 arguments, as
further arguments can be passed to a script.
* tests/misc/env-S.pl: Adjust accordingly.

src/env.c
tests/misc/env-S.pl

index 97b6d6bfdd9ae932b57b58f6ddd5519b55335aaa..1915e79aac390655ed4f0cdb3758ebc4c863e000 100644 (file)
--- a/src/env.c
+++ b/src/env.c
@@ -48,7 +48,7 @@ static bool dev_debug;
 static char *varname;
 static size_t vnlen;
 
-static char const shortopts[] = "+C:iS:u:v0 \t-";
+static char const shortopts[] = "+C:iS:u:v0 \t";
 
 static struct option const longopts[] =
 {
@@ -566,14 +566,12 @@ main (int argc, char **argv)
           break;
         case ' ':
         case '\t':
-        case '-':
           /* Space,tab,dash are undocumented options. Attempt to detect
              incorrect shebang usage with extraneous space, e.g.:
                 #!/usr/bin/env -i command
              In which case argv[1] == "-i command".  */
           error (0, 0, _("invalid option -- '%c'"), optc);
-          if (argc == 3)
-            error (0, 0, _("use -[v]S to pass options in shebang lines"));
+          error (0, 0, _("use -[v]S to pass options in shebang lines"));
           usage (EXIT_CANCELED);
 
         case_GETOPT_HELP_CHAR;
@@ -656,7 +654,7 @@ main (int argc, char **argv)
   int exit_status = errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
   error (0, errno, "%s", quote (argv[optind]));
 
-  if (argc == 3 && exit_status == EXIT_ENOENT && strchr (argv[optind], ' '))
+  if (exit_status == EXIT_ENOENT && strchr (argv[optind], ' '))
     error (0, 0, _("use -[v]S to pass options in shebang lines"));
 
   return exit_status;
index d3bfc46c3b128ef16236b709c0b4e7160c3bbcf9..5c3715b6b4b101aff659aaf0f05586fa424e8262 100755 (executable)
@@ -201,20 +201,18 @@ my @Tests =
       {ERR=>"$prog: only \${VARNAME} expansion is supported, " .
            "error at: \${9B}\n"}],
 
-     # Test incorrect shebang usage (extraneous sapce).
-     # With anything other than 3 args report invalid options (as before).
-     ['err_sp1', q['-v-S cat -n'], {EXIT=>125},
-      {ERR=>"env: invalid option -- '-'\n" .
-           "Try 'env --help' for more information.\n"}],
+     # Test incorrect shebang usage (extraneous whitespace).
      ['err_sp2', q['-v -S cat -n'], {EXIT=>125},
       {ERR=>"env: invalid option -- ' '\n" .
+            "env: use -[v]S to pass options in shebang lines\n" .
            "Try 'env --help' for more information.\n"}],
      ['err_sp3', q['-v -S cat -n'], {EXIT=>125}, # embedded tab after -v
       {ERR=>"env: invalid option -- '\t'\n" .
+            "env: use -[v]S to pass options in shebang lines\n" .
            "Try 'env --help' for more information.\n"}],
 
-     # With exactly 3 args, assume it's incorrect shebang usage,
-     # and report a different message. This typically happens with:
+     # Also diagnose incorrect shebang usage when failing to exec.
+     # This typically happens with:
      #
      #   $ cat xxx
      #   #!env -v -S cat -n
@@ -225,12 +223,11 @@ my @Tests =
      #   argv[0] = env
      #   argv[1] = '-v -S cat -n'
      #   argv[2] = './xxx'
-     ['err_sp5', q['-v -S cat -n' ./xxx], {EXIT=>125},
-      {ERR=>"env: invalid option -- ' '\n" .
-            "env: use -[v]S to pass options in shebang lines\n" .
-            "Try 'env --help' for more information.\n"}],
+     ['err_sp5', q['cat -n' ./xxx], {EXIT=>127},
+      {ERR=>"env: 'cat -n': No such file or directory\n" .
+            "env: use -[v]S to pass options in shebang lines\n"}],
 
-     ['err_sp6', q['cat -n' ./xxx], {EXIT=>127},
+     ['err_sp6', q['cat -n' ./xxx arg], {EXIT=>127},
       {ERR=>"env: 'cat -n': No such file or directory\n" .
             "env: use -[v]S to pass options in shebang lines\n"}],
     );