[BusyBox] Added -i (interactive flag) to rm applet

Christophe Boyanique totof at raceme.org
Mon Mar 12 16:31:25 UTC 2001


Hello,

here is a patch (diffed from cvs current version) to add -i flag to the rm
applet:

- adds new feature BB_FEATURE_RM_INTERACTIVE in Config.h*
- adds askConfirmation() function in utility.c and busybox.h
- adds proper define in usage.h
- prevents removing in fileAction and dirAction (in rm.c) if required (-i
and user doesn't answer y|Y)

Size increasing of BusyBox: (Debian 2.2, i386)

- with trivial help:    320 bytes
- without trivial help: 384 bytes

I'll try later to add the same flag to cp_mv but that should be (way) more
complicated.

Christophe.
-------------- next part --------------
diff -ur busybox.ref/Config.h busybox/Config.h
--- busybox.ref/Config.h	Thu Mar  8 23:57:00 2001
+++ busybox/Config.h	Mon Mar 12 17:10:28 2001
@@ -219,6 +219,9 @@
 // (i.e. in case of an unreachable NFS system).
 #define BB_FEATURE_MOUNT_FORCE
 //
+// use -i (interactive) flag for rm
+//#define BB_FEATURE_RM_INTERACTIVE
+//
 // Enable support for creation of tar files.
 #define BB_FEATURE_TAR_CREATE
 //
diff -ur busybox.ref/Config.h.Hurd busybox/Config.h.Hurd
--- busybox.ref/Config.h.Hurd	Fri Nov  3 20:47:00 2000
+++ busybox/Config.h.Hurd	Mon Mar 12 17:11:02 2001
@@ -212,6 +212,9 @@
 // (i.e. in case of an unreachable NFS system).
 #define BB_FEATURE_MOUNT_FORCE
 //
+// use -i (interactive) flag for rm
+//#define BB_FEATURE_RM_INTERACTIVE
+//
 // Enable support for creation of tar files.
 #define BB_FEATURE_TAR_CREATE
 //
diff -ur busybox.ref/busybox.h busybox/busybox.h
--- busybox.ref/busybox.h	Fri Mar  9 00:59:45 2001
+++ busybox/busybox.h	Mon Mar 12 17:08:59 2001
@@ -250,4 +250,8 @@
 #define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
 #endif
 
+#if defined(BB_FEATURE_RM_INTERACTIVE) && defined(BB_RM)
+int askConfirmation(void);
+#endif
+
 #endif /* _BB_INTERNAL_H_ */
diff -ur busybox.ref/docs/busybox.sgml busybox/docs/busybox.sgml
--- busybox.ref/docs/busybox.sgml	Wed Feb  7 05:09:23 2001
+++ busybox/docs/busybox.sgml	Mon Mar 12 16:52:10 2001
@@ -2728,6 +2728,7 @@
 
 		<para>
 		<screen>
+			-i		Always prompt before removing each destinations
 			-f		Remove existing destinations, never prompt
 			-r or -R	Remove the contents of directories recursively
 		</screen>
diff -ur busybox.ref/rm.c busybox/rm.c
--- busybox.ref/rm.c	Tue Feb 20 07:14:08 2001
+++ busybox/rm.c	Mon Mar 12 16:57:48 2001
@@ -33,11 +33,21 @@
 
 static int recursiveFlag = FALSE;
 static int forceFlag = FALSE;
+#ifdef BB_FEATURE_RM_INTERACTIVE
+	static int interactiveFlag = FALSE;
+#endif
 static const char *srcName;
 
 
 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
 {
+#ifdef BB_FEATURE_RM_INTERACTIVE
+	if (interactiveFlag == TRUE) {
+		printf("rm: remove `%s'? ", fileName);
+		if (askConfirmation() == 0)
+			return (TRUE);
+	}
+#endif
 	if (unlink(fileName) < 0) {
 		perror_msg("%s", fileName);
 		return (FALSE);
@@ -52,6 +62,13 @@
 		perror_msg("%s", fileName);
 		return (FALSE);
 	} 
+#ifdef BB_FEATURE_RM_INTERACTIVE
+	if (interactiveFlag == TRUE) {
+		printf("rm: remove directory `%s'? ", fileName);
+		if (askConfirmation() == 0)
+			return (TRUE);
+	}
+#endif
 	if (rmdir(fileName) < 0) {
 		perror_msg("%s", fileName);
 		return (FALSE);
@@ -79,6 +96,14 @@
 						break;
 					case 'f':
 						forceFlag = TRUE;
+#ifdef BB_FEATURE_RM_INTERACTIVE
+						interactiveFlag = FALSE;
+#endif
+						break;
+					case 'i':
+#ifdef BB_FEATURE_RM_INTERACTIVE
+						interactiveFlag = TRUE;
+#endif
 						break;
 					case '-':
 						stopIt = TRUE;
diff -ur busybox.ref/usage.h busybox/usage.h
--- busybox.ref/usage.h	Mon Mar 12 10:58:51 2001
+++ busybox/usage.h	Mon Mar 12 16:49:06 2001
@@ -808,12 +808,18 @@
 #define reset_full_usage \
 	"Resets the screen."
 
+#ifdef BB_FEATURE_RM_INTERACTIVE
+  #define USAGE_RM_INTERACTIVE(a) a
+#else
+  #define USAGE_RM_INTERACTIVE(a)
+#endif
 #define rm_trivial_usage \
 	"[OPTION]... FILE..."
 #define rm_full_usage \
 	"Remove (unlink) the FILE(s).  You may use '--' to\n" \
 	"indicate that all following arguments are non-options.\n\n" \
 	"Options:\n" \
+	USAGE_RM_INTERACTIVE("\t-i\t\talways prompt before removing each destinations\n") \
 	"\t-f\t\tremove existing destinations, never prompt\n" \
 	"\t-r or -R\tremove the contents of directories recursively"
 
diff -ur busybox.ref/utility.c busybox/utility.c
--- busybox.ref/utility.c	Mon Mar 12 11:00:17 2001
+++ busybox/utility.c	Mon Mar 12 16:35:02 2001
@@ -1841,6 +1841,24 @@
 }
 #endif
 
+#ifdef BB_FEATURE_RM_INTERACTIVE
+	#if defined (BB_CP_MV) || defined (BB_RM)
+int askConfirmation()
+{
+	int c = '\0';
+	int ret = 0;
+
+	while (c != '\n') {
+		c = getchar();
+		if ( c != '\n' ) {
+			ret = ((c=='y')||(c=='Y')) ? 1 : 0;
+		}
+	}
+	return ret;
+}
+	#endif
+#endif
+
 /* END CODE */
 /*
 Local Variables:


More information about the busybox mailing list