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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/************************************************************************
* Error handling routines.
*
* The functions in this file are independent of any application
* variables, and may be used with any C program.
* Either of the names CLIENT or SERVER may be defined when compiling
* this function. If neither are defined, we assume CLIENT.
*/
//#include <varargs.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <iostream>
using namespace std;
#ifdef CLIENT
#ifdef SERVER
cant define both CLIENT and SERVER
#endif
#endif
#ifndef CLIENT
#ifndef SERVER
#define CLIENT 1 /* default to client */
#endif
#endif
#ifndef NULL
#define NULL ((void *) 0)
#endif
char *pname = NULL;
#ifdef CLIENT /* these all output to stderr */
/* ------------------- BEGIN CLIENT ------------------- */
/*
* Print the UNIX errno value.
*/
void my_perror()
{
//char *sys_err_str();
char *str = strerror(errno);
cerr << str << endl;
//fprintf(stderr, " %s\n", sys_err_str());
}
/*
* Print the UNIX errno value.
* We just append it to the end of the emesgstr[] array.
*/
/*
* Fatal error. Print a message and terminate.
* Don't dump core and don't print the system's errno value.
*
* err_quit(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_quit(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (pname != NULL)
cerr << pname;
fmt = va_arg(args, char *);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
va_end(args);
exit(1);
}
/*
* Fatal error related to a system call. Print a message and terminate.
* Don't dump core, but do print the system's errno value and its
* associated message.
*
* err_sys(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_sys(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (pname != NULL) {
cerr << "if (pname != NULL) {" << endl;
fprintf(stderr, "%s: ", pname);
}
fmt = va_arg(args, char *);
vfprintf(stderr, fmt, args);
va_end(args);
my_perror();
exit(1);
}
/*
* Recoverable error. Print a message, and return to caller.
*
* err_ret(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_ret(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (pname != NULL)
fprintf(stderr, "%s: ", pname);
fmt = va_arg(args, char *);
vfprintf(stderr, fmt, args);
va_end(args);
my_perror();
fflush(stdout);
fflush(stderr);
return;
}
/*
* Fatal error. Print a message, dump core (for debugging) and terminate.
*
* err_dump(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_dump(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
if (pname != NULL)
fprintf(stderr, "%s: ", pname);
fmt = va_arg(args, char *);
vfprintf(stderr, fmt, args);
va_end(args);
my_perror();
fflush(stdout); /* abort doesn't flush stdio buffers */
fflush(stderr);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
#endif /* ------------------- END CLIENT ------------------- */
/* ------------------- BEGIN SERVER ------------------- */
#ifdef SERVER
#ifdef BSD
/*
* Under BSD, these server routines use the syslog(3) facility.
* They don't append a newline, for example.
*/
#include <syslog.h>
#else /* not BSD */
/*
* There really ought to be a better way to handle server logging
* under System V.
*/
#define syslog(a,b) cerr << "syslog: "<< (b)
#define openlog(a,b,c) cerr << (a)
#endif /* BSD */
/*
* Print the UNIX errno value.
* We just append it to the end of the emesgstr[] array.
*/
char emesgstr[255] = {"test"}; /* used by all server routines */
void my_perror()
{
// register int len;
//char *sys_err_str();
// len = strlen(emesgstr);
cerr << emesgstr
<< strerror(errno)
<< endl;
//sprintf(emesgstr + len, " %s", sys_err_str());
}
/*
* Identify ourself, for syslog() messages.
*
* LOG_PID is an option that says prepend each message with our pid.
* LOG_CONS is an option that says write to console if unable to send
* the message to syslogd.
* LOG_DAEMON is our facility.
*/
void err_init(char *ident)
{
openlog(ident, (LOG_PID | LOG_CONS), LOG_DAEMON);
}
/*
* Fatal error. Print a message and terminate.
* Don't print the system's errno value.
*
* err_quit(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_quit(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fmt = va_arg(args, char *);
vsprintf(emesgstr, fmt, args);
va_end(args);
syslog(LOG_ERR, emesgstr);
exit(1);
}
/*
* Fatal error related to a system call. Print a message and terminate.
* Don't dump core, but do print the system's errno value and its
* associated message.
*
* err_sys(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_sys(char *fmt, ...)
{
cerr << "err_sys: " << fmt << endl;
va_list args;
va_start(args, fmt);
fmt = va_arg(args, char *);
vsprintf(emesgstr, fmt, args);
va_end(args);
my_perror();
syslog(LOG_ERR, emesgstr);
exit(1);
}
/*
* Recoverable error. Print a message, and return to caller.
*
* err_ret(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_ret(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fmt = va_arg(args, char *);
vsprintf(emesgstr, fmt, args);
va_end(args);
my_perror();
syslog(LOG_ERR, emesgstr);
return;
}
/*
* Fatal error. Print a message, dump core (for debugging) and terminate.
*
* err_dump(str, arg1, arg2, ...)
*
* The string "str" must specify the conversion specification for any args.
*/
/*VARARGS1*/
void err_dump(char *fmt, ...)
{
va_list args;
va_start(args,fmt);
fmt = va_arg(args, char *);
vsprintf(emesgstr, fmt, args);
va_end(args);
my_perror();
syslog(LOG_ERR, emesgstr);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
#endif /* ------------------- SERVER ------------------- */
|