-
Notifications
You must be signed in to change notification settings - Fork 2
/
carp.c
68 lines (61 loc) · 1.25 KB
/
carp.c
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
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include "citaa.h"
static void v(int is_croak, int is_x, int exit_code, const char *fmt, va_list ap);
void
croak(int exit_code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
v(1, errno, exit_code, fmt, ap);
va_end(ap);
}
void
croakx(int exit_code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
v(1, -1, exit_code, fmt, ap);
va_end(ap);
}
void
v(int is_croak, int use_errno, int exit_code, const char *fmt, va_list ap)
{
fprintf(stderr, "%s: ", thisprogname());
if (fmt != NULL) {
vfprintf(stderr, fmt, ap);
if (use_errno >= 0)
fprintf(stderr, ": ");
}
if (use_errno >= 0)
fprintf(stderr, "%s\n", strerror(use_errno));
else
fprintf(stderr, "\n");
if (is_croak)
exit(exit_code);
}
#if defined(__linux__)
static char proggy[MAXPATHLEN];
#endif
const char *thisprogname(void)
{
#if defined(__FreeBSD__)
return getprogname();
#elif defined(__APPLE__)
return getprogname();
#elif defined(__sun__)
return getexecname();
#elif defined(__linux__)
if (readlink("/proc/self/exe", proggy, MAXPATHLEN) != -1)
return proggy;
return "";
#else
#error "unsupported OS"
#endif
}