util.c: output function might override errno and thus affect perror()

Original patch by Raymond Cole with some modifications, thanks!

ref.
https://git.suckless.org/dwm/commit/fcb2476b693ca4c40ad32c7119e27bbeb856865c.html
This commit is contained in:
bakkeby
2024-10-29 22:21:11 +01:00
parent 8ae6f04654
commit 38950399fe
2 changed files with 7 additions and 7 deletions

12
util.c
View File

@@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -10,17 +11,16 @@ void
die(const char *fmt, ...)
{
va_list ap;
int saved_errno;
saved_errno = errno;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
if (fmt[0] && fmt[strlen(fmt)-1] == ':')
fprintf(stderr, " %s", strerror(saved_errno));
fputc('\n', stderr);
exit(1);
}