forked from mariusae/trickle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
180 lines (150 loc) · 3.13 KB
/
print.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* print.c
*
* Copyright (c) 2001, 2002 Marius Aamodt Eriksen <[email protected]>
* All rights reserved.
*
* $Id: print.c,v 1.5 2003/03/06 05:54:44 marius Exp $
*/
/*
* Some code adopted from:
*
* err.c
*
* Adapted from OpenBSD libc *err* *warn* code.
*
* Copyright (c) 2000 Dug Song <[email protected]>
*
* Copyright (c) 1993
* The Regents of the University of California. All rights reserved.
*/
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
static void vprint(const char *, va_list);
static void vprintx(const char *, va_list);
static int verbose, use_syslog;
extern char *__progname;
void
print_setup(int _verbose, int _use_syslog)
{
verbose = _verbose;
use_syslog = _use_syslog;
if (use_syslog)
openlog(__progname, LOG_PID, LOG_DAEMON);
}
/*
* These are adopted from the OpenBSD err*() and warn*() functions.
*/
void
errv(int level, int eval, const char *fmt, ...)
{
va_list ap;
if (level > verbose)
exit(eval);
va_start(ap, fmt);
vprint(fmt, ap);
va_end(ap);
exit(eval);
}
void
errxv(int level, int eval, const char *fmt, ...)
{
va_list ap;
if (level > verbose)
exit(eval);
va_start(ap, fmt);
vprintx(fmt, ap);
va_end(ap);
exit(eval);
}
void
warnv(int level, const char *fmt, ...)
{
va_list ap;
if (level > verbose)
return;
va_start(ap, fmt);
vprint(fmt, ap);
va_end(ap);
}
void
warnxv(int level, const char *fmt, ...)
{
va_list ap;
if (level > verbose)
return;
va_start(ap, fmt);
vprintx(fmt, ap);
va_end(ap);
}
static void
vprint(const char *fmt, va_list ap)
{
if (use_syslog) {
char msg[1024];
if (fmt != NULL) {
msg[0] = '\0';
vsnprintf(msg, sizeof(msg), fmt, ap);
strlcat(msg, ": ", sizeof(msg));
strlcat(msg, strerror(errno), sizeof(msg));
syslog(LOG_INFO, "%s", msg);
}
return;
}
fprintf(stderr, "%s: ", __progname);
if (fmt != NULL)
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": %s\n", strerror(errno));
}
static void
vprintx(const char *fmt, va_list ap)
{
if (use_syslog) {
if (fmt != NULL)
vsyslog(LOG_INFO, fmt, ap);
return;
}
fprintf(stderr, "%s: ", __progname);
if (fmt != NULL)
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
}
/*
* Assumes 80 character wide screen. Use termcap, or etc. to
* determine real width and do this dynamically.
* if (ioctl(fileno(stdout), TIOCGWINSZ, &winsz) != -1)
* width = winsz.ws_col && winsz.ws_col < 256 ? winsz.ws_col : 80;
* else
* width = 80;
*/
/*
* XXX only do if isatty(); ...
*/
void
print_dump(u_char *buf, int len)
{
int i, j, goback;
printf("%s: ", __progname);
for (i = 0; i < len; ++i) {
printf("%02x ", buf[i]);
if ((goback = i % 16) == 15 || i == len - 1) {
for (j = 15 - goback; j >= 0; j--) printf(" ");
for (j = i - goback; j <= i; j++)
if (buf[j] > 31 && buf[j] < 127)
printf("%c", buf[j]);
else
printf(".");
if (i != len - 1)
printf("\n%s: ", __progname);
}
}
printf("\n");
}