From 2a45ad01e211d0b36056c21d5211be46195b273d Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 13 Nov 2017 18:04:46 -0500 Subject: color: Implement %m for cfprintf() --- color.c | 7 +++++++ color.h | 1 + eval.c | 8 ++++---- parse.c | 12 ++++++------ printf.c | 2 +- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/color.c b/color.c index 8df6a1f..429df2d 100644 --- a/color.c +++ b/color.c @@ -473,6 +473,7 @@ int cfprintf(CFILE *cfile, const char *format, ...) { FILE *file = cfile->file; int ret = -1; + int error = errno; va_list args; va_start(args, format); @@ -517,6 +518,12 @@ int cfprintf(CFILE *cfile, const char *format, ...) { } break; + case 'm': + if (fputs(strerror(error), file) == EOF) { + goto done; + } + break; + case 'P': if (print_path(cfile, va_arg(args, const struct BFTW *)) != 0) { goto done; diff --git a/color.h b/color.h index d7aecfd..ca8a46c 100644 --- a/color.h +++ b/color.h @@ -100,6 +100,7 @@ int cfclose(CFILE *cfile); * %g: A double * %s: A string * %zu: A size_t + * %m: strerror(errno) * %P: A colored file path, from a const struct BFTW * argument * %L: A colored link target, from a const struct BFTW * argument * %{cc}: Change the color to 'cc' diff --git a/eval.c b/eval.c index be73adc..d13b548 100644 --- a/eval.c +++ b/eval.c @@ -68,7 +68,7 @@ static bool eval_should_ignore(const struct eval_state *state, int error) { */ static void eval_error(struct eval_state *state) { if (!eval_should_ignore(state, errno)) { - cfprintf(state->cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", state->ftwbuf->path, strerror(errno)); + cfprintf(state->cmdline->cerr, "%{er}error: '%s': %m%{rs}\n", state->ftwbuf->path); *state->ret = EXIT_FAILURE; } } @@ -306,7 +306,7 @@ static int eval_exec_finish(const struct expr *expr, const struct cmdline *cmdli int ret = 0; if (expr->execbuf && bfs_exec_finish(expr->execbuf) != 0) { if (errno != 0) { - cfprintf(cmdline->cerr, "%{er}error: %s %s: %s.%{rs}\n", expr->argv[0], expr->argv[1], strerror(errno)); + cfprintf(cmdline->cerr, "%{er}error: %s %s: %m%{rs}\n", expr->argv[0], expr->argv[1]); } ret = -1; } @@ -325,7 +325,7 @@ static int eval_exec_finish(const struct expr *expr, const struct cmdline *cmdli bool eval_exec(const struct expr *expr, struct eval_state *state) { bool ret = bfs_exec(expr->execbuf, state->ftwbuf) == 0; if (errno != 0) { - cfprintf(state->cmdline->cerr, "%{er}error: %s %s: %s.%{rs}\n", expr->argv[0], expr->argv[1], strerror(errno)); + cfprintf(state->cmdline->cerr, "%{er}error: %s %s: %m%{rs}\n", expr->argv[0], expr->argv[1]); *state->ret = EXIT_FAILURE; } return ret; @@ -1100,7 +1100,7 @@ static enum bftw_action cmdline_callback(struct BFTW *ftwbuf, void *ptr) { if (ftwbuf->typeflag == BFTW_ERROR) { if (!eval_should_ignore(&state, ftwbuf->error)) { args->ret = EXIT_FAILURE; - cfprintf(cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", ftwbuf->path, strerror(ftwbuf->error)); + cfprintf(cmdline->cerr, "%{er}error: '%s': %m%{rs}\n", ftwbuf->path); } state.action = BFTW_SKIP_SUBTREE; goto done; diff --git a/parse.c b/parse.c index 1fc4832..e173d10 100644 --- a/parse.c +++ b/parse.c @@ -254,7 +254,7 @@ int free_cmdline(struct cmdline *cmdline) { if (cfclose(ofile->cfile) != 0) { if (cerr) { - cfprintf(cerr, "%{er}error: '%s': %s%{rs}\n", ofile->path, strerror(errno)); + cfprintf(cerr, "%{er}error: '%s': %m%{rs}\n", ofile->path); } ret = -1; } @@ -265,7 +265,7 @@ int free_cmdline(struct cmdline *cmdline) { if (cout && fflush(cout->file) != 0) { if (cerr) { - cfprintf(cerr, "%{er}error: standard output: %s%{rs}\n", strerror(errno)); + cfprintf(cerr, "%{er}error: standard output: %m%{rs}\n"); } ret = -1; } @@ -375,13 +375,13 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char * CFILE *cfile = cfopen(path, state->use_color ? cmdline->colors : NULL); if (!cfile) { - cfprintf(cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", path, strerror(errno)); + cfprintf(cmdline->cerr, "%{er}error: '%s': %m%{rs}\n", path); goto out; } struct stat sb; if (fstat(fileno(cfile->file), &sb) != 0) { - cfprintf(cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", path, strerror(errno)); + cfprintf(cmdline->cerr, "%{er}error: '%s': %m%{rs}\n", path); goto out_close; } @@ -428,7 +428,7 @@ static int stat_arg(const struct parser_state *state, struct expr *expr, struct int ret = xfstatat(AT_FDCWD, expr->sdata, sb, flags); if (ret != 0) { - cfprintf(cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", expr->sdata, strerror(errno)); + cfprintf(cmdline->cerr, "%{er}error: '%s': %m%{rs}\n", expr->sdata); } return ret; } @@ -1269,7 +1269,7 @@ static struct expr *parse_fstype(struct parser_state *state, int arg1, int arg2) if (!cmdline->mtab) { cmdline->mtab = parse_bfs_mtab(); if (!cmdline->mtab) { - cfprintf(cmdline->cerr, "%{er}error: Couldn't parse the mount table: %s.%{rs}\n\n", strerror(errno)); + cfprintf(cmdline->cerr, "%{er}error: Couldn't parse the mount table: %m%{rs}\n"); return NULL; } } diff --git a/printf.c b/printf.c index 9157ccb..2d48cb6 100644 --- a/printf.c +++ b/printf.c @@ -618,7 +618,7 @@ struct bfs_printf *parse_bfs_printf(const char *format, struct cmdline *cmdline) if (!cmdline->mtab) { cmdline->mtab = parse_bfs_mtab(); if (!cmdline->mtab) { - cfprintf(cmdline->cerr, "%{er}error: Couldn't parse the mount table: %s.%{rs}\n\n", strerror(errno)); + cfprintf(cmdline->cerr, "%{er}error: Couldn't parse the mount table: %m%{rs}\n"); goto directive_error; } } -- cgit v1.2.3