[BusyBox] Fix for "rm"

Pavel Roskin pavel_roskin at geocities.com
Wed Jun 14 16:42:46 UTC 2000


Hello!

Since we probably a not going to switch to getopt immediately, I have
fixed the command line parser for "rm".

Currenly "rm -- foo" works but "rm foo" doesn't.

I'd like to have everybody doing anything with the command line parsers
look into the patch, check it and make sure that you understand how it
works. I want to stop breakage going on in the CVS repository.

Important point about the patch:

1) argc and argv are changed only simultaniously, in the consequent lines,
without embedding them into any checks. It is important that we can trust
the value in argc.

2) argv and argc are advanced twice - unconditionally for the first
argument (it's normally "rm") and conditionally the second time (i.e. only
if the argument begins with '-' we parse it and hide from the rest of the
program)

3) We don't advance argv and argc if the current argument doesn't begin
with '-' - we break from the loop instead.

4) We don't process the options appearing after the non-option arguments -
we cannot remove them from the argv without extra efforts.

5) We do trust argc. If it's correct and we check it, *argv will not be
NULL.

6) **argv can be an empty string - our parser doesn't need any checking
for zero-length arguments.

7) We check argc after and only after parsing the options because the
options (such as -f) may affect the validity of the command line (i.e. the
number of required arguments). We don't want to count options together
with non-option arguments.

The patch is below. I'll apply it unless I get negative comments.

Regards,
Pavel Roskin

=============================================
diff -u -r1.13 rm.c
--- rm.c	2000/06/06 16:15:23	1.13
+++ rm.c	2000/06/14 15:22:49
@@ -68,14 +68,12 @@
 	int stopIt=FALSE;
 	struct stat statbuf;
 
-	if (argc < 2) {
-		usage(rm_usage);
-	}
+	argc--;
 	argv++;
 
 	/* Parse any options */
-	while (--argc >= 0 && *argv && **argv && stopIt==FALSE) {
-		while (**argv == '-') {
+	while (argc > 0 && stopIt == FALSE) {
+		if (**argv == '-') {
 			while (*++(*argv))
 				switch (**argv) {
 					case 'R':
@@ -91,8 +89,15 @@
 					default:
 						usage(rm_usage);
 				}
+			argc--;
+			argv++;
 		}
-		argv++;
+		else
+			break;
+	}
+
+	if (argc < 1 && forceFlag == FALSE) {
+		usage(rm_usage);
 	}
 
 	while (argc-- > 0) {
=============================================







More information about the busybox mailing list