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 void 76 err(int eval, const char *fmt, ...) 77 { 78 va_list ap; 79 va_start(ap, fmt); 80 verrc(eval, errno, fmt, ap); 81 va_end(ap); 82 } 83 84 void 85 verr(eval, fmt, ap) 86 int eval; 87 const char *fmt; 88 va_list ap; 89 { 90 verrc(eval, errno, fmt, ap); 91 } 92 93 void 94 errc(int eval, int code, const char *fmt, ...) 95 { 96 va_list ap; 97 va_start(ap, fmt); 98 verrc(eval, code, fmt, ap); 99 va_end(ap); 100 } 101 102 void 103 verrc(eval, code, fmt, ap) 104 int eval; 105 int code; 106 const char *fmt; 107 va_list ap; 108 { 109 if (err_file == 0) 110 err_set_file((FILE *)0); 111 fprintf(err_file, "%s: ", __progname); 112 if (fmt != NULL) { 113 vfprintf(err_file, fmt, ap); 114 fprintf(err_file, ": "); 115 } 116 fprintf(err_file, "%s\n", strerror(code)); 117 if (err_exit) 118 err_exit(eval); 119 exit(eval); 120 } 121 122 void 123 errx(int eval, const char *fmt, ...) 124 { 125 va_list ap; 126 va_start(ap, fmt); 127 verrx(eval, fmt, ap); 128 va_end(ap); 129 } 130 131 void 132 verrx(eval, fmt, ap) 133 int eval; 134 const char *fmt; 135 va_list ap; 136 { 137 if (err_file == 0) 138 err_set_file((FILE *)0); 139 fprintf(err_file, "%s: ", __progname); 140 if (fmt != NULL) 141 vfprintf(err_file, fmt, ap); 142 fprintf(err_file, "\n"); 143 if (err_exit) 144 err_exit(eval); 145 exit(eval); 146 } 147 148 __weak_reference(_warn, warn); 149 150 void 151 _warn(const char *fmt, ...) 152 { 153 va_list ap; 154 va_start(ap, fmt); 155 vwarnc(errno, fmt, ap); 156 va_end(ap); 157 } 158 159 void 160 vwarn(fmt, ap) 161 const char *fmt; 162 va_list ap; 163 { 164 vwarnc(errno, fmt, ap); 165 } 166 167 void 168 warnc(int code, const char *fmt, ...) 169 { 170 va_list ap; 171 va_start(ap, fmt); 172 vwarnc(code, fmt, ap); 173 va_end(ap); 174 } 175 176 void 177 vwarnc(code, fmt, ap) 178 int code; 179 const char *fmt; 180 va_list ap; 181 { 182 if (err_file == 0) 183 err_set_file((FILE *)0); 184 fprintf(err_file, "%s: ", __progname); 185 if (fmt != NULL) { 186 vfprintf(err_file, fmt, ap); 187 fprintf(err_file, ": "); 188 } 189 fprintf(err_file, "%s\n", strerror(code)); 190 } 191 192 void 193 warnx(const char *fmt, ...) 194 { 195 va_list ap; 196 va_start(ap, fmt); 197 vwarnx(fmt, ap); 198 va_end(ap); 199 } 200 201 void 202 vwarnx(fmt, ap) 203 const char *fmt; 204 va_list ap; 205 { 206 if (err_file == 0) 207 err_set_file((FILE *)0); 208 fprintf(err_file, "%s: ", __progname); 209 if (fmt != NULL) 210 vfprintf(err_file, fmt, ap); 211 fprintf(err_file, "\n"); 212 } 213