]> git.wh0rd.org - dump.git/blob - compat/lib/compaterr.c
Relicensed dump/restore under the revised BSD license, as per ftp://ftp.cs.berkeley...
[dump.git] / compat / lib / compaterr.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*-
10 * Copyright (c) 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #ifndef lint
39 static const char rcsid[] =
40 "$Id: compaterr.c,v 1.10 2003/03/30 15:40:35 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <compaterr.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdarg.h>
50
51 extern char *__progname; /* Program name, from crt0. */
52
53 #if !defined(HAVE_ERR) || !defined(HAVE_ERRX) || !defined(HAVE_VERR) || !defined(HAVE_VERRX) || !defined(HAVE_VWARN) || !defined(HAVE_VWARNX) || !defined(HAVE_WARN) || !defined(HAVE_WARNX)
54
55 __BEGIN_DECLS
56 __dead void errc __P((int, int, const char *, ...));
57 __dead void verrc __P((int, int, const char *, _BSD_VA_LIST_));
58 void warnc __P((int, const char *, ...));
59 void vwarnc __P((int, const char *, _BSD_VA_LIST_));
60 void err_set_file __P((void *));
61 void err_set_exit __P((void (*)(int)));
62 __END_DECLS
63
64 static void (*err_exit)(int);
65
66 static FILE *err_file; /* file to use for error output */
67 /*
68 * This is declared to take a `void *' so that the caller is not required
69 * to include <stdio.h> first. However, it is really a `FILE *', and the
70 * manual page documents it as such.
71 */
72 void
73 err_set_file(void *fp)
74 {
75 if (fp)
76 err_file = fp;
77 else
78 err_file = stderr;
79 }
80
81 void
82 err_set_exit(void (*ef)(int))
83 {
84 err_exit = ef;
85 }
86
87 __dead void
88 errc(int eval, int code, const char *fmt, ...)
89 {
90 va_list ap;
91 va_start(ap, fmt);
92 verrc(eval, code, fmt, ap);
93 va_end(ap);
94 }
95
96 __dead void
97 verrc(int eval, int code, const char *fmt, va_list ap)
98 {
99 if (err_file == 0)
100 err_set_file((FILE *)0);
101 fprintf(err_file, "%s: ", __progname);
102 if (fmt != NULL) {
103 vfprintf(err_file, fmt, ap);
104 fprintf(err_file, ": ");
105 }
106 fprintf(err_file, "%s\n", strerror(code));
107 if (err_exit)
108 err_exit(eval);
109 exit(eval);
110 }
111
112 void
113 warnc(int code, const char *fmt, ...)
114 {
115 va_list ap;
116 va_start(ap, fmt);
117 vwarnc(code, fmt, ap);
118 va_end(ap);
119 }
120
121 void
122 vwarnc(int code, const char *fmt, va_list ap)
123 {
124 if (err_file == 0)
125 err_set_file((FILE *)0);
126 fprintf(err_file, "%s: ", __progname);
127 if (fmt != NULL) {
128 vfprintf(err_file, fmt, ap);
129 fprintf(err_file, ": ");
130 }
131 fprintf(err_file, "%s\n", strerror(code));
132 }
133 #endif
134
135 #ifndef HAVE_ERR
136 __dead void
137 err(int eval, const char *fmt, ...)
138 {
139 va_list ap;
140 va_start(ap, fmt);
141 verrc(eval, errno, fmt, ap);
142 va_end(ap);
143 }
144 #endif
145
146 #ifndef HAVE_VERR
147 __dead void
148 verr(int eval, const char *fmt, va_list ap)
149 {
150 verrc(eval, errno, fmt, ap);
151 }
152 #endif
153
154 #ifndef HAVE_ERRX
155 __dead void
156 errx(int eval, const char *fmt, ...)
157 {
158 va_list ap;
159 va_start(ap, fmt);
160 verrx(eval, fmt, ap);
161 va_end(ap);
162 }
163 #endif
164
165 #ifndef HAVE_VERRX
166 __dead void
167 verrx(int eval, const char *fmt, va_list ap)
168 {
169 if (err_file == 0)
170 err_set_file((FILE *)0);
171 fprintf(err_file, "%s: ", __progname);
172 if (fmt != NULL)
173 vfprintf(err_file, fmt, ap);
174 fprintf(err_file, "\n");
175 if (err_exit)
176 err_exit(eval);
177 exit(eval);
178 }
179 #endif
180
181 #ifndef HAVE_WARN
182 void
183 warn(const char *fmt, ...)
184 {
185 va_list ap;
186 va_start(ap, fmt);
187 vwarnc(errno, fmt, ap);
188 va_end(ap);
189 }
190 #endif
191
192 #ifndef HAVE_VWARN
193 void
194 vwarn(const char *fmt, va_list ap)
195 {
196 vwarnc(errno, fmt, ap);
197 }
198 #endif
199
200 #ifndef HAVE_WARNX
201 void
202 warnx(const char *fmt, ...)
203 {
204 va_list ap;
205 va_start(ap, fmt);
206 vwarnx(fmt, ap);
207 va_end(ap);
208 }
209 #endif
210
211 #ifndef HAVE_VWARNX
212 void
213 vwarnx(const char *fmt, va_list ap)
214 {
215 if (err_file == 0)
216 err_set_file((FILE *)0);
217 fprintf(err_file, "%s: ", __progname);
218 if (fmt != NULL)
219 vfprintf(err_file, fmt, ap);
220 fprintf(err_file, "\n");
221 }
222 #endif