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
This commit is contained in:
peter 2001-01-23 11:16:50 +00:00
parent 271766943f
commit 5248b99d8f
5 changed files with 49 additions and 2 deletions

View File

@ -54,6 +54,7 @@ PLAN *c_cmin __P((char *));
PLAN *c_ctime __P((char *));
PLAN *c_delete __P((void));
PLAN *c_depth __P((void));
PLAN *c_empty __P((void));
PLAN *c_exec __P((char ***, int));
PLAN *c_flags __P((char *));
PLAN *c_execdir __P((char ***));

View File

@ -181,6 +181,8 @@ recurses down the tree.
It will not attempt to delete a filename with a ``/''
character in its pathname relative to "." for security reasons.
Depth\-first traversal processing is implied by this option.
.It Ic -empty
True if the current file or directory is empty.
.It Ic -exec Ar utility Op argument ... ;
True if the program named
.Ar utility

View File

@ -41,9 +41,9 @@
enum ntype {
N_AND = 1, /* must start > 0 */
N_AMIN, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CTIME, N_DEPTH,
N_EXEC, N_EXECDIR, N_EXPR, N_FLAGS,
N_EMPTY, N_EXEC, N_EXECDIR, N_EXPR, N_FLAGS,
N_FOLLOW, N_FSTYPE, N_GROUP, N_INUM, N_LINKS, N_LS, N_MMIN,
N_MTIME, N_NAME,
N_MTIME, N_NAME,
N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK, N_OPENPAREN, N_OR, N_PATH,
N_PERM, N_PRINT, N_PRUNE, N_SIZE, N_TYPE, N_USER, N_XDEV,
N_PRINT0, N_DELETE, N_MAXDEPTH, N_MINDEPTH

View File

@ -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>
@ -384,6 +385,48 @@ c_exec(argvp, isok)
return (new);
}
/*
* -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 --
*

View File

@ -68,6 +68,7 @@ static OPTION const options[] = {
{ "-ctime", N_CTIME, c_ctime, O_ARGV },
{ "-delete", N_DELETE, c_delete, O_ZERO },
{ "-depth", N_DEPTH, c_depth, O_ZERO },
{ "-empty", N_EMPTY, c_empty, O_ZERO },
{ "-exec", N_EXEC, c_exec, O_ARGVP },
{ "-execdir", N_EXECDIR, c_execdir, O_ARGVP },
{ "-flags", N_FLAGS, c_flags, O_ARGV },