-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherr.c
97 lines (81 loc) · 1.6 KB
/
err.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
/* (c) 2006, Quest Software, Inc. All rights reserved. */
#include "common.h"
#include "err.h"
#if HAVE_SYSLOG_H
# include <syslog.h>
#endif
#define ERR 1
#define WARN 0
#define X 2
#ifndef va_copy
# define va_copy(x,y) (x) = (y)
#endif
static void _err(int exitcode, int flags, const char *fmt, va_list ap);
static int syslog_enabled = 0;
void
err_enable_syslog(int enabled)
{
syslog_enabled = enabled;
}
static void
_err(int exitcode, int flags, const char *fmt, va_list ap)
{
int save_errno = errno;
#if HAVE_VSYSLOG
va_list ap2;
char fmt2[8192];
#endif
#if HAVE_VSYSLOG
if (syslog_enabled) {
va_copy(ap2, ap);
if ((flags & X) == 0) {
snprintf(fmt2, sizeof fmt2, "%s: %%m", fmt);
fmt = fmt2;
}
}
#endif
fprintf(stderr, "%s: ",
(flags & ERR) ? "error" : "warning");
vfprintf(stderr, fmt, ap);
if ((flags & X) == 0)
fprintf(stderr, ": %s", strerror(save_errno));
fprintf(stderr, "\n");
#if HAVE_VSYSLOG
if (syslog_enabled) {
errno = save_errno;
vsyslog(LOG_DAEMON | ((flags & ERR) ? LOG_ERR : LOG_WARNING), fmt, ap2);
}
#endif
if (flags & ERR)
exit(exitcode);
}
void
errx(int exitcode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_err(exitcode, ERR|X, fmt, ap);
}
void
err(int exitcode, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_err(exitcode, ERR, fmt, ap);
}
void
warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_err(0, WARN|X, fmt, ap);
va_end(ap);
}
void
warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_err(0, WARN, fmt, ap);
va_end(ap);
}