diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2017-11-13 18:04:46 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2017-11-13 18:04:46 -0500 |
commit | 2a45ad01e211d0b36056c21d5211be46195b273d (patch) | |
tree | 087d19624803cdd8299ccb4184afceb769caba99 | |
parent | 05aa323fabcd5353b90075335736e14d9144cedb (diff) | |
download | bfs-2a45ad01e211d0b36056c21d5211be46195b273d.tar.xz |
color: Implement %m for cfprintf()
-rw-r--r-- | color.c | 7 | ||||
-rw-r--r-- | color.h | 1 | ||||
-rw-r--r-- | eval.c | 8 | ||||
-rw-r--r-- | parse.c | 12 | ||||
-rw-r--r-- | printf.c | 2 |
5 files changed, 19 insertions, 11 deletions
@@ -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; @@ -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' @@ -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; @@ -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; } } @@ -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; } } |