Subversion Repositories planix.SVN

Rev

Rev 57 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
57 7u83 1
/*-
2
 * Copyright (c) 1993
3
 *	The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
 
30
 
31
#include <err.h>
32
#include <errno.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
 
38
 
39
static FILE *err_file; /* file to use for error output */
40
static void (*err_exit)(int);
41
 
42
/*
43
 * This is declared to take a `void *' so that the caller is not required
44
 * to include <stdio.h> first.  However, it is really a `FILE *', and the
45
 * manual page documents it as such.
46
 */
47
void
48
err_set_file(void *fp)
49
{
50
	if (fp)
51
		err_file = fp;
52
	else
53
		err_file = stderr;
54
}
55
 
56
void
57
err_set_exit(void (*ef)(int))
58
{
59
	err_exit = ef;
60
}
61
 
62
__weak_reference(_err, err);
63
 
64
void
65
err(int eval, const char *fmt, ...)
66
{
67
	va_list ap;
68
	va_start(ap, fmt);
69
	verrc(eval, errno, fmt, ap);
70
	va_end(ap);
71
}
72
 
73
void
74
verr(int eval, const char *fmt, va_list ap)
75
{
76
	verrc(eval, errno, fmt, ap);
77
}
78
 
79
void
80
errc(int eval, int code, const char *fmt, ...)
81
{
82
	va_list ap;
83
	va_start(ap, fmt);
84
	verrc(eval, code, fmt, ap);
85
	va_end(ap);
86
}
87
 
88
void
89
verrc(int eval, int code, const char *fmt, va_list ap)
90
{
91
	if (err_file == NULL)
92
		err_set_file(NULL);
93
	fprintf(err_file, "%s: ", getprogname());
94
	if (fmt != NULL) {
95
		vfprintf(err_file, fmt, ap);
96
		fprintf(err_file, ": ");
97
	}
98
	fprintf(err_file, "%s\n", strerror(code));
99
	if (err_exit)
100
		err_exit(eval);
101
	exit(eval);
102
}
103
 
104
void
105
errx(int eval, const char *fmt, ...)
106
{
107
	va_list ap;
108
	va_start(ap, fmt);
109
	verrx(eval, fmt, ap);
110
	va_end(ap);
111
}
112
 
113
void
114
verrx(int eval, const char *fmt, va_list ap)
115
{
116
	if (err_file == NULL)
117
		err_set_file(NULL);
118
	fprintf(err_file, "%s: ", getprogname());
119
	if (fmt != NULL)
120
		vfprintf(err_file, fmt, ap);
121
	fprintf(err_file, "\n");
122
	if (err_exit)
123
		err_exit(eval);
124
	exit(eval);
125
}
126
 
127
 
128
void
129
_warn(const char *fmt, ...)
130
{
131
	va_list ap;
132
	va_start(ap, fmt);
133
	vwarnc(errno, fmt, ap);
134
	va_end(ap);
135
}
136
 
137
void
138
vwarn(const char *fmt, va_list ap)
139
{
140
	vwarnc(errno, fmt, ap);
141
}
142
 
143
void
144
warnc(int code, const char *fmt, ...)
145
{
146
	va_list ap;
147
	va_start(ap, fmt);
148
	vwarnc(code, fmt, ap);
149
	va_end(ap);
150
}
151
 
152
void
153
vwarnc(int code, const char *fmt, va_list ap)
154
{
155
	if (err_file == NULL)
156
		err_set_file(NULL);
157
	fprintf(err_file, "%s: ", getprogname());
158
	if (fmt != NULL) {
159
		vfprintf(err_file, fmt, ap);
160
		fprintf(err_file, ": ");
161
	}
162
	fprintf(err_file, "%s\n", strerror(code));
163
}
164
 
165
void
166
warnx(const char *fmt, ...)
167
{
168
	va_list ap;
169
	va_start(ap, fmt);
170
	vwarnx(fmt, ap);
171
	va_end(ap);
172
}
173
 
174
void
175
vwarnx(const char *fmt, va_list ap)
176
{
177
	if (err_file == NULL)
178
		err_set_file(NULL);
179
	fprintf(err_file, "%s: ", getprogname());
180
	if (fmt != NULL)
181
		vfprintf(err_file, fmt, ap);
182
	fprintf(err_file, "\n");
183
}