| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 | #include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cerr.h"
#define SGR_BOLD "\33[1m"
#define SGR_DONE "\33[0m"
static bool color;
static const char *progname;
void
cerrinit(const char *s)
{
	const char *p = strrchr(s, '/');
	progname = p ? p + 1 : s;
	if (isatty(STDOUT_FILENO)) {
		const char *ev = getenv("NO_COLOR");
		if (!ev || !*ev)
			color = true;
	}
}
void
die(const char *fmt, ...)
{
	va_list ap;
	int e = errno;
	va_start(ap, fmt);
	fprintf(stderr, "%s%s:%s ", color ? SGR_BOLD : "", progname,
	        color ? SGR_DONE : "");
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, ": %s\n", strerror(e));
	va_end(ap);
	exit(EXIT_FAILURE);
}
void
diex(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "%s%s:%s ", color ? SGR_BOLD : "", progname,
	        color ? SGR_DONE : "");
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
	va_end(ap);
	exit(EXIT_FAILURE);
}
void
die_with_off(const char *file, size_t off, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "%s%s:%s:%zu:%s ", color ? SGR_BOLD : "", progname, file,
	        off, color ? SGR_DONE : "");
	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
	va_end(ap);
	exit(EXIT_FAILURE);
}
 |