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.
This commit is contained in:
Paul Eggert 2024-08-28 17:28:35 -07:00
parent 6cb321aff4
commit 248ef134f8
4 changed files with 18 additions and 18 deletions

View File

@ -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)
{

View File

@ -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;

View File

@ -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);
}

View File

@ -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);