From ffa83efbb604ba2ed921e86bc6c837f6d605faa2 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 20 May 2024 12:54:15 -0400 Subject: Stop using %m --- src/bfstd.c | 4 ++++ src/bfstd.h | 5 +++++ src/color.c | 7 ------- src/color.h | 1 - src/ctx.c | 4 ++-- src/diag.c | 16 +--------------- src/diag.h | 12 ++++-------- src/eval.c | 20 +++++++++----------- src/parse.c | 14 +++++++------- tests/tests.h | 3 ++- 10 files changed, 34 insertions(+), 52 deletions(-) diff --git a/src/bfstd.c b/src/bfstd.c index 7680f17..ce2218c 100644 --- a/src/bfstd.c +++ b/src/bfstd.c @@ -322,6 +322,10 @@ const char *xstrerror(int errnum) { return ret; } +const char *errstr(void) { + return xstrerror(errno); +} + /** Get the single character describing the given file type. */ static char type_char(mode_t mode) { switch (mode & S_IFMT) { diff --git a/src/bfstd.h b/src/bfstd.h index d06bbd9..8055c55 100644 --- a/src/bfstd.h +++ b/src/bfstd.h @@ -247,6 +247,11 @@ char *xstpencpy(char *dest, char *end, const char *src, size_t n); */ const char *xstrerror(int errnum); +/** + * Shorthand for xstrerror(errno). + */ +const char *errstr(void); + /** * Format a mode like ls -l (e.g. -rw-r--r--). * diff --git a/src/color.c b/src/color.c index a0e553f..137d795 100644 --- a/src/color.c +++ b/src/color.c @@ -1197,7 +1197,6 @@ static int print_expr(CFILE *cfile, const struct bfs_expr *expr, bool verbose, i attr(printf(2, 0)) static int cvbuff(CFILE *cfile, const char *format, va_list args) { const struct colors *colors = cfile->colors; - int error = errno; // Color specifier (e.g. ${blu}) state struct esc_seq **esc; @@ -1255,12 +1254,6 @@ static int cvbuff(CFILE *cfile, const char *format, va_list args) { } break; - case 'm': - if (dstrcat(&cfile->buffer, xstrerror(error)) != 0) { - return -1; - } - break; - case 'p': switch (*++i) { case 'q': diff --git a/src/color.h b/src/color.h index 3550888..8582eb3 100644 --- a/src/color.h +++ b/src/color.h @@ -87,7 +87,6 @@ int cfclose(CFILE *cfile); * %g: A double * %s: A string * %zu: A size_t - * %m: strerror(errno) * %pq: A shell-escaped string, like bash's printf %q * %pQ: A TTY-escaped string. * %pF: A colored file name, from a const struct BFTW * argument diff --git a/src/ctx.c b/src/ctx.c index 11531df..71fef98 100644 --- a/src/ctx.c +++ b/src/ctx.c @@ -178,9 +178,9 @@ void bfs_ctx_flush(const struct bfs_ctx *ctx) { const char *path = ctx_file->path; if (path) { - bfs_error(ctx, "%pq: %m.\n", path); + bfs_error(ctx, "%pq: %s.\n", path, errstr()); } else if (cfile == ctx->cout) { - bfs_error(ctx, "(standard output): %m.\n"); + bfs_error(ctx, "(standard output): %s.\n", errstr()); } } diff --git a/src/diag.c b/src/diag.c index bb744f6..3594b84 100644 --- a/src/diag.c +++ b/src/diag.c @@ -38,10 +38,6 @@ noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...) { abort(); } -const char *bfs_errstr(void) { - return xstrerror(errno); -} - const char *debug_flag_name(enum debug_flags flag) { switch (flag) { case DEBUG_COST: @@ -68,7 +64,7 @@ const char *debug_flag_name(enum debug_flags flag) { } void bfs_perror(const struct bfs_ctx *ctx, const char *str) { - bfs_error(ctx, "%s: %m.\n", str); + bfs_error(ctx, "%s: %s.\n", str, errstr()); } void bfs_error(const struct bfs_ctx *ctx, const char *format, ...) { @@ -95,19 +91,12 @@ bool bfs_debug(const struct bfs_ctx *ctx, enum debug_flags flag, const char *for } void bfs_verror(const struct bfs_ctx *ctx, const char *format, va_list args) { - int error = errno; - bfs_error_prefix(ctx); - - errno = error; cvfprintf(ctx->cerr, format, args); } bool bfs_vwarning(const struct bfs_ctx *ctx, const char *format, va_list args) { - int error = errno; - if (bfs_warning_prefix(ctx)) { - errno = error; cvfprintf(ctx->cerr, format, args); return true; } else { @@ -116,10 +105,7 @@ bool bfs_vwarning(const struct bfs_ctx *ctx, const char *format, va_list args) { } bool bfs_vdebug(const struct bfs_ctx *ctx, enum debug_flags flag, const char *format, va_list args) { - int error = errno; - if (bfs_debug_prefix(ctx, flag)) { - errno = error; cvfprintf(ctx->cerr, format, args); return true; } else { diff --git a/src/diag.h b/src/diag.h index f2498b5..da22f3a 100644 --- a/src/diag.h +++ b/src/diag.h @@ -9,6 +9,7 @@ #define BFS_DIAG_H #include "prelude.h" +#include "bfstd.h" #include /** @@ -54,16 +55,11 @@ void bfs_diagf(const struct bfs_loc *loc, const char *format, ...); */ #define bfs_diag(...) bfs_diagf(bfs_location(), __VA_ARGS__) -/** - * Get the last error message. - */ -const char *bfs_errstr(void); - /** * Print a diagnostic message including the last error. */ #define bfs_ediag(...) \ - bfs_ediag_("" __VA_ARGS__, bfs_errstr()) + bfs_ediag_("" __VA_ARGS__, errstr()) #define bfs_ediag_(format, ...) \ bfs_diag(sizeof(format) > 1 ? format ": %s" : "%s", __VA_ARGS__) @@ -84,7 +80,7 @@ noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...); * Abort with a message including the last error. */ #define bfs_eabort(...) \ - bfs_eabort_("" __VA_ARGS__, bfs_errstr()) + bfs_eabort_("" __VA_ARGS__, errstr()) #define bfs_eabort_(format, ...) \ bfs_abort(sizeof(format) > 1 ? format ": %s" : "%s", __VA_ARGS__) @@ -117,7 +113,7 @@ noreturn void bfs_abortf(const struct bfs_loc *loc, const char *format, ...); * Unconditional assert, including the last error. */ #define bfs_everify(...) \ - bfs_everify_(#__VA_ARGS__, __VA_ARGS__, "", bfs_errstr()) + bfs_everify_(#__VA_ARGS__, __VA_ARGS__, "", errstr()) #define bfs_everify_(str, cond, format, ...) \ ((cond) ? (void)0 : bfs_abort( \ diff --git a/src/eval.c b/src/eval.c index 4fcda60..3d75f80 100644 --- a/src/eval.c +++ b/src/eval.c @@ -63,7 +63,6 @@ static void eval_error(struct bfs_eval *state, const char *format, ...) { // By POSIX, any errors should be accompanied by a non-zero exit status *state->ret = EXIT_FAILURE; - int error = errno; const struct bfs_ctx *ctx = state->ctx; CFILE *cerr = ctx->cerr; @@ -71,7 +70,6 @@ static void eval_error(struct bfs_eval *state, const char *format, ...) { va_list args; va_start(args, format); - errno = error; cvfprintf(cerr, format, args); va_end(args); } @@ -90,7 +88,7 @@ static bool eval_should_ignore(const struct bfs_eval *state, int error) { */ static void eval_report_error(struct bfs_eval *state) { if (!eval_should_ignore(state, errno)) { - eval_error(state, "%m.\n"); + eval_error(state, "%s.\n", errstr()); } } @@ -99,9 +97,9 @@ static void eval_report_error(struct bfs_eval *state) { */ static void eval_io_error(const struct bfs_expr *expr, struct bfs_eval *state) { if (expr->path) { - eval_error(state, "'%s': %m.\n", expr->path); + eval_error(state, "'%s': %s.\n", expr->path, errstr()); } else { - eval_error(state, "(standard output): %m.\n"); + eval_error(state, "(standard output): %s.\n", errstr()); } // Don't report the error again in bfs_ctx_free() @@ -228,7 +226,7 @@ bool eval_context(const struct bfs_expr *expr, struct bfs_eval *state) { static const struct timespec *eval_stat_time(const struct bfs_stat *statbuf, enum bfs_stat_field field, struct bfs_eval *state) { const struct timespec *ret = bfs_stat_time(statbuf, field); if (!ret) { - eval_error(state, "Couldn't get file %s: %m.\n", bfs_stat_field_name(field)); + eval_error(state, "Couldn't get file %s: %s.\n", bfs_stat_field_name(field), errstr()); } return ret; } @@ -398,7 +396,7 @@ static int eval_exec_finish(const struct bfs_expr *expr, const struct bfs_ctx *c if (expr->eval_fn == eval_exec) { if (bfs_exec_finish(expr->exec) != 0) { if (errno != 0) { - bfs_error(ctx, "%s %s: %m.\n", expr->argv[0], expr->argv[1]); + bfs_error(ctx, "%s %s: %s.\n", expr->argv[0], expr->argv[1], errstr()); } ret = -1; } @@ -419,7 +417,7 @@ static int eval_exec_finish(const struct bfs_expr *expr, const struct bfs_ctx *c bool eval_exec(const struct bfs_expr *expr, struct bfs_eval *state) { bool ret = bfs_exec(expr->exec, state->ftwbuf) == 0; if (errno != 0) { - eval_error(state, "%s %s: %m.\n", expr->argv[0], expr->argv[1]); + eval_error(state, "%s %s: %s.\n", expr->argv[0], expr->argv[1], errstr()); } return ret; } @@ -902,7 +900,7 @@ bool eval_regex(const struct bfs_expr *expr, struct bfs_eval *state) { eval_error(state, "%s.\n", str); free(str); } else { - eval_error(state, "bfs_regerror(): %m.\n"); + eval_error(state, "bfs_regerror(): %s.\n", errstr()); } } @@ -1020,7 +1018,7 @@ static int eval_gettime(struct bfs_eval *state, struct timespec *ts) { #ifdef BFS_CLOCK int ret = clock_gettime(BFS_CLOCK, ts); if (ret != 0) { - bfs_warning(state->ctx, "%pP: clock_gettime(): %m.\n", state->ftwbuf); + bfs_warning(state->ctx, "%pP: clock_gettime(): %s.\n", state->ftwbuf, errstr()); } return ret; #else @@ -1619,7 +1617,7 @@ int bfs_eval(struct bfs_ctx *ctx) { if (ctx->status) { args.bar = bfs_bar_show(); if (!args.bar) { - bfs_warning(ctx, "Couldn't show status bar: %m.\n\n"); + bfs_warning(ctx, "Couldn't show status bar: %s.\n\n", errstr()); } } diff --git a/src/parse.c b/src/parse.c index a1155c0..8a8eb41 100644 --- a/src/parse.c +++ b/src/parse.c @@ -381,7 +381,7 @@ static int expr_open(struct bfs_parser *parser, struct bfs_expr *expr, const cha return 0; fail: - parse_expr_error(parser, expr, "%m.\n"); + parse_expr_error(parser, expr, "%s.\n", errstr()); if (cfile) { cfclose(cfile); } else if (file) { @@ -401,7 +401,7 @@ static int stat_arg(const struct bfs_parser *parser, char **arg, struct bfs_stat int ret = bfs_stat(AT_FDCWD, *arg, flags, sb); if (ret != 0) { - parse_argv_error(parser, arg, 1, "%m.\n"); + parse_argv_error(parser, arg, 1, "%s.\n", errstr()); } return ret; } @@ -1344,7 +1344,7 @@ static struct bfs_expr *parse_files0_from(struct bfs_parser *parser, int arg1, i file = xfopen(from, O_RDONLY | O_CLOEXEC); } if (!file) { - parse_expr_error(parser, expr, "%m.\n"); + parse_expr_error(parser, expr, "%s.\n", errstr()); return NULL; } @@ -1509,7 +1509,7 @@ static struct bfs_expr *parse_fstype(struct bfs_parser *parser, int arg1, int ar } if (!bfs_ctx_mtab(parser->ctx)) { - parse_expr_error(parser, expr, "Couldn't parse the mount table: %m.\n"); + parse_expr_error(parser, expr, "Couldn't parse the mount table: %s.\n", errstr()); return NULL; } @@ -1534,7 +1534,7 @@ static struct bfs_expr *parse_group(struct bfs_parser *parser, int arg1, int arg return NULL; } } else if (errno) { - parse_expr_error(parser, expr, "%m.\n"); + parse_expr_error(parser, expr, "%s.\n", errstr()); return NULL; } else { parse_expr_error(parser, expr, "No such group.\n"); @@ -1577,7 +1577,7 @@ static struct bfs_expr *parse_user(struct bfs_parser *parser, int arg1, int arg2 return NULL; } } else if (errno) { - parse_expr_error(parser, expr, "%m.\n"); + parse_expr_error(parser, expr, "%s.\n", errstr()); return NULL; } else { parse_expr_error(parser, expr, "No such user.\n"); @@ -1737,7 +1737,7 @@ static int parse_reftime(const struct bfs_parser *parser, struct bfs_expr *expr) if (xgetdate(expr->argv[1], &expr->reftime) == 0) { return 0; } else if (errno != EINVAL) { - parse_expr_error(parser, expr, "%m.\n"); + parse_expr_error(parser, expr, "%s.\n", errstr()); return -1; } diff --git a/tests/tests.h b/tests/tests.h index 19b7f5e..de95a49 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -9,6 +9,7 @@ #define BFS_TESTS_H #include "prelude.h" +#include "bfstd.h" #include "diag.h" /** Unit test function type. */ @@ -61,7 +62,7 @@ static inline bool bfs_check(bool ret) { * Check a condition, logging the current error string on failure. */ #define bfs_echeck(...) \ - bfs_echeck_(#__VA_ARGS__, __VA_ARGS__, "", bfs_errstr()) + bfs_echeck_(#__VA_ARGS__, __VA_ARGS__, "", errstr()) #define bfs_echeck_(str, cond, format, ...) \ ((cond) ? true : (bfs_diag( \ -- cgit v1.2.3