summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2005-08-29 21:13:32 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2005-08-29 21:13:32 +0000
commit6334db91d35e8984fc353f78f6b4a2af0553485a (patch)
treef24e291621b51ffbfb02683afa66ef2bcad089a2 /src
parent8cb5f87f68afa3101d04c6dd704e8e1f020b2086 (diff)
downloadcoreutils-6334db91d35e8984fc353f78f6b4a2af0553485a.tar.xz
Include lstat.h, quotearg.h.
(diagnose_leading_hyphen): New function. (main): Use it.
Diffstat (limited to 'src')
-rw-r--r--src/rm.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/rm.c b/src/rm.c
index 56c866e62..f83831d17 100644
--- 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);
}
}