make error reporting more robust

pull/50/head
Daniel Micay 2018-10-03 16:55:25 -04:00
parent 6dfe33b4f1
commit 1fbf0e27f5
1 changed files with 20 additions and 3 deletions

23
util.c
View File

@ -1,3 +1,4 @@
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -5,10 +6,26 @@
#include "util.h" #include "util.h"
static int write_full(int fd, const char *buf, size_t length) {
do {
ssize_t bytes_written = write(fd, buf, length);
if (bytes_written == -1) {
if (errno == EINTR) {
continue;
}
return -1;
}
buf += bytes_written;
length -= bytes_written;
} while (length);
return 0;
}
COLD noreturn void fatal_error(const char *s) { COLD noreturn void fatal_error(const char *s) {
const char *prefix = "fatal allocator error: "; const char *prefix = "fatal allocator error: ";
write(STDERR_FILENO, prefix, strlen(prefix)); (void)(write_full(STDERR_FILENO, prefix, strlen(prefix)) != -1 &&
write(STDERR_FILENO, s, strlen(s)); write_full(STDERR_FILENO, s, strlen(s)) != -1 &&
write(STDERR_FILENO, "\n", 1); write_full(STDERR_FILENO, "\n", 1));
abort(); abort();
} }