Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: Re: Globbing for Empty Directories?



The only non-trivial part is finding a suitable qualifier.

Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.46
diff -u -r1.46 expn.yo
--- Doc/Zsh/expn.yo	8 May 2003 10:30:49 -0000	1.46
+++ Doc/Zsh/expn.yo	1 Apr 2004 18:27:22 -0000
@@ -1630,6 +1630,9 @@
 item(tt(/))(
 directories
 )
+item(tt(F))(
+`full' (i.e. non-empty) directories
+)
 item(tt(.))(
 plain files
 )
Index: Src/glob.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/glob.c,v
retrieving revision 1.31
diff -u -r1.31 glob.c
--- Src/glob.c	6 Oct 2003 22:42:39 -0000	1.31
+++ Src/glob.c	1 Apr 2004 18:27:28 -0000
@@ -1322,6 +1322,9 @@
 		    func = qualmodeflags;
 		    data = qgetmodespec(&s);
 		    break;
+		case 'F':
+		    func = qualnonemptydir;
+		    break;
 		case 'M':
 		    /* Mark directories with a / */
 		    if ((gf_markdirs = !(sense & 1)))
@@ -2799,3 +2802,24 @@
     }
     return 0;
 }
+
+/**/
+static int
+qualnonemptydir(char *name, struct stat *buf, off_t days, char *str)
+{
+    DIR *dirh = opendir(name);
+    struct dirent *de;
+
+    if (dirh == NULL)
+	return 0;
+
+    while ((de = readdir(dirh))) {
+	if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
+	    closedir(dirh);
+	    return 1;
+	}
+    }
+
+    closedir(dirh);
+    return 0;
+}

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>
Work: pws@xxxxxxx
Web: http://www.pwstephenson.fsnet.co.uk



Messages sorted by: Reverse Date, Date, Thread, Author