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