Debian Codegraph @

compat/lib/compaterr.c

raw symbols SCIP
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - 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 #include "config.h" // IWYU pragma: keep
39 #include "compat/include/compaterr.h"
40
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <stdarg.h>
46
47 extern char *__progname; /* Program name, from crt0. */
48
49 #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)
50
51 void errc (int, int, const char *, ...)
52 __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4)));
53 void verrc (int, int, const char *, va_list)
54 __attribute__ ((__noreturn__, __format__ (__printf__, 3, 0)));
55 void warnc (int, const char *, ...)
56 __attribute__ ((__format__ (__printf__, 2, 3)));
57 void vwarnc (int, const char *, va_list)
58 __attribute__ ((__format__ (__printf__, 2, 0)));
59 void err_set_file (void *);
60 void err_set_exit (void (*)(int));
61
62 void (*err_exit)(int);
63
64 static FILE *err_file; /* file to use for error output */
65 /*
66 * This is declared to take a `void *' so that the caller is not required
67 * to include <stdio.h> first. However, it is really a `FILE *', and the
68 * manual page documents it as such.
69 */
70 void
71 err_set_file(void *fp)
72 {
73 if (fp)
74 err_file = (FILE*)fp;
75 else
76 err_file = stderr;
77 }
78
79 void
80 err_set_exit(void (*ef)(int))
81 {
82 err_exit = ef;
83 }
84
85 void
86 errc(int eval, int code, const char *fmt, ...)
87 {
88 va_list ap;
89 va_start(ap, fmt);
90 verrc(eval, code, fmt, ap);
91 va_end(ap);
92 }
93
94 void
95 verrc(int eval, int code, const char *fmt, va_list ap)
96 {
97 if (err_file == 0)
98 err_set_file(NULL);
99 fprintf(err_file, "%s: ", __progname);
100 if (fmt != NULL) {
101 vfprintf(err_file, fmt, ap);
102 fprintf(err_file, ": ");
103 }
104 fprintf(err_file, "%s\n", strerror(code));
105 if (err_exit)
106 err_exit(eval);
107 exit(eval);
108 }
109
110 void
111 warnc(int code, const char *fmt, ...)
112 {
113 va_list ap;
114 va_start(ap, fmt);
115 vwarnc(code, fmt, ap);
116 va_end(ap);
117 }
118
119 void
120 vwarnc(int code, const char *fmt, va_list ap)
121 {
122 if (err_file == 0)
123 err_set_file(NULL);
124 fprintf(err_file, "%s: ", __progname);
125 if (fmt != NULL) {
126 vfprintf(err_file, fmt, ap);
127 fprintf(err_file, ": ");
128 }
129 fprintf(err_file, "%s\n", strerror(code));
130 }
131 #endif
132
133 #if !defined(HAVE_ERR)
134 void
135 err(int eval, const char *fmt, ...)
136 {
137 va_list ap;
138 va_start(ap, fmt);
139 verrc(eval, errno, fmt, ap);
140 va_end(ap);
141 }
142 #endif
143
144 #if !defined(HAVE_VERR)
145 void
146 verr(int eval, const char *fmt, va_list ap)
147 {
148 verrc(eval, errno, fmt, ap);
149 }
150 #endif
151
152 #if !defined(HAVE_ERRX)
153 void
154 errx(int eval, const char *fmt, ...)
155 {
156 va_list ap;
157 va_start(ap, fmt);
158 verrx(eval, fmt, ap);
159 va_end(ap);
160 }
161 #endif
162
163 #if !defined(HAVE_VERRX)
164 void
165 verrx(int eval, const char *fmt, va_list ap)
166 {
167 if (err_file == 0)
168 err_set_file(NULL);
169 fprintf(err_file, "%s: ", __progname);
170 if (fmt != NULL)
171 vfprintf(err_file, fmt, ap);
172 fprintf(err_file, "\n");
173 if (err_exit)
174 err_exit(eval);
175 exit(eval);
176 }
177 #endif
178
179 #if !defined(HAVE_WARN)
180 void
181 warn(const char *fmt, ...)
182 {
183 va_list ap;
184 va_start(ap, fmt);
185 vwarnc(errno, fmt, ap);
186 va_end(ap);
187 }
188 #endif
189
190 #if !defined(HAVE_VWARN)
191 void
192 vwarn(const char *fmt, va_list ap)
193 {
194 vwarnc(errno, fmt, ap);
195 }
196 #endif
197
198 #if !defined(HAVE_WARNX)
199 void
200 warnx(const char *fmt, ...)
201 {
202 va_list ap;
203 va_start(ap, fmt);
204 vwarnx(fmt, ap);
205 va_end(ap);
206 }
207 #endif
208
209 #if !defined(HAVE_VWARNX)
210 void
211 vwarnx(const char *fmt, va_list ap)
212 {
213 if (err_file == 0)
214 err_set_file(NULL);
215 fprintf(err_file, "%s: ", __progname);
216 if (fmt != NULL)
217 vfprintf(err_file, fmt, ap);
218 fprintf(err_file, "\n");
219 }
220 #endif