-
Notifications
You must be signed in to change notification settings - Fork 26
/
carp.c
118 lines (109 loc) · 2.46 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Part of DNS zone file validator `validns`.
*
* Copyright 2011-2014 Anton Berezin <[email protected]>
* Modified BSD license.
* (See LICENSE file in the distribution.)
*
*/
#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 "common.h"
#include "carp.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 *
bitch(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (!G.opt.no_output) {
fprintf(stderr, "%s:%d: ", file_info->name, file_info->line);
if (fmt != NULL) {
vfprintf(stderr, fmt, ap);
}
fprintf(stderr, "\n");
}
va_end(ap);
G.exit_code = 1;
G.stats.error_count++;
file_info->paren_mode = 0;
if (G.opt.die_on_first_error)
exit(1);
return NULL;
}
void *
moan(char *file_name, int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (!G.opt.no_output) {
fprintf(stderr, "%s:%d: ", file_name, line);
if (fmt != NULL) {
vfprintf(stderr, fmt, ap);
}
fprintf(stderr, "\n");
}
va_end(ap);
G.exit_code = 1;
G.stats.error_count++;
if (G.opt.die_on_first_error)
exit(1);
return NULL;
}
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__) || defined(__NetBSD__) || defined(__OpenBSD__)
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
}