From 6334db91d35e8984fc353f78f6b4a2af0553485a Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 29 Aug 2005 21:13:32 +0000 Subject: Include lstat.h, quotearg.h. (diagnose_leading_hyphen): New function. (main): Use it. --- src/rm.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src') 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); } } -- cgit v1.2.3-54-g00ecf