158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1558f0484fSRodney W. Grimes * without specific prior written permission. 1658f0484fSRodney W. Grimes * 1758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2758f0484fSRodney W. Grimes * SUCH DAMAGE. 2858f0484fSRodney W. Grimes */ 2958f0484fSRodney W. Grimes 3026a2df73SDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint) 3149efc512SBruce Evans static char sccsid[] = "@(#)err.c 8.1 (Berkeley) 6/4/93"; 3226a2df73SDavid E. O'Brien #endif /* LIBC_SCCS and not lint */ 33ea8d448aSDavid E. O'Brien #include <sys/cdefs.h> 34ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$"); 3558f0484fSRodney W. Grimes 361643f03dSBruce Evans #include "namespace.h" 3758f0484fSRodney W. Grimes #include <err.h> 3858f0484fSRodney W. Grimes #include <errno.h> 394cd01193SMark Murray #include <stdarg.h> 4058f0484fSRodney W. Grimes #include <stdio.h> 4158f0484fSRodney W. Grimes #include <stdlib.h> 4258f0484fSRodney W. Grimes #include <string.h> 434cd01193SMark Murray #include "un-namespace.h" 4458f0484fSRodney W. Grimes 454cd01193SMark Murray #include "libc_private.h" 4658f0484fSRodney W. Grimes 473688be0eSGarrett Wollman static FILE *err_file; /* file to use for error output */ 483688be0eSGarrett Wollman static void (*err_exit)(int); 493688be0eSGarrett Wollman 50b4b4fb87SGarrett Wollman /* 51b4b4fb87SGarrett Wollman * This is declared to take a `void *' so that the caller is not required 52b4b4fb87SGarrett Wollman * to include <stdio.h> first. However, it is really a `FILE *', and the 53b4b4fb87SGarrett Wollman * manual page documents it as such. 54b4b4fb87SGarrett Wollman */ 553688be0eSGarrett Wollman void 563688be0eSGarrett Wollman err_set_file(void *fp) 573688be0eSGarrett Wollman { 583688be0eSGarrett Wollman if (fp) 593688be0eSGarrett Wollman err_file = fp; 603688be0eSGarrett Wollman else 613688be0eSGarrett Wollman err_file = stderr; 623688be0eSGarrett Wollman } 633688be0eSGarrett Wollman 643688be0eSGarrett Wollman void 653688be0eSGarrett Wollman err_set_exit(void (*ef)(int)) 663688be0eSGarrett Wollman { 673688be0eSGarrett Wollman err_exit = ef; 683688be0eSGarrett Wollman } 693688be0eSGarrett Wollman 70ac8e56a7SBruce Evans __weak_reference(_err, err); 71ac8e56a7SBruce Evans 72eaa86f9dSBruce Evans void 73ac8e56a7SBruce Evans _err(int eval, const char *fmt, ...) 7458f0484fSRodney W. Grimes { 7558f0484fSRodney W. Grimes va_list ap; 7658f0484fSRodney W. Grimes va_start(ap, fmt); 77b4b4fb87SGarrett Wollman verrc(eval, errno, fmt, ap); 7858f0484fSRodney W. Grimes va_end(ap); 7958f0484fSRodney W. Grimes } 8058f0484fSRodney W. Grimes 81eaa86f9dSBruce Evans void 8258f0484fSRodney W. Grimes verr(eval, fmt, ap) 8358f0484fSRodney W. Grimes int eval; 8458f0484fSRodney W. Grimes const char *fmt; 8558f0484fSRodney W. Grimes va_list ap; 8658f0484fSRodney W. Grimes { 87b4b4fb87SGarrett Wollman verrc(eval, errno, fmt, ap); 8858f0484fSRodney W. Grimes } 89b4b4fb87SGarrett Wollman 90b4b4fb87SGarrett Wollman void 91b4b4fb87SGarrett Wollman errc(int eval, int code, const char *fmt, ...) 92b4b4fb87SGarrett Wollman { 93b4b4fb87SGarrett Wollman va_list ap; 94b4b4fb87SGarrett Wollman va_start(ap, fmt); 95b4b4fb87SGarrett Wollman verrc(eval, code, fmt, ap); 96b4b4fb87SGarrett Wollman va_end(ap); 97b4b4fb87SGarrett Wollman } 98b4b4fb87SGarrett Wollman 99b4b4fb87SGarrett Wollman void 10022e5baf7SWarner Losh verrc(int eval, int code, const char *fmt, va_list ap) 101b4b4fb87SGarrett Wollman { 102b4b4fb87SGarrett Wollman if (err_file == 0) 103b4b4fb87SGarrett Wollman err_set_file((FILE *)0); 1044cd01193SMark Murray fprintf(err_file, "%s: ", _getprogname()); 105b4b4fb87SGarrett Wollman if (fmt != NULL) { 106b4b4fb87SGarrett Wollman vfprintf(err_file, fmt, ap); 107b4b4fb87SGarrett Wollman fprintf(err_file, ": "); 108b4b4fb87SGarrett Wollman } 109b4b4fb87SGarrett Wollman fprintf(err_file, "%s\n", strerror(code)); 1103688be0eSGarrett Wollman if (err_exit) 1113688be0eSGarrett Wollman err_exit(eval); 11258f0484fSRodney W. Grimes exit(eval); 11358f0484fSRodney W. Grimes } 11458f0484fSRodney W. Grimes 115eaa86f9dSBruce Evans void 11658f0484fSRodney W. Grimes errx(int eval, const char *fmt, ...) 11758f0484fSRodney W. Grimes { 11858f0484fSRodney W. Grimes va_list ap; 11958f0484fSRodney W. Grimes va_start(ap, fmt); 12058f0484fSRodney W. Grimes verrx(eval, fmt, ap); 12158f0484fSRodney W. Grimes va_end(ap); 12258f0484fSRodney W. Grimes } 12358f0484fSRodney W. Grimes 124eaa86f9dSBruce Evans void 12522e5baf7SWarner Losh verrx(int eval, const char *fmt, va_list ap) 12658f0484fSRodney W. Grimes { 127b4b4fb87SGarrett Wollman if (err_file == 0) 1283688be0eSGarrett Wollman err_set_file((FILE *)0); 1294cd01193SMark Murray fprintf(err_file, "%s: ", _getprogname()); 13058f0484fSRodney W. Grimes if (fmt != NULL) 131b4b4fb87SGarrett Wollman vfprintf(err_file, fmt, ap); 132b4b4fb87SGarrett Wollman fprintf(err_file, "\n"); 1333688be0eSGarrett Wollman if (err_exit) 1343688be0eSGarrett Wollman err_exit(eval); 13558f0484fSRodney W. Grimes exit(eval); 13658f0484fSRodney W. Grimes } 13758f0484fSRodney W. Grimes 1381643f03dSBruce Evans __weak_reference(_warn, warn); 1391643f03dSBruce Evans 14058f0484fSRodney W. Grimes void 1411643f03dSBruce Evans _warn(const char *fmt, ...) 14258f0484fSRodney W. Grimes { 14358f0484fSRodney W. Grimes va_list ap; 14458f0484fSRodney W. Grimes va_start(ap, fmt); 145b4b4fb87SGarrett Wollman vwarnc(errno, fmt, ap); 14658f0484fSRodney W. Grimes va_end(ap); 14758f0484fSRodney W. Grimes } 14858f0484fSRodney W. Grimes 14958f0484fSRodney W. Grimes void 15022e5baf7SWarner Losh vwarn(const char *fmt, va_list ap) 15158f0484fSRodney W. Grimes { 152b4b4fb87SGarrett Wollman vwarnc(errno, fmt, ap); 15358f0484fSRodney W. Grimes } 15458f0484fSRodney W. Grimes 15558f0484fSRodney W. Grimes void 156b4b4fb87SGarrett Wollman warnc(int code, const char *fmt, ...) 15758f0484fSRodney W. Grimes { 15858f0484fSRodney W. Grimes va_list ap; 15958f0484fSRodney W. Grimes va_start(ap, fmt); 160b4b4fb87SGarrett Wollman vwarnc(code, fmt, ap); 161b4b4fb87SGarrett Wollman va_end(ap); 162b4b4fb87SGarrett Wollman } 163b4b4fb87SGarrett Wollman 164b4b4fb87SGarrett Wollman void 16522e5baf7SWarner Losh vwarnc(int code, const char *fmt, va_list ap) 166b4b4fb87SGarrett Wollman { 167b4b4fb87SGarrett Wollman if (err_file == 0) 168b4b4fb87SGarrett Wollman err_set_file((FILE *)0); 1694cd01193SMark Murray fprintf(err_file, "%s: ", _getprogname()); 170b4b4fb87SGarrett Wollman if (fmt != NULL) { 171b4b4fb87SGarrett Wollman vfprintf(err_file, fmt, ap); 172b4b4fb87SGarrett Wollman fprintf(err_file, ": "); 173b4b4fb87SGarrett Wollman } 174b4b4fb87SGarrett Wollman fprintf(err_file, "%s\n", strerror(code)); 175b4b4fb87SGarrett Wollman } 176b4b4fb87SGarrett Wollman 177b4b4fb87SGarrett Wollman void 178b4b4fb87SGarrett Wollman warnx(const char *fmt, ...) 179b4b4fb87SGarrett Wollman { 180b4b4fb87SGarrett Wollman va_list ap; 181b4b4fb87SGarrett Wollman va_start(ap, fmt); 18275a21a38SGarrett Wollman vwarnx(fmt, ap); 18358f0484fSRodney W. Grimes va_end(ap); 18458f0484fSRodney W. Grimes } 18558f0484fSRodney W. Grimes 18658f0484fSRodney W. Grimes void 18722e5baf7SWarner Losh vwarnx(const char *fmt, va_list ap) 18858f0484fSRodney W. Grimes { 189b4b4fb87SGarrett Wollman if (err_file == 0) 1903688be0eSGarrett Wollman err_set_file((FILE *)0); 1914cd01193SMark Murray fprintf(err_file, "%s: ", _getprogname()); 19258f0484fSRodney W. Grimes if (fmt != NULL) 193b4b4fb87SGarrett Wollman vfprintf(err_file, fmt, ap); 194b4b4fb87SGarrett Wollman fprintf(err_file, "\n"); 19558f0484fSRodney W. Grimes } 196