summaryrefslogtreecommitdiffstats
path: root/usr.bin/find/function.c
diff options
context:
space:
mode:
authorpeter <peter@FreeBSD.org>2001-01-23 11:16:50 +0000
committerpeter <peter@FreeBSD.org>2001-01-23 11:16:50 +0000
commit7eafba69d54138a0429c71ddd6080ad718f3527c (patch)
tree9fd9977704c86e786a10c71f41130e4113c603ba /usr.bin/find/function.c
parent3bdade41c9a9713ee00256d16b86b65b74fc8f7e (diff)
downloadFreeBSD-src-7eafba69d54138a0429c71ddd6080ad718f3527c.zip
FreeBSD-src-7eafba69d54138a0429c71ddd6080ad718f3527c.tar.gz
Add the -empty flag, from OpenBSD. It returns true if the directory
is empty. There doesn't appear to be another easy way to do this. mobile# mkdir foo mobile# mkdir foo/bar mobile# mkdir bar mobile# find . -empty ./foo/bar ./bar
Diffstat (limited to 'usr.bin/find/function.c')
-rw-r--r--usr.bin/find/function.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c
index 670d102..18cd83c 100644
--- a/usr.bin/find/function.c
+++ b/usr.bin/find/function.c
@@ -49,6 +49,7 @@ static const char rcsid[] =
#include <sys/wait.h>
#include <sys/mount.h>
+#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fnmatch.h>
@@ -385,6 +386,48 @@ c_exec(argvp, isok)
}
/*
+ * -empty functions --
+ *
+ * True if the file or directory is empty
+ */
+int
+f_empty(plan, entry)
+ PLAN *plan;
+ FTSENT *entry;
+{
+ if (S_ISREG(entry->fts_statp->st_mode) && entry->fts_statp->st_size == 0)
+ return (1);
+ if (S_ISDIR(entry->fts_statp->st_mode)) {
+ struct dirent *dp;
+ int empty;
+ DIR *dir;
+
+ empty = 1;
+ dir = opendir(entry->fts_accpath);
+ if (dir == NULL)
+ err(1, "%s", entry->fts_accpath);
+ for (dp = readdir(dir); dp; dp = readdir(dir))
+ if (dp->d_name[0] != '.' ||
+ (dp->d_name[1] != '\0' &&
+ (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) {
+ empty = 0;
+ break;
+ }
+ closedir(dir);
+ return (empty);
+ }
+ return (0);
+}
+
+PLAN *
+c_empty()
+{
+ ftsoptions &= ~FTS_NOSTAT;
+
+ return (palloc(N_EMPTY, f_empty));
+}
+
+/*
* -execdir utility [arg ... ] ; functions --
*
* True if the executed utility returns a zero value as exit status.
OpenPOWER on IntegriCloud