1/*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 33#include <sys/cdefs.h> 34#include <err.h> 35#include <errno.h> 36#include <stdarg.h> 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40 41//#include "libc_private.h" 42 43extern const char *_getprogname(void); 44 45static FILE *err_file; /* file to use for error output */ 46static void (*err_exit)(int); 47 48void _err(int eval, const char *fmt, ...); 49void _warn(const char *fmt, ...); 50 51/* 52 * This is declared to take a `void *' so that the caller is not required 53 * to include <stdio.h> first. However, it is really a `FILE *', and the 54 * manual page documents it as such. 55 */ 56void 57err_set_file(void *fp) 58{ 59 if (fp) 60 err_file = fp; 61 else 62 err_file = stderr; 63} 64 65void 66err_set_exit(void (*ef)(int)) 67{ 68 err_exit = ef; 69} 70 71__weak_reference(_err, err); 72 73void 74_err(int eval, const char *fmt, ...) 75{ 76 va_list ap; 77 va_start(ap, fmt); 78 verrc(eval, errno, fmt, ap); 79 va_end(ap); 80} 81 82void 83verr(int eval, const char *fmt, va_list ap) 84{ 85 verrc(eval, errno, fmt, ap); 86} 87 88void 89errc(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 97void 98verrc(int eval, int code, const char *fmt, va_list ap) 99{ 100 if (err_file == NULL) 101 err_set_file(NULL); 102 fprintf(err_file, "%s: ", _getprogname()); 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 113void 114errx(int eval, const char *fmt, ...) 115{ 116 va_list ap; 117 va_start(ap, fmt); 118 verrx(eval, fmt, ap); 119 va_end(ap); 120} 121 122void 123verrx(int eval, const char *fmt, va_list ap) 124{ 125 if (err_file == NULL) 126 err_set_file(NULL); 127 fprintf(err_file, "%s: ", _getprogname()); 128 if (fmt != NULL) 129 vfprintf(err_file, fmt, ap); 130 fprintf(err_file, "\n"); 131 if (err_exit) 132 err_exit(eval); 133 exit(eval); 134} 135 136__weak_reference(_warn, warn); 137 138void 139_warn(const char *fmt, ...) 140{ 141 va_list ap; 142 va_start(ap, fmt); 143 vwarnc(errno, fmt, ap); 144 va_end(ap); 145} 146 147void 148vwarn(const char *fmt, va_list ap) 149{ 150 vwarnc(errno, fmt, ap); 151} 152 153void 154warnc(int code, const char *fmt, ...) 155{ 156 va_list ap; 157 va_start(ap, fmt); 158 vwarnc(code, fmt, ap); 159 va_end(ap); 160} 161 162void 163vwarnc(int code, const char *fmt, va_list ap) 164{ 165 int saved_errno; 166 167 saved_errno = errno; 168 if (err_file == NULL) 169 err_set_file(NULL); 170 fprintf(err_file, "%s: ", _getprogname()); 171 if (fmt != NULL) { 172 vfprintf(err_file, fmt, ap); 173 fprintf(err_file, ": "); 174 } 175 fprintf(err_file, "%s\n", strerror(code)); 176 errno = saved_errno; 177} 178 179void 180warnx(const char *fmt, ...) 181{ 182 va_list ap; 183 va_start(ap, fmt); 184 vwarnx(fmt, ap); 185 va_end(ap); 186} 187 188void 189vwarnx(const char *fmt, va_list ap) 190{ 191 int saved_errno; 192 193 saved_errno = errno; 194 if (err_file == NULL) 195 err_set_file(NULL); 196 fprintf(err_file, "%s: ", _getprogname()); 197 if (fmt != NULL) 198 vfprintf(err_file, fmt, ap); 199 fprintf(err_file, "\n"); 200 errno = saved_errno; 201} 202