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
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
*/
/*
* This file exercises the various err(3C)/warn(3C) functions and produces
* output that is checked by the corresponding err.ksh script.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <err.h>
#include <sys/debug.h>
static FILE *stream = stderr;
typedef enum variant {
VARIANT_ = 0, /* warn(), err() */
VARIANT_C, /* warnc(), errc() */
VARIANT_X, /* warnx(), errx() */
VARIANT_V, /* vwarn(), verr() */
VARIANT_VC, /* vwarnc(), verrc() */
VARIANT_VX, /* vwarnx(), verrx() */
} variant_t;
void
usage(void)
{
(void) fprintf(stderr,
"usage: err [-e errno] [-x code] [-v variant]\n");
exit(EXIT_FAILURE);
}
void
callback_func(int code)
{
(void) fprintf(stream, "CALLBACK %d\n", code);
}
void
xtest(variant_t variant, int errcode, int exitcode, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
switch (variant) {
case VARIANT_V:
errno = errcode;
if (exitcode != 0)
verr(exitcode, fmt, va);
else
vwarn(fmt, va);
break;
case VARIANT_VC:
errno = 0;
if (exitcode != 0)
verrc(exitcode, errcode, fmt, va);
else
vwarnc(errcode, fmt, va);
break;
case VARIANT_VX:
if (exitcode != 0)
verrx(exitcode, fmt, va);
else
vwarnx(fmt, va);
break;
default:
errx(EXIT_FAILURE, "Unhandled variant in %s", __func__);
}
va_end(va);
}
int
main(int argc, char **argv)
{
int errcode = 0;
int exitcode = 0;
variant_t variant = VARIANT_;
const char *errstr;
long long num;
int ch;
/*
* -e specify errno for the test
* -v select variant to test
* -x specify exit code for the test
*/
while ((ch = getopt(argc, argv, "e:v:x:")) != -1) {
switch (ch) {
case 'e':
num = strtonum(optarg, 0, 127, &errstr);
if (errstr != NULL)
errx(EXIT_FAILURE, "-x: %s", errstr);
errcode = (int)num;
break;
case 'v':
num = strtonum(optarg, 0, VARIANT_VX, &errstr);
if (errstr != NULL)
errx(EXIT_FAILURE, "-v: %s", errstr);
switch (num) {
case VARIANT_:
variant = VARIANT_;
break;
case VARIANT_C:
variant = VARIANT_C;
break;
case VARIANT_X:
variant = VARIANT_X;
break;
case VARIANT_V:
variant = VARIANT_V;
break;
case VARIANT_VC:
variant = VARIANT_VC;
break;
case VARIANT_VX:
variant = VARIANT_VX;
break;
default:
errx(EXIT_FAILURE, "Unknown variant %ld", num);
}
break;
case 'x':
num = strtonum(optarg, 0, 127, &errstr);
if (errstr != NULL)
errx(EXIT_FAILURE, "-x: %s", errstr);
exitcode = (int)num;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc > 0)
errx(EXIT_FAILURE, "Unexpected argument '%s'.", argv[1]);
switch (variant) {
case VARIANT_:
errno = errcode;
if (exitcode != 0)
err(exitcode, "E/%d/%d", variant, exitcode);
else
warn("W/%d", variant);
break;
case VARIANT_C:
errno = 0;
if (exitcode != 0)
errc(exitcode, errcode, "E/%d/%d", variant, exitcode);
else
warnc(errcode, "W/%d", variant);
break;
case VARIANT_X:
if (exitcode != 0)
errx(exitcode, "E/%d/%d", variant, exitcode);
else
warnx("W/%d", variant);
break;
case VARIANT_V:
case VARIANT_VC:
case VARIANT_VX:
if (exitcode != 0) {
xtest(variant, errcode, exitcode, "E/%d/%d", variant,
exitcode);
} else {
xtest(variant, errcode, exitcode, "W/%d", variant);
}
break;
}
return (0);
}
|