os161-1.99
 All Data Structures
err.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * 4.4BSD error printing functions.
00032  */
00033 
00034 #include <unistd.h>
00035 #include <stdio.h>
00036 #include <errno.h>
00037 #include <string.h>
00038 
00039 #include "host-err.h"
00040 
00041 #ifdef NEED_ERR
00042 
00043 /*
00044  * This is initialized by hostcompat_init
00045  */
00046 extern const char *hostcompat_progname;
00047 
00048 /*
00049  * Common routine for all the *err* and *warn* functions.
00050  */
00051 static
00052 void
00053 hostcompat_printerr(int use_errno, const char *fmt, va_list ap)
00054 {
00055         const char *errmsg;
00056 
00057         /*
00058          * Get the error message for the current errno.
00059          * Do this early, before doing anything that might change the
00060          * value in errno.
00061          */
00062         errmsg = strerror(errno);
00063 
00064         /*
00065          * Look up the program name.
00066          * Strictly speaking we should pull off the rightmost
00067          * path component of argv[0] and use that as the program
00068          * name (this is how BSD err* prints) but it doesn't make
00069          * much difference.
00070          */
00071         if (hostcompat_progname != NULL) {
00072                 fprintf(stderr, "%s: ", hostcompat_progname);
00073         }
00074         else {
00075                 fprintf(stderr, "libhostcompat: hostcompat_init not called\n");
00076                 fprintf(stderr, "libhostcompat-program: ");
00077         }
00078 
00079         /* process the printf format and args */
00080         vfprintf(stderr, fmt, ap);
00081 
00082         if (use_errno) {
00083                 /* if we're using errno, print the error string from above. */
00084                 fprintf(stderr, ": %s\n", errmsg);
00085         }
00086         else {
00087                 /* otherwise, just a newline. */
00088                 fprintf(stderr, "\n");
00089         }
00090 }
00091 
00092 /*
00093  * The va_list versions of the warn/err functions.
00094  */
00095 
00096 /* warn/vwarn: use errno, don't exit */
00097 void
00098 vwarn(const char *fmt, va_list ap)
00099 {
00100         hostcompat_printerr(1, fmt, ap);
00101 }
00102 
00103 /* warnx/vwarnx: don't use errno, don't exit */
00104 void
00105 vwarnx(const char *fmt, va_list ap)
00106 {
00107         hostcompat_printerr(0, fmt, ap);
00108 }
00109 
00110 /* err/verr: use errno, then exit */
00111 void
00112 verr(int exitcode, const char *fmt, va_list ap)
00113 {
00114         hostcompat_printerr(1, fmt, ap);
00115         exit(exitcode);
00116 }
00117 
00118 /* errx/verrx: don't use errno, but do then exit */
00119 void
00120 verrx(int exitcode, const char *fmt, va_list ap)
00121 {
00122         hostcompat_printerr(0, fmt, ap);
00123         exit(exitcode);
00124 }
00125 
00126 /*
00127  * The non-va_list versions of the warn/err functions.
00128  * Just hand off to the va_list versions.
00129  */
00130 
00131 void
00132 warn(const char *fmt, ...)
00133 {
00134         va_list ap;
00135         va_start(ap, fmt);
00136         vwarn(fmt, ap);
00137         va_end(ap);
00138 }
00139 
00140 void
00141 warnx(const char *fmt, ...)
00142 {
00143         va_list ap;
00144         va_start(ap, fmt);
00145         vwarnx(fmt, ap);
00146         va_end(ap);
00147 }
00148 
00149 void
00150 err(int exitcode, const char *fmt, ...)
00151 {
00152         va_list ap;
00153         va_start(ap, fmt);
00154         verr(exitcode, fmt, ap);
00155         va_end(ap);
00156 }
00157 
00158 void
00159 errx(int exitcode, const char *fmt, ...)
00160 {
00161         va_list ap;
00162         va_start(ap, fmt);
00163         verrx(exitcode, fmt, ap);
00164         va_end(ap);
00165 }
00166 
00167 #endif /* NEED_ERR */
 All Data Structures