Imported Upstream version 0.4b37
[debian/dump] / 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.11 2003/10/26 16:05:46 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <sys/types.h>
45 #include <compaterr.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdarg.h>
51
52 extern char *__progname;                /* Program name, from crt0. */
53
54 #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)
55
56 __BEGIN_DECLS
57 __dead void     errc __P((int, int, const char *, ...));
58 __dead void     verrc __P((int, int, const char *, _BSD_VA_LIST_));
59 void            warnc __P((int, const char *, ...));
60 void            vwarnc __P((int, const char *, _BSD_VA_LIST_));
61 void            err_set_file __P((void *));
62 void            err_set_exit __P((void (*)(int)));
63 __END_DECLS
64
65 static void (*err_exit)(int);
66
67 static FILE *err_file; /* file to use for error output */
68 /*
69  * This is declared to take a `void *' so that the caller is not required
70  * to include <stdio.h> first.  However, it is really a `FILE *', and the
71  * manual page documents it as such.
72  */
73 void
74 err_set_file(void *fp)
75 {
76         if (fp)
77                 err_file = fp;
78         else
79                 err_file = stderr;
80 }
81
82 void
83 err_set_exit(void (*ef)(int))
84 {
85         err_exit = ef;
86 }
87
88 __dead 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 __dead void
98 verrc(int eval, int code, const char *fmt, va_list ap)
99 {
100         if (err_file == 0)
101                 err_set_file((FILE *)0);
102         fprintf(err_file, "%s: ", __progname);
103         if (fmt != NULL) {
104                 vfprintf(err_file, fmt, ap);
105                 fprintf(err_file, ": ");
106         }
107         fprintf(err_file, "%s\n", strerror(code));
108         if (err_exit)
109                 err_exit(eval);
110         exit(eval);
111 }
112
113 void
114 warnc(int code, const char *fmt, ...)
115 {
116         va_list ap;
117         va_start(ap, fmt);
118         vwarnc(code, fmt, ap);
119         va_end(ap);
120 }
121
122 void
123 vwarnc(int code, const char *fmt, va_list ap)
124 {
125         if (err_file == 0)
126                 err_set_file((FILE *)0);
127         fprintf(err_file, "%s: ", __progname);
128         if (fmt != NULL) {
129                 vfprintf(err_file, fmt, ap);
130                 fprintf(err_file, ": ");
131         }
132         fprintf(err_file, "%s\n", strerror(code));
133 }
134 #endif
135
136 #ifndef HAVE_ERR
137 __dead void
138 err(int eval, const char *fmt, ...)
139 {
140         va_list ap;
141         va_start(ap, fmt);
142         verrc(eval, errno, fmt, ap);
143         va_end(ap);
144 }
145 #endif
146
147 #ifndef HAVE_VERR
148 __dead void
149 verr(int eval, const char *fmt, va_list ap)
150 {
151         verrc(eval, errno, fmt, ap);
152 }
153 #endif
154
155 #ifndef HAVE_ERRX
156 __dead void
157 errx(int eval, const char *fmt, ...)
158 {
159         va_list ap;
160         va_start(ap, fmt);
161         verrx(eval, fmt, ap);
162         va_end(ap);
163 }
164 #endif
165
166 #ifndef HAVE_VERRX
167 __dead void
168 verrx(int eval, const char *fmt, va_list ap)
169 {
170         if (err_file == 0)
171                 err_set_file((FILE *)0);
172         fprintf(err_file, "%s: ", __progname);
173         if (fmt != NULL)
174                 vfprintf(err_file, fmt, ap);
175         fprintf(err_file, "\n");
176         if (err_exit)
177                 err_exit(eval);
178         exit(eval);
179 }
180 #endif
181
182 #ifndef HAVE_WARN
183 void
184 warn(const char *fmt, ...)
185 {
186         va_list ap;
187         va_start(ap, fmt);
188         vwarnc(errno, fmt, ap);
189         va_end(ap);
190 }
191 #endif
192
193 #ifndef HAVE_VWARN
194 void
195 vwarn(const char *fmt, va_list ap)
196 {
197         vwarnc(errno, fmt, ap);
198 }
199 #endif
200
201 #ifndef HAVE_WARNX
202 void
203 warnx(const char *fmt, ...)
204 {
205         va_list ap;
206         va_start(ap, fmt);
207         vwarnx(fmt, ap);
208         va_end(ap);
209 }
210 #endif
211
212 #ifndef HAVE_VWARNX
213 void
214 vwarnx(const char *fmt, va_list ap)
215 {
216         if (err_file == 0)
217                 err_set_file((FILE *)0);
218         fprintf(err_file, "%s: ", __progname);
219         if (fmt != NULL)
220                 vfprintf(err_file, fmt, ap);
221         fprintf(err_file, "\n");
222 }
223 #endif