[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: "rm" bug
From: |
Paul Eggert |
Subject: |
Re: "rm" bug |
Date: |
Mon, 29 Aug 2005 14:17:10 -0700 |
User-agent: |
Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) |
address@hidden writes:
> I cannot remove files which beggins with "-"
> for example:
> -TRY_TO_REMOVE_THIS_FILE
> is this bug, or I don't know how to use "rm"?
The latter, but this comes up so often (I must have seen this reported
a hundred times...) that I thought I'd try to improve the diagnostic.
I installed this:
2005-08-29 Paul Eggert <address@hidden>
* NEWS: "rm -FOO" now suggests "rm ./-FOO" if the file "-FOO"
exists and "-FOO" is not a valid option.
* src/rm.c: Include lstat.h, quotearg.h.
(diagnose_leading_hyphen): New function.
(main): Use it.
Index: NEWS
===================================================================
RCS file: /fetish/cu/NEWS,v
retrieving revision 1.304
diff -p -u -r1.304 NEWS
--- NEWS 23 Aug 2005 16:39:01 -0000 1.304
+++ NEWS 29 Aug 2005 21:11:54 -0000
@@ -191,6 +191,9 @@ GNU coreutils NEWS
If stdin is a terminal, nohup now redirects it from /dev/null to
prevent the command from tying up an OpenSSH session after you logout.
+ "rm -FOO" now suggests "rm ./-FOO" if the file "-FOO" exists and
+ "-FOO" is not a valid option.
+
stat -f -c %S outputs the fundamental block size (used for block counts).
stat -f's default output format has been changed to output this size as well.
stat -f recognizes file systems of type XFS and JFS
Index: src/rm.c
===================================================================
RCS file: /fetish/cu/src/rm.c,v
retrieving revision 1.134
diff -p -u -r1.134 rm.c
--- src/rm.c 14 May 2005 07:58:37 -0000 1.134
+++ src/rm.c 29 Aug 2005 21:11:54 -0000
@@ -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);
}
}