summaryrefslogtreecommitdiffstats
path: root/eval.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2017-01-14 16:38:08 -0500
committerTavian Barnes <tavianator@tavianator.com>2017-02-05 19:02:25 -0500
commita6f94c132c425bbab543e98fcd19f4ff7519d1b7 (patch)
tree215135837c0335946b70593877ec5411ba8e6c17 /eval.c
parent9f1863d45fe596e258596a4b4cc9a4064bcb11d3 (diff)
downloadbfs-a6f94c132c425bbab543e98fcd19f4ff7519d1b7.tar.xz
Implement -printf/-fprintf
Based on a patch by Fangrui Song <i@maskray.me>. Closes #16.
Diffstat (limited to 'eval.c')
-rw-r--r--eval.c77
1 files changed, 47 insertions, 30 deletions
diff --git a/eval.c b/eval.c
index 35501e0..f24c7c4 100644
--- a/eval.c
+++ b/eval.c
@@ -37,7 +37,7 @@ struct eval_state {
/** The bftw() callback return value. */
enum bftw_action action;
/** The eval_cmdline() return value. */
- int ret;
+ int *ret;
/** A stat() buffer, if necessary. */
struct stat statbuf;
};
@@ -58,7 +58,7 @@ static void eval_error(struct eval_state *state) {
if (!eval_should_ignore(state, errno)) {
pretty_error(state->cmdline->stderr_colors,
"'%s': %s\n", state->ftwbuf->path, strerror(errno));
- state->ret = -1;
+ *state->ret = -1;
}
}
@@ -557,14 +557,6 @@ done:
}
/**
- * -prune action.
- */
-bool eval_prune(const struct expr *expr, struct eval_state *state) {
- state->action = BFTW_SKIP_SUBTREE;
- return true;
-}
-
-/**
* -hidden test.
*/
bool eval_hidden(const struct expr *expr, struct eval_state *state) {
@@ -709,22 +701,6 @@ bool eval_perm(const struct expr *expr, struct eval_state *state) {
}
/**
- * -print action.
- */
-bool eval_print(const struct expr *expr, struct eval_state *state) {
- const struct colors *colors = state->cmdline->stdout_colors;
- if (colors) {
- fill_statbuf(state);
- }
-
- if (pretty_print(colors, state->ftwbuf) != 0) {
- eval_error(state);
- }
-
- return true;
-}
-
-/**
* -fprint action.
*/
bool eval_fprint(const struct expr *expr, struct eval_state *state) {
@@ -745,7 +721,7 @@ error:
/**
* -f?print0 action.
*/
-bool eval_print0(const struct expr *expr, struct eval_state *state) {
+bool eval_fprint0(const struct expr *expr, struct eval_state *state) {
const char *path = state->ftwbuf->path;
size_t length = strlen(path) + 1;
if (fwrite(path, 1, length, expr->file) != length) {
@@ -755,6 +731,48 @@ bool eval_print0(const struct expr *expr, struct eval_state *state) {
}
/**
+ * -f?printf action.
+ */
+bool eval_fprintf(const struct expr *expr, struct eval_state *state) {
+ if (expr->printf->needs_stat) {
+ if (!fill_statbuf(state)) {
+ goto done;
+ }
+ }
+
+ if (bfs_printf(expr->file, expr->printf, state->ftwbuf) != 0) {
+ eval_error(state);
+ }
+
+done:
+ return true;
+}
+
+/**
+ * -print action.
+ */
+bool eval_print(const struct expr *expr, struct eval_state *state) {
+ const struct colors *colors = state->cmdline->stdout_colors;
+ if (colors) {
+ fill_statbuf(state);
+ }
+
+ if (pretty_print(colors, state->ftwbuf) != 0) {
+ eval_error(state);
+ }
+
+ return true;
+}
+
+/**
+ * -prune action.
+ */
+bool eval_prune(const struct expr *expr, struct eval_state *state) {
+ state->action = BFTW_SKIP_SUBTREE;
+ return true;
+}
+
+/**
* -quit action.
*/
bool eval_quit(const struct expr *expr, struct eval_state *state) {
@@ -1054,12 +1072,12 @@ static enum bftw_action cmdline_callback(struct BFTW *ftwbuf, void *ptr) {
.ftwbuf = ftwbuf,
.cmdline = cmdline,
.action = BFTW_CONTINUE,
- .ret = args->ret,
+ .ret = &args->ret,
};
if (ftwbuf->typeflag == BFTW_ERROR) {
if (!eval_should_ignore(&state, ftwbuf->error)) {
- state.ret = -1;
+ args->ret = -1;
pretty_error(cmdline->stderr_colors, "'%s': %s\n", ftwbuf->path, strerror(ftwbuf->error));
}
state.action = BFTW_SKIP_SUBTREE;
@@ -1089,7 +1107,6 @@ done:
debug_stat(&state);
}
- args->ret = state.ret;
return state.action;
}