]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Include lstat.h, quotearg.h.
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 29 Aug 2005 21:13:32 +0000 (21:13 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 29 Aug 2005 21:13:32 +0000 (21:13 +0000)
(diagnose_leading_hyphen): New function.
(main): Use it.

src/rm.c

index 56c866e62a854014a15607133ae004742e8aea66..f83831d175a823f7241a7e1b2f4f203603b56195 100644 (file)
--- a/src/rm.c
+++ b/src/rm.c
@@ -51,7 +51,9 @@
 #include "system.h"
 #include "dirname.h"
 #include "error.h"
+#include "lstat.h"
 #include "quote.h"
+#include "quotearg.h"
 #include "remove.h"
 #include "root-dev-ino.h"
 
@@ -95,6 +97,33 @@ static struct option const long_opts[] =
   {NULL, 0, NULL, 0}
 };
 
+/* Advise the user about invalid usages like "rm -foo" if the file
+   "-foo" exists, assuming ARGC and ARGV are as with `main'.  */
+
+static void
+diagnose_leading_hyphen (int argc, char **argv)
+{
+  /* OPTIND is unreliable, so iterate through the arguments looking
+     for a file name that looks like an option.  */
+  int i;
+
+  for (i = 1; i < argc; i++)
+    {
+      char const *arg = argv[i];
+      struct stat st;
+
+      if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
+       {
+         fprintf (stderr,
+                  _("Try `%s ./%s' to remove the file %s.\n"),
+                  argv[0],
+                  quotearg_n_style (1, shell_quoting_style, arg),
+                  quote (arg));
+         break;
+       }
+    }
+}
+
 void
 usage (int status)
 {
@@ -222,6 +251,7 @@ main (int argc, char **argv)
        case_GETOPT_HELP_CHAR;
        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
        default:
+         diagnose_leading_hyphen (argc, argv);
          usage (EXIT_FAILURE);
        }
     }