summaryrefslogtreecommitdiffstats
path: root/eval.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-03-12 16:07:34 -0500
committerTavian Barnes <tavianator@tavianator.com>2016-03-12 16:07:34 -0500
commitf1401f09c59363170f021c4a1676735be2e3cc44 (patch)
tree2b534caed6c1e1dbcca06814f20d603e641f7bdc /eval.c
parent037361965a14e1899020bf16c9112936e39020e8 (diff)
downloadbfs-f1401f09c59363170f021c4a1676735be2e3cc44.tar.xz
Implement -size.
Diffstat (limited to 'eval.c')
-rw-r--r--eval.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/eval.c b/eval.c
index aae82cd..05499db 100644
--- a/eval.c
+++ b/eval.c
@@ -75,7 +75,7 @@ static time_t timespec_diff(const struct timespec *lhs, const struct timespec *r
/**
* Perform a comparison.
*/
-static bool do_cmp(const struct expr *expr, int n) {
+static bool do_cmp(const struct expr *expr, long long n) {
switch (expr->cmp) {
case CMP_EXACT:
return n == expr->idata;
@@ -449,6 +449,29 @@ bool eval_samefile(const struct expr *expr, struct eval_state *state) {
}
/**
+ * -size test.
+ */
+bool eval_size(const struct expr *expr, struct eval_state *state) {
+ const struct stat *statbuf = fill_statbuf(state);
+ if (!statbuf) {
+ return false;
+ }
+
+ static off_t scales[] = {
+ [SIZE_BLOCKS] = 512,
+ [SIZE_BYTES] = 1,
+ [SIZE_WORDS] = 2,
+ [SIZE_KB] = 1024,
+ [SIZE_MB] = 1024*1024,
+ [SIZE_GB] = 1024*1024*1024,
+ };
+
+ off_t scale = scales[expr->sizeunit];
+ off_t size = (statbuf->st_size + scale - 1)/scale; // Round up
+ return do_cmp(expr, size);
+}
+
+/**
* -type test.
*/
bool eval_type(const struct expr *expr, struct eval_state *state) {