From c85f569daf3e0e99c9e8941d0234711afdb58cd7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 16 Mar 2017 23:01:00 -0400 Subject: Give struct expr a CFILE* instead of just a FILE* This unifies the behaviour of -print and -fprint /dev/stdout. --- eval.c | 50 ++++++++++++++------------------------------------ 1 file changed, 14 insertions(+), 36 deletions(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index 3cf8965..1a8bc02 100644 --- a/eval.c +++ b/eval.c @@ -11,6 +11,7 @@ #include "bfs.h" #include "bftw.h" +#include "color.h" #include "dstring.h" #include "util.h" #include @@ -687,7 +688,8 @@ bool eval_perm(const struct expr *expr, struct eval_state *state) { * -f?ls action. */ bool eval_fls(const struct expr *expr, struct eval_state *state) { - FILE *file = expr->file; + CFILE *cfile = expr->cfile; + FILE *file = cfile->file; const struct BFTW *ftwbuf = state->ftwbuf; const struct stat *statbuf = fill_statbuf(state); if (!statbuf) { @@ -753,14 +755,8 @@ bool eval_fls(const struct expr *expr, struct eval_state *state) { goto error; } - if (file == stdout) { - if (cfprintf(state->cmdline->cout, " %P", ftwbuf) < 0) { - goto error; - } - } else { - if (fprintf(file, " %s", ftwbuf->path) < 0) { - goto error; - } + if (cfprintf(cfile, " %P", ftwbuf) < 0) { + goto error; } if (ftwbuf->typeflag == BFTW_LNK) { @@ -788,20 +784,18 @@ error: } /** - * -fprint action. + * -f?print action. */ bool eval_fprint(const struct expr *expr, struct eval_state *state) { - const char *path = state->ftwbuf->path; - if (fputs(path, expr->file) == EOF) { - goto error; + CFILE *cfile = expr->cfile; + if (cfile->colors) { + fill_statbuf(state); } - if (fputc('\n', expr->file) == EOF) { - goto error; + + if (cfprintf(cfile, "%P\n", state->ftwbuf) < 0) { + eval_error(state); } - return true; -error: - eval_error(state); return true; } @@ -811,7 +805,7 @@ error: 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) { + if (fwrite(path, 1, length, expr->cfile->file) != length) { eval_error(state); } return true; @@ -827,7 +821,7 @@ bool eval_fprintf(const struct expr *expr, struct eval_state *state) { } } - if (bfs_printf(expr->file, expr->printf, state->ftwbuf) != 0) { + if (bfs_printf(expr->cfile->file, expr->printf, state->ftwbuf) != 0) { eval_error(state); } @@ -835,22 +829,6 @@ done: return true; } -/** - * -print action. - */ -bool eval_print(const struct expr *expr, struct eval_state *state) { - CFILE *cout = state->cmdline->cout; - if (cout->colors) { - fill_statbuf(state); - } - - if (cfprintf(cout, "%P\n", state->ftwbuf) < 0) { - eval_error(state); - } - - return true; -} - /** * -prune action. */ -- cgit v1.2.3