From 248ef134f8ed4b45ee1bf4cd7f4e57b686661a21 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 28 Aug 2024 17:28:35 -0700 Subject: [PATCH] Prefer strerror to perror * src/patch.c (putline): Move from here ... * src/util.c: ... to here, and make it extern. * src/pch.c (there_is_another_patch): * src/util.c (pfatal): Use putline with strerror rather than attempting to work around old perror bugs. This also works better in the unlikely case where the program name length does not fit in int. --- src/patch.c | 13 ------------- src/pch.c | 5 +++-- src/util.c | 17 ++++++++++++++--- src/util.h | 1 + 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/patch.c b/src/patch.c index e6abd3a..d0c75ac 100644 --- a/src/patch.c +++ b/src/patch.c @@ -1321,19 +1321,6 @@ print_unidiff_range (FILE *fp, lin start, lin count) } } -/* Output to FP a line containing the concatenation of the remaining - string arguments. A null pointer terminates the string args. */ -static void -putline (FILE *fp, ...) -{ - va_list ap; - va_start (ap, fp); - for (char *arg; (arg = va_arg (ap, char *)); ) - fputs (arg, fp); - va_end (ap); - putc ('\n', fp); -} - static void print_header_line (FILE *fp, const char *tag, bool reverse) { diff --git a/src/pch.c b/src/pch.c index 35f839f..eb71152 100644 --- a/src/pch.c +++ b/src/pch.c @@ -297,10 +297,11 @@ there_is_another_patch (bool need_header, mode_t *file_type) inerrno = stat_file (inname, &instat); if (inerrno) { - perror (inname); + fputs (inname, stderr); + putline (stderr, ": ", strerror (inerrno), nullptr); fflush (stderr); free (inname); - inname = 0; + inname = nullptr; } else invc = -1; diff --git a/src/util.c b/src/util.c index 78c107c..6e1ffce 100644 --- a/src/util.c +++ b/src/util.c @@ -992,6 +992,19 @@ write_fatal (void) pfatal ("write error"); } +/* Output to FP a line containing the concatenation of the remaining + string arguments. A null pointer terminates the string args. */ +void +putline (FILE *fp, ...) +{ + va_list ap; + va_start (ap, fp); + for (char *arg; (arg = va_arg (ap, char *)); ) + fputs (arg, fp); + va_end (ap); + putc ('\n', fp); +} + /* Say something from patch, something from the system, then silence . . . */ void @@ -1004,9 +1017,7 @@ pfatal (char const *format, ...) va_start (args, format); vfprintf (stderr, format, args); va_end (args); - fflush (stderr); /* perror bypasses stdio on some hosts. */ - errno = errnum; - perror (" "); + putline (stderr, " : ", strerror (errnum), nullptr); fflush (stderr); fatal_exit (0); } diff --git a/src/util.h b/src/util.h index b59175d..3e686ff 100644 --- a/src/util.h +++ b/src/util.h @@ -64,6 +64,7 @@ void read_fatal (void) __attribute__ ((noreturn)); void remove_prefix (char *, size_t); void removedirs (char const *); void write_fatal (void) __attribute__ ((noreturn)); +void putline (FILE *, ...); void insert_file_id (struct stat const *, enum file_id_type); enum file_id_type lookup_file_id (struct stat const *); void set_queued_output (struct stat const *, bool);