util.c: output function might override errno and thus affect perror() 545031a0
Original patch by Raymond Cole with some modifications, thanks!
Hiltjo Posthuma · 2024-10-27 20:08 1 file(s) · +7 −6
util.c +7 −6
1 1
/* See LICENSE file for copyright and license details. */
2 +
#include <errno.h>
2 3
#include <stdarg.h>
3 4
#include <stdio.h>
4 5
#include <stdlib.h>
10 11
die(const char *fmt, ...)
11 12
{
12 13
	va_list ap;
14 +
	int saved_errno;
15 +
16 +
	saved_errno = errno;
13 17
14 18
	va_start(ap, fmt);
15 19
	vfprintf(stderr, fmt, ap);
16 20
	va_end(ap);
17 21
18 -
	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
19 -
		fputc(' ', stderr);
20 -
		perror(NULL);
21 -
	} else {
22 -
		fputc('\n', stderr);
23 -
	}
22 +
	if (fmt[0] && fmt[strlen(fmt)-1] == ':')
23 +
		fprintf(stderr, " %s", strerror(saved_errno));
24 +
	fputc('\n', stderr);
24 25
25 26
	exit(1);
26 27
}