From 8fad779ae21b1052268af0befa930c75d1dd780b Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 18 Dec 2016 18:35:33 -0500 Subject: Implement BSD find's -depth N --- bfs.h | 1 + eval.c | 7 +++++++ parse.c | 19 ++++++++++++++++++- tests.sh | 32 +++++++++++++++++++++++++++++++- tests/test_0112.out | 6 ++++++ tests/test_0113.out | 3 +++ tests/test_0114.out | 10 ++++++++++ tests/test_0115.out | 6 ++++++ tests/test_0116.out | 3 +++ tests/test_0117.out | 10 ++++++++++ 10 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 tests/test_0112.out create mode 100644 tests/test_0113.out create mode 100644 tests/test_0114.out create mode 100644 tests/test_0115.out create mode 100644 tests/test_0116.out create mode 100644 tests/test_0117.out 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 @@ -473,6 +473,13 @@ out: return ret; } +/** + * -depth N test. + */ +bool eval_depth(const struct expr *expr, struct eval_state *state) { + return do_cmp(expr, state->ftwbuf->depth); +} + /** * -empty test. */ diff --git a/parse.c b/parse.c index 8d865e8..1af73cb 100644 --- a/parse.c +++ b/parse.c @@ -831,6 +831,23 @@ static struct expr *parse_depth(struct parser_state *state, int arg1, int arg2) return parse_nullary_option(state); } +/** + * 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. */ @@ -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 -- cgit v1.2.3