-
Notifications
You must be signed in to change notification settings - Fork 1
/
err.c
121 lines (103 loc) · 2.16 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifdef __WIN32
#define NEED_ERR
#define LACK_PROGNAME
#define NEED_ENDIAN_STUBS
#define LACK_SYSTYPES
#endif
#ifdef NEED_ERR
/*
* Implementation of the err/errx/verr/verrx/warn/warnx/vwarn/vwarnx
* functions from BSD.
*
* This file is public-domain; anyone may deal in it without restriction.
*
* Written by Graue <graue@oceanbase.org> on January 16, 2006.
*/
/*
err/warn family of functions cheat sheet:
Print:
last component of program name
[ if fmt is non-NULL
": "
the formatted error message
]
[ if function name does not end in x
": "
strerror(errno)
]
newline
Then if function name has "err" in it, quit with exit code `eval'.
BSD's -x versions actually print ": " at the end if passed NULL, so I
duplicate that behavior. Passing these functions NULL is kind of useless
though.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#ifdef LACK_PROGNAME
char *__progname = "synth";
#else
extern char *__progname;
#endif
#define progname __progname
void vwarn(const char *fmt, va_list args)
{
fputs(progname, stderr);
if (fmt != NULL)
{
fputs(": ", stderr);
vfprintf(stderr, fmt, args);
}
fputs(": ", stderr);
fputs(strerror(errno), stderr);
putc('\n', stderr);
}
void vwarnx(const char *fmt, va_list args)
{
fputs(progname, stderr);
fputs(": ", stderr);
if (fmt != NULL)
vfprintf(stderr, fmt, args);
putc('\n', stderr);
}
void verr(int eval, const char *fmt, va_list args)
{
vwarn(fmt, args);
exit(eval);
}
void verrx(int eval, const char *fmt, va_list args)
{
vwarnx(fmt, args);
exit(eval);
}
void warn(const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
vwarn(fmt, argptr);
va_end(argptr);
}
void warnx(const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
vwarnx(fmt, argptr);
va_end(argptr);
}
void err(int eval, const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
verr(eval, fmt, argptr);
/* NOTREACHED, so don't worry about va_end() */
}
void errx(int eval, const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
verrx(eval, fmt, argptr);
/* NOTREACHED, so don't worry about va_end() */
}
#endif