xref: /dragonfly/lib/libc/gen/err.c (revision 2ee85085)
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  *	From: @(#)err.c	8.1 (Berkeley) 6/4/93
33  *
34  * $FreeBSD: src/lib/libc/gen/err.c,v 1.6 1999/08/27 23:58:33 peter Exp $
35  * $DragonFly: src/lib/libc/gen/err.c,v 1.3 2005/03/09 18:52:21 joerg Exp $
36  */
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include <stdarg.h>
45 
46 
47 static FILE *err_file; /* file to use for error output */
48 static void (*err_exit)(int);
49 
50 /*
51  * This is declared to take a `void *' so that the caller is not required
52  * to include <stdio.h> first.  However, it is really a `FILE *', and the
53  * manual page documents it as such.
54  */
55 void
56 err_set_file(void *fp)
57 {
58 	if (fp)
59 		err_file = fp;
60 	else
61 		err_file = stderr;
62 }
63 
64 void
65 err_set_exit(void (*ef)(int))
66 {
67 	err_exit = ef;
68 }
69 
70 void
71 err(int eval, const char *fmt, ...)
72 {
73 	va_list ap;
74 	va_start(ap, fmt);
75 	verrc(eval, errno, fmt, ap);
76 	va_end(ap);
77 }
78 
79 void
80 verr(eval, fmt, ap)
81 	int eval;
82 	const char *fmt;
83 	va_list ap;
84 {
85 	verrc(eval, errno, fmt, ap);
86 }
87 
88 void
89 errc(int eval, int code, const char *fmt, ...)
90 {
91 	va_list ap;
92 	va_start(ap, fmt);
93 	verrc(eval, code, fmt, ap);
94 	va_end(ap);
95 }
96 
97 void
98 verrc(eval, code, fmt, ap)
99 	int eval;
100 	int code;
101 	const char *fmt;
102 	va_list ap;
103 {
104 	if (err_file == 0)
105 		err_set_file((FILE *)0);
106 	fprintf(err_file, "%s: ", getprogname());
107 	if (fmt != NULL) {
108 		vfprintf(err_file, fmt, ap);
109 		fprintf(err_file, ": ");
110 	}
111 	fprintf(err_file, "%s\n", strerror(code));
112 	if (err_exit)
113 		err_exit(eval);
114 	exit(eval);
115 }
116 
117 void
118 errx(int eval, const char *fmt, ...)
119 {
120 	va_list ap;
121 	va_start(ap, fmt);
122 	verrx(eval, fmt, ap);
123 	va_end(ap);
124 }
125 
126 void
127 verrx(eval, fmt, ap)
128 	int eval;
129 	const char *fmt;
130 	va_list ap;
131 {
132 	if (err_file == 0)
133 		err_set_file((FILE *)0);
134 	fprintf(err_file, "%s: ", getprogname());
135 	if (fmt != NULL)
136 		vfprintf(err_file, fmt, ap);
137 	fprintf(err_file, "\n");
138 	if (err_exit)
139 		err_exit(eval);
140 	exit(eval);
141 }
142 
143 void
144 warn(const char *fmt, ...)
145 {
146 	va_list ap;
147 	va_start(ap, fmt);
148 	vwarnc(errno, fmt, ap);
149 	va_end(ap);
150 }
151 
152 void
153 vwarn(fmt, ap)
154 	const char *fmt;
155 	va_list ap;
156 {
157 	vwarnc(errno, fmt, ap);
158 }
159 
160 void
161 warnc(int code, const char *fmt, ...)
162 {
163 	va_list ap;
164 	va_start(ap, fmt);
165 	vwarnc(code, fmt, ap);
166 	va_end(ap);
167 }
168 
169 void
170 vwarnc(code, fmt, ap)
171 	int code;
172 	const char *fmt;
173 	va_list ap;
174 {
175 	if (err_file == 0)
176 		err_set_file((FILE *)0);
177 	fprintf(err_file, "%s: ", getprogname());
178 	if (fmt != NULL) {
179 		vfprintf(err_file, fmt, ap);
180 		fprintf(err_file, ": ");
181 	}
182 	fprintf(err_file, "%s\n", strerror(code));
183 }
184 
185 void
186 warnx(const char *fmt, ...)
187 {
188 	va_list ap;
189 	va_start(ap, fmt);
190 	vwarnx(fmt, ap);
191 	va_end(ap);
192 }
193 
194 void
195 vwarnx(fmt, ap)
196 	const char *fmt;
197 	va_list ap;
198 {
199 	if (err_file == 0)
200 		err_set_file((FILE *)0);
201 	fprintf(err_file, "%s: ", getprogname());
202 	if (fmt != NULL)
203 		vfprintf(err_file, fmt, ap);
204 	fprintf(err_file, "\n");
205 }
206