Repository navigation
Expand file tree
/
Copy pathreport.c
More file actions
280 lines (232 loc) · 5.45 KB
/
Copy pathreport.c
File metadata and controls
280 lines (232 loc) · 5.45 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright (c) 1995-1998 by Cisco systems, Inc.
Permission to use, copy, modify, and distribute this software for
any purpose and without fee is hereby granted, provided that this
copyright and permission notice appear on all copies of the
software and supporting documentation, the name of Cisco Systems,
Inc. not be used in advertising or publicity pertaining to
distribution of the program without specific prior permission, and
notice be given in supporting documentation that modification,
copying and distribution is by permission of Cisco Systems, Inc.
Cisco Systems, Inc. makes no representations about the suitability
of this software for any purpose. THIS SOFTWARE IS PROVIDED ``AS
IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE.
Modified by Olivier BEDOUET
ChangeLog:
- 2009/17/12: if debug, use logfile and not syslog, if not check
logfile value or use syslog
- 2010/2/22: if can't open logfile in debug, log an error to syslog
*/
#define REPORT_C
#include "tac_plus.h"
#include <stdio.h>
#ifdef AIX
#include <sys/types.h>
#else
#include <time.h>
#endif
#ifdef __STDC__
#include <stdarg.h> /* ANSI C, variable length args */
#else
#include <varargs.h> /* has 'vararg' definitions */
#endif
FILE *ostream = NULL;
/*char *logfile = LOGFILE_DEFAULT;*/
char *logfile = NULL;
/* report:
*
* This routine reports errors and such via stderr and syslog() if
* appopriate. It just helps avoid a lot of if-else in the code.
*
* LOG_DEBUG messages are ignored unless debugging is on.
* All other priorities are always logged to syslog.
*/
#ifdef __STDC__
void
report(int priority, char *fmt,...)
#else
/* VARARGS2 */
void
report(priority, fmt, va_alist)
int priority;
char *fmt;
va_dcl /* no terminating semi-colon */
#endif
{
char msg[255]; /* temporary string */
char *fp, *bufp, *charp;
int len, m, i, n;
char digits[16];
va_list ap;
#ifdef __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
/* ensure that msg is never overwritten */
n = 255;
fp = fmt;
len = 0;
msg[n-1] = '\0';
bufp = msg;
while (*fp) {
if (*fp != '%') {
if ((len+1) >= n) {
break;
}
*bufp++ = *fp++;
len++;
continue;
}
/* seen a '%' */
fp++;
switch (*fp) {
case 's':
fp++;
charp = va_arg(ap, char *);
m = strlen(charp);
break;
case 'u':
fp++;
i = va_arg(ap, uint);
sprintf(digits, "%u", i);
m = strlen(digits);
charp = digits;
break;
case 'x':
fp++;
i = va_arg(ap, uint);
sprintf(digits, "%x", i);
m = strlen(digits);
charp = digits;
break;
case 'd':
fp++;
i = va_arg(ap, int);
sprintf(digits, "%d", i);
m = strlen(digits);
charp = digits;
break;
}
if ((len + m + 1) >= n) {
break;
}
memcpy(bufp, charp, m);
bufp += m;
len += m;
continue;
}
msg[len] = '\0';
/* check we never overwrote the end of the buffer */
if (msg[n-1]) {
abort();
}
va_end(ap);
if (console) {
extern int errno;
if (!ostream)
ostream = fopen("/dev/console", "w");
if (ostream) {
if (priority == LOG_ERR)
fprintf(ostream, "Error ");
fprintf(ostream, "%s\n", msg);
}
else
syslog(LOG_ERR, "Cannot open /dev/console errno=%d", errno);
}
/* Olivier BEDOUET: If debug or logfile */
if (debug || logfile) {
int logfd;
if (logfile == NULL) /* Olivier BEDOUET: We are in debug mode */
logfile = LOGFILE_DEFAULT;
logfd = open(logfile, O_CREAT | O_WRONLY | O_APPEND, 0640);
if (logfd >= 0) {
char buf[512];
time_t t = time(NULL);
char *ct = ctime(&t);
ct[24] = '\0';
tac_lockfd(logfile, logfd);
sprintf(buf, "%s [%d]: ", ct, getpid());
write(logfd, buf, strlen(buf));
if (priority == LOG_ERR)
write(logfd, "Error ", 6);
write(logfd, msg, strlen(msg));
write(logfd, "\n", 1);
close(logfd);
/* 2010/02/22: Moved to here */
return; /* Olivier BEDOUET: If we have a logfile, don't use syslog */
}
else /* Olivier BEDOUET: if can't open logfile, do a report do syslog */
{
syslog(LOG_ERR, "Can't open logfile !");
}
}
if (single) {
fprintf(stderr, "%s\n", msg);
}
if (priority == LOG_DEBUG)
return;
if (priority == LOG_ERR)
syslog(priority, "Error %s", msg);
else
syslog(priority, "%s", msg);
}
/* format a hex dump for syslog */
void
report_hex(priority, p, len)
u_char *p;
int len;
{
char buf[256];
char digit[10];
int buflen;
int i;
if (len <= 0)
return;
buf[0] = '\0';
buflen = 0;
for (i = 0; i < len && i < 255; i++, p++) {
sprintf(digit, "0x%x ", *p);
strcat(buf, digit);
buflen += strlen(digit);
if (buflen > 75) {
report(priority, "%s", buf);
buf[0] = '\0';
buflen = 0;
}
}
if (buf[0]) {
report(priority, "%s", buf);
}
}
/* format a non-null terminated string for syslog */
void
report_string(priority, p, len)
u_char *p;
int len;
{
char buf[256];
char *bufp = buf;
int i;
if (len <= 0)
return;
for (i = 0; i < len && i < 255; i++) {
if (32 <= *p && *p <= 126) {
*bufp++ = *p++;
} else {
sprintf(bufp, " 0x%x ", *p);
bufp += strlen(bufp);
p++;
}
}
*bufp = '\0';
report(priority, "%s", buf);
}
void
regerror(s)
char *s;
{
report(LOG_ERR, "in regular expression %s", s);
}