This is an non-standard extension.
It tests with a file exists, is a directory, and is empty.
Tests against non-existing files, non-directories and non-empty
directories causes exit status 1.
It is a sane alternative to writing
test -z "$(ls -A "$1")"
Signed-off-by: Mattias Andrée
---
test.1 | 4 +++-
test.c | 29 +
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/test.1 b/test.1
index 539016a..b1d2121 100644
--- a/test.1
+++ b/test.1
@@ -22,13 +22,15 @@ exists and has (any size
.Op Fl e
| non-zero size
.Op Fl s ) .
-.It ( Fl f | Fl d | Fl p | Fl hL | Fl S | Fl b | Fl c ) Ar file
+.It ( Fl f | Fl d | Fl E | Fl p | Fl hL | Fl S | Fl b | Fl c ) Ar file
.Ar file
exists and is a
(regular file
.Op Fl f
| directory
.Op Fl d
+| empty directory
+.Op Fl E
| named pipe
.Op Fl p
| symbolic link
diff --git a/test.c b/test.c
index 3fe12c3..7e27c10 100644
--- a/test.c
+++ b/test.c
@@ -2,6 +2,8 @@
#include
#include
+#include
+#include
#include
#include
@@ -112,6 +114,32 @@ binary_nt(char *s1, char *s2)
return mtimecmp(&buf1, &buf2) > 0;
}
+static int
+unary_E(char *s)
+{
+ struct stat buf;
+DIR *dir;
+struct dirent *file;
+
+ if (stat(s, &buf) || !S_ISDIR(buf.st_mode))
+ return 0;
+
+ if (!(dir = opendir(s)))
+enprintf(2, "opendir %s:", s);
+ while (errno = 0, (file = readdir(dir))) {
+ if (strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
+ closedir(dir);
+ return 0;
+ }
+ }
+if (errno)
+enprintf(2, "readdir %s:", s);
+closedir(dir);
+return 1;
+
+
+}
+
struct test {
char *name;
int (*func)();
@@ -121,6 +149,7 @@ static struct test unary[] = {
{ "-b", unary_b },
{ "-c", unary_c },
{ "-d", unary_d },
+ { "-E", unary_E },
{ "-e", unary_e },
{ "-f", unary_f },
{ "-g", unary_g },
--
2.7.4