Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham
	autolearn_force=no version=3.4.1
X-AuditID: cbfec7f4-f79026d00000418a-30-56bc757823bc
Date: Thu, 11 Feb 2016 11:50:13 +0000
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh Hackers' List <zsh-workers@zsh.org>
Subject: Re: PATCH: rm * with count
Message-id: <20160211115013.05e0ce7f@pwslap01u.europe.root.pri>
In-reply-to: <160210132048.ZM3836@torch.brasslantern.com>
References: <20160210164150.5bef82b2@pwslap01u.europe.root.pri>
 <160210132048.ZM3836@torch.brasslantern.com>
Organization: Samsung Cambridge Solution Centre
X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu)
MIME-version: 1.0
Content-type: text/plain; charset=US-ASCII
Content-transfer-encoding: 7bit
X-Brightmail-Tracker:
 H4sIAAAAAAAAA+NgFjrLLMWRmVeSWpSXmKPExsVy+t/xq7oVpXvCDHrXqlkcbH7I5MDoserg
	B6YAxigum5TUnMyy1CJ9uwSujPcrRAtWCVecfDWDpYHxFF8XIyeHhICJxIqD8xghbDGJC/fW
	s3UxcnEICSxllFg48R0rSEJIYAaTxJ5rQhD2OUaJWUdVIIrOMkrM23aaDSTBIqAqcWDtLLBJ
	bAKGElM3zQayOThEBLQl2j+KgYSFBZQlTu09DlbCK2AvMefhP2YQm1PAUmLu8Y3MEPPzJJZN
	PMIEYvML6Etc/fuJCeI4e4mZV85A9QpK/Jh8jwXEZhbQkti8rYkVwpaX2LzmLdQcdYkbd3ez
	T2AUnoWkZRaSlllIWhYwMq9iFE0tTS4oTkrPNdQrTswtLs1L10vOz93ECAnkLzsYFx+zOsQo
	wMGoxMMbUL87TIg1say4MvcQowQHs5IIr2HOnjAh3pTEyqrUovz4otKc1OJDjNIcLErivHN3
	vQ8REkhPLEnNTk0tSC2CyTJxcEo1MLZ8Weerl7VBlPGROWcwg7TE5tTN7W+j4zmVw4/2asWl
	v70d/G6G4pvv/HeZDh0ufS4wwWnOo2XzTXeHPT+/IjqDfZnS7kNH+/kmsqvkaP7/pvtpw0TR
	iE5xxrWhUS828No3s79KiBGO/SZY1LyP8dsVAZ+GHik9sx9V03+b6r1McTxyMPPVNiWW4oxE
	Qy3mouJEAMXOBBNgAgAA
X-Seq: zsh-workers 37946

On Wed, 10 Feb 2016 13:20:48 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Feb 10,  4:41pm, Peter Stephenson wrote:
> } Subject: PATCH: rm * with count
> }
> } Faced with a "rm *" prompt, it occurred to me it might be a little bit
> } more helpful to decide if something has gone really horribly wrong if we
> } made the easy tweak to count the number of files in the directory.
> } 
> } Any comments on the following?
> 
> My only comment would be that this can potentially take a long time if
> the number of files in the directory is large.  Perhaps stop after some
> number (100?) and prompt with
> 
>     "sure you want to delete more than %d files in "

It can certainly be slow if it's on a network, although it's going to
have to do the full glob anyway if it actually executes the command.

> Also if the count is zero maybe the prompt should say something about
> "no files to remove" or something.

I didn't feel it was quite the job of this test to say things like "I
can't actually find that directory" or "I've found it but it's empty";
it was starting to get too fiddly and not really adding to safety, which
was the primary purpose.

pws


diff --git a/Src/utils.c b/Src/utils.c
index de4af5a..12911d3 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2634,13 +2634,36 @@ zsleep_random(long max_us, time_t end_time)
 int
 checkrmall(char *s)
 {
+    DIR *rmd;
+    int count = 0;
     if (!shout)
 	return 1;
-    fprintf(shout, "zsh: sure you want to delete all the files in ");
     if (*s != '/') {
-	nicezputs(pwd[1] ? pwd : "", shout);
-	fputc('/', shout);
-    }
+	if (pwd[1])
+	    s = zhtricat(pwd, "/", s);
+	else
+	    s = dyncat("/", s);
+    }
+    const int max_count = 100;
+    if ((rmd = opendir(unmeta(s)))) {
+	int ignoredots = !isset(GLOBDOTS);
+	while (zreaddir(rmd, ignoredots)) {
+	    count++;
+	    if (count > max_count)
+		break;
+	}
+	closedir(rmd);
+    }
+    if (count > max_count)
+	fprintf(shout, "zsh: sure you want to delete more than %d files in ",
+		max_count);
+    else if (count == 1)
+	fprintf(shout, "zsh: sure you want to delete the only file in ");
+    else if (count > 0)
+	fprintf(shout, "zsh: sure you want to delete all %d files in ",
+		count);
+    else
+	fprintf(shout, "zsh: sure you want to delete all the files in ");
     nicezputs(s, shout);
     if(isset(RMSTARWAIT)) {
 	fputs("? (waiting ten seconds)", shout);

