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