diff options
| -rw-r--r-- | lib/ChangeLog | 5 | ||||
| -rw-r--r-- | lib/error.c | 32 |
2 files changed, 34 insertions, 3 deletions
diff --git a/lib/ChangeLog b/lib/ChangeLog index 96eaa330..c72452b1 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2021-09-10 Colin Cross <[email protected]> + + * error.c (error): Call fflush on stdout and stderr. Setup errno and + call verr, verrx, vwarn or vwarnx based on status and errnum. + 2021-09-06 Dmitry V. Levin <[email protected]> * color.c (parse_opt): Replace asprintf followed by error(EXIT_FAILURE) diff --git a/lib/error.c b/lib/error.c index 75e964fd..5186fc15 100644 --- a/lib/error.c +++ b/lib/error.c @@ -29,7 +29,9 @@ #include <config.h> #if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H) +#include <errno.h> #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <err.h> @@ -37,13 +39,37 @@ unsigned int error_message_count = 0; void error(int status, int errnum, const char *format, ...) { va_list argp; + int saved_errno = errno; + + fflush (stdout); va_start(argp, format); - verr(status, format, argp); + if (status) + { + if (errnum) + { + errno = errnum; + verr (status, format, argp); + } + else + verrx (status, format, argp); + } + else + { + if (errnum) + { + errno = errnum; + vwarn (format, argp); + } + else + vwarnx (format, argp); + } va_end(argp); - if (status) - exit(status); + fflush (stderr); + ++error_message_count; + + errno = saved_errno; } #endif |
