Initial import from FreeBSD RELENG_4:
[games.git] / lib / libc / gen / err.c
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
35 #if defined(LIBC_RCS) && !defined(lint)
36 static const char rcsid[] =
37   "$FreeBSD: src/lib/libc/gen/err.c,v 1.6 1999/08/27 23:58:33 peter Exp $";
38 #endif /* LIBC_RCS and not lint */
39
40 #include <err.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include <stdarg.h>
47
48 extern char *__progname;                /* Program name, from crt0. */
49
50 static FILE *err_file; /* file to use for error output */
51 static void (*err_exit)(int);
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  */
58 void
59 err_set_file(void *fp)
60 {
61         if (fp)
62                 err_file = fp;
63         else
64                 err_file = stderr;
65 }
66
67 void
68 err_set_exit(void (*ef)(int))
69 {
70         err_exit = ef;
71 }
72
73 void
74 err(int eval, const char *fmt, ...)
75 {
76         va_list ap;
77         va_start(ap, fmt);
78         verrc(eval, errno, fmt, ap);
79         va_end(ap);
80 }
81
82 void
83 verr(eval, fmt, ap)
84         int eval;
85         const char *fmt;
86         va_list ap;
87 {
88         verrc(eval, errno, fmt, ap);
89 }
90
91 void
92 errc(int eval, int code, const char *fmt, ...)
93 {
94         va_list ap;
95         va_start(ap, fmt);
96         verrc(eval, code, fmt, ap);
97         va_end(ap);
98 }
99
100 void
101 verrc(eval, code, fmt, ap)
102         int eval;
103         int code;
104         const char *fmt;
105         va_list ap;
106 {
107         if (err_file == 0)
108                 err_set_file((FILE *)0);
109         fprintf(err_file, "%s: ", __progname);
110         if (fmt != NULL) {
111                 vfprintf(err_file, fmt, ap);
112                 fprintf(err_file, ": ");
113         }
114         fprintf(err_file, "%s\n", strerror(code));
115         if (err_exit)
116                 err_exit(eval);
117         exit(eval);
118 }
119
120 void
121 errx(int eval, const char *fmt, ...)
122 {
123         va_list ap;
124         va_start(ap, fmt);
125         verrx(eval, fmt, ap);
126         va_end(ap);
127 }
128
129 void
130 verrx(eval, fmt, ap)
131         int eval;
132         const char *fmt;
133         va_list ap;
134 {
135         if (err_file == 0)
136                 err_set_file((FILE *)0);
137         fprintf(err_file, "%s: ", __progname);
138         if (fmt != NULL)
139                 vfprintf(err_file, fmt, ap);
140         fprintf(err_file, "\n");
141         if (err_exit)
142                 err_exit(eval);
143         exit(eval);
144 }
145
146 void
147 warn(const char *fmt, ...)
148 {
149         va_list ap;
150         va_start(ap, fmt);
151         vwarnc(errno, fmt, ap);
152         va_end(ap);
153 }
154
155 void
156 vwarn(fmt, ap)
157         const char *fmt;
158         va_list ap;
159 {
160         vwarnc(errno, fmt, ap);
161 }
162
163 void
164 warnc(int code, const char *fmt, ...)
165 {
166         va_list ap;
167         va_start(ap, fmt);
168         vwarnc(code, fmt, ap);
169         va_end(ap);
170 }
171
172 void
173 vwarnc(code, fmt, ap)
174         int code;
175         const char *fmt;
176         va_list ap;
177 {
178         if (err_file == 0)
179                 err_set_file((FILE *)0);
180         fprintf(err_file, "%s: ", __progname);
181         if (fmt != NULL) {
182                 vfprintf(err_file, fmt, ap);
183                 fprintf(err_file, ": ");
184         }
185         fprintf(err_file, "%s\n", strerror(code));
186 }
187
188 void
189 warnx(const char *fmt, ...)
190 {
191         va_list ap;
192         va_start(ap, fmt);
193         vwarnx(fmt, ap);
194         va_end(ap);
195 }
196
197 void
198 vwarnx(fmt, ap)
199         const char *fmt;
200         va_list ap;
201 {
202         if (err_file == 0)
203                 err_set_file((FILE *)0);
204         fprintf(err_file, "%s: ", __progname);
205         if (fmt != NULL)
206                 vfprintf(err_file, fmt, ap);
207         fprintf(err_file, "\n");
208 }