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. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34
35#include <err.h>
36#include <errno.h>
37#include <stdarg.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41
42//#include "libc_private.h"
43#define __weak_reference(a, b)
44
45extern const char *_getprogname(void);
46
47static FILE *err_file; /* file to use for error output */
48static void (*err_exit)(int);
49
50void _err(int eval, const char *fmt, ...);
51void _warn(const char *fmt, ...);
52
53/*
54 * This is declared to take a `void *' so that the caller is not required
55 * to include <stdio.h> first.  However, it is really a `FILE *', and the
56 * manual page documents it as such.
57 */
58void
59err_set_file(void *fp)
60{
61	if (fp)
62		err_file = fp;
63	else
64		err_file = stderr;
65}
66
67void
68err_set_exit(void (*ef)(int))
69{
70	err_exit = ef;
71}
72
73__weak_reference(_err, err);
74
75void
76_err(int eval, const char *fmt, ...)
77{
78	va_list ap;
79	va_start(ap, fmt);
80	verrc(eval, errno, fmt, ap);
81	va_end(ap);
82}
83
84void
85verr(eval, fmt, ap)
86	int eval;
87	const char *fmt;
88	va_list ap;
89{
90	verrc(eval, errno, fmt, ap);
91}
92
93void
94errc(int eval, int code, const char *fmt, ...)
95{
96	va_list ap;
97	va_start(ap, fmt);
98	verrc(eval, code, fmt, ap);
99	va_end(ap);
100}
101
102void
103verrc(eval, code, fmt, ap)
104	int eval;
105	int code;
106	const char *fmt;
107	va_list ap;
108{
109	if (err_file == 0)
110		err_set_file((FILE *)0);
111	fprintf(err_file, "%s: ", _getprogname());
112	if (fmt != NULL) {
113		vfprintf(err_file, fmt, ap);
114		fprintf(err_file, ": ");
115	}
116	fprintf(err_file, "%s\n", strerror(code));
117	if (err_exit)
118		err_exit(eval);
119	exit(eval);
120}
121
122void
123errx(int eval, const char *fmt, ...)
124{
125	va_list ap;
126	va_start(ap, fmt);
127	verrx(eval, fmt, ap);
128	va_end(ap);
129}
130
131void
132verrx(eval, fmt, ap)
133	int eval;
134	const char *fmt;
135	va_list ap;
136{
137	if (err_file == 0)
138		err_set_file((FILE *)0);
139	fprintf(err_file, "%s: ", _getprogname());
140	if (fmt != NULL)
141		vfprintf(err_file, fmt, ap);
142	fprintf(err_file, "\n");
143	if (err_exit)
144		err_exit(eval);
145	exit(eval);
146}
147
148__weak_reference(_warn, warn);
149
150void
151_warn(const char *fmt, ...)
152{
153	va_list ap;
154	va_start(ap, fmt);
155	vwarnc(errno, fmt, ap);
156	va_end(ap);
157}
158
159void
160vwarn(fmt, ap)
161	const char *fmt;
162	va_list ap;
163{
164	vwarnc(errno, fmt, ap);
165}
166
167void
168warnc(int code, const char *fmt, ...)
169{
170	va_list ap;
171	va_start(ap, fmt);
172	vwarnc(code, fmt, ap);
173	va_end(ap);
174}
175
176void
177vwarnc(code, fmt, ap)
178	int code;
179	const char *fmt;
180	va_list ap;
181{
182	if (err_file == 0)
183		err_set_file((FILE *)0);
184	fprintf(err_file, "%s: ", _getprogname());
185	if (fmt != NULL) {
186		vfprintf(err_file, fmt, ap);
187		fprintf(err_file, ": ");
188	}
189	fprintf(err_file, "%s\n", strerror(code));
190}
191
192void
193warnx(const char *fmt, ...)
194{
195	va_list ap;
196	va_start(ap, fmt);
197	vwarnx(fmt, ap);
198	va_end(ap);
199}
200
201void
202vwarnx(fmt, ap)
203	const char *fmt;
204	va_list ap;
205{
206	if (err_file == 0)
207		err_set_file((FILE *)0);
208	fprintf(err_file, "%s: ", _getprogname());
209	if (fmt != NULL)
210		vfprintf(err_file, fmt, ap);
211	fprintf(err_file, "\n");
212}
213
214#pragma weak err=_err
215#pragma weak warn=_warn
216