summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-12-18 18:35:33 -0500
committerTavian Barnes <tavianator@tavianator.com>2016-12-18 18:35:33 -0500
commit8fad779ae21b1052268af0befa930c75d1dd780b (patch)
tree59fe107f78d9b53f0e6d3ac59e326105a2d06230
parente319411a3f2f4e82f3519cc1a1f45e7153a5e2af (diff)
downloadbfs-8fad779ae21b1052268af0befa930c75d1dd780b.tar.xz
Implement BSD find's -depth N
-rw-r--r--bfs.h1
-rw-r--r--eval.c7
-rw-r--r--parse.c19
-rwxr-xr-xtests.sh32
-rw-r--r--tests/test_0112.out6
-rw-r--r--tests/test_0113.out3
-rw-r--r--tests/test_0114.out10
-rw-r--r--tests/test_0115.out6
-rw-r--r--tests/test_0116.out3
-rw-r--r--tests/test_0117.out10
10 files changed, 95 insertions, 2 deletions
diff --git a/bfs.h b/bfs.h
index 732a6c5..22fee84 100644
--- a/bfs.h
+++ b/bfs.h
@@ -285,6 +285,7 @@ bool eval_used(const struct expr *expr, struct eval_state *state);
bool eval_gid(const struct expr *expr, struct eval_state *state);
bool eval_uid(const struct expr *expr, struct eval_state *state);
+bool eval_depth(const struct expr *expr, struct eval_state *state);
bool eval_empty(const struct expr *expr, struct eval_state *state);
bool eval_hidden(const struct expr *expr, struct eval_state *state);
bool eval_inum(const struct expr *expr, struct eval_state *state);
diff --git a/eval.c b/eval.c
index 123c37a..b62c1b4 100644
--- a/eval.c
+++ b/eval.c
@@ -474,6 +474,13 @@ out:
}
/**
+ * -depth N test.
+ */
+bool eval_depth(const struct expr *expr, struct eval_state *state) {
+ return do_cmp(expr, state->ftwbuf->depth);
+}
+
+/**
* -empty test.
*/
bool eval_empty(const struct expr *expr, struct eval_state *state) {
diff --git a/parse.c b/parse.c
index 8d865e8..1af73cb 100644
--- a/parse.c
+++ b/parse.c
@@ -832,6 +832,23 @@ static struct expr *parse_depth(struct parser_state *state, int arg1, int arg2)
}
/**
+ * Parse -depth [N].
+ */
+static struct expr *parse_depth_n(struct parser_state *state, int arg1, int arg2) {
+ const char *arg = state->argv[1];
+ if (arg) {
+ while (*arg == '-' || *arg == '+') {
+ ++arg;
+ }
+ if (*arg >= '0' && *arg <= '9') {
+ return parse_test_icmp(state, eval_depth);
+ }
+ }
+
+ return parse_depth(state, arg1, arg2);
+}
+
+/**
* Parse -{min,max}depth N.
*/
static struct expr *parse_depth_limit(struct parser_state *state, int is_min, int arg2) {
@@ -1800,7 +1817,7 @@ static const struct table_entry parse_table[] = {
{"d", false, parse_depth},
{"daystart", false, parse_daystart},
{"delete", false, parse_delete},
- {"depth", false, parse_depth},
+ {"depth", false, parse_depth_n},
{"empty", false, parse_empty},
{"exec", false, parse_exec, 0},
{"execdir", false, parse_exec, EXEC_CHDIR},
diff --git a/tests.sh b/tests.sh
index b1672e6..8848ebe 100755
--- a/tests.sh
+++ b/tests.sh
@@ -705,9 +705,39 @@ function test_0111() {
bfs_diff weirdnames -nohidden
}
+function test_0112() {
+ [ "$BSD" ] || return 0
+ bfs_diff basic -depth 2
+}
+
+function test_0113() {
+ [ "$BSD" ] || return 0
+ bfs_diff basic -depth +2
+}
+
+function test_0114() {
+ [ "$BSD" ] || return 0
+ bfs_diff basic -depth -2
+}
+
+function test_0115() {
+ [ "$BSD" ] || return 0
+ bfs_diff basic -depth -depth 2
+}
+
+function test_0116() {
+ [ "$BSD" ] || return 0
+ bfs_diff basic -depth -depth +2
+}
+
+function test_0117() {
+ [ "$BSD" ] || return 0
+ bfs_diff basic -depth -depth -2
+}
+
result=0
-for i in {1..111}; do
+for i in {1..117}; do
test="test_$(printf '%04d' $i)"
if [ -t 1 ]; then
diff --git a/tests/test_0112.out b/tests/test_0112.out
new file mode 100644
index 0000000..3bfc1d3
--- /dev/null
+++ b/tests/test_0112.out
@@ -0,0 +1,6 @@
+basic/c/d
+basic/e/f
+basic/g/h
+basic/j/foo
+basic/k/foo
+basic/l/foo
diff --git a/tests/test_0113.out b/tests/test_0113.out
new file mode 100644
index 0000000..847995d
--- /dev/null
+++ b/tests/test_0113.out
@@ -0,0 +1,3 @@
+basic/k/foo/bar
+basic/l/foo/bar
+basic/l/foo/bar/baz
diff --git a/tests/test_0114.out b/tests/test_0114.out
new file mode 100644
index 0000000..7575ae4
--- /dev/null
+++ b/tests/test_0114.out
@@ -0,0 +1,10 @@
+basic
+basic/a
+basic/b
+basic/c
+basic/e
+basic/g
+basic/i
+basic/j
+basic/k
+basic/l
diff --git a/tests/test_0115.out b/tests/test_0115.out
new file mode 100644
index 0000000..3bfc1d3
--- /dev/null
+++ b/tests/test_0115.out
@@ -0,0 +1,6 @@
+basic/c/d
+basic/e/f
+basic/g/h
+basic/j/foo
+basic/k/foo
+basic/l/foo
diff --git a/tests/test_0116.out b/tests/test_0116.out
new file mode 100644
index 0000000..847995d
--- /dev/null
+++ b/tests/test_0116.out
@@ -0,0 +1,3 @@
+basic/k/foo/bar
+basic/l/foo/bar
+basic/l/foo/bar/baz
diff --git a/tests/test_0117.out b/tests/test_0117.out
new file mode 100644
index 0000000..7575ae4
--- /dev/null
+++ b/tests/test_0117.out
@@ -0,0 +1,10 @@
+basic
+basic/a
+basic/b
+basic/c
+basic/e
+basic/g
+basic/i
+basic/j
+basic/k
+basic/l