/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* 4.4BSD error printing functions.31*/3233#include <unistd.h>34#include <stdio.h>35#include <errno.h>36#include <string.h>3738#include "host-err.h"3940#ifdef NEED_ERR4142/*43* This is initialized by hostcompat_init44*/45extern const char *hostcompat_progname;4647/*48* Common routine for all the *err* and *warn* functions.49*/50static51void52hostcompat_printerr(int use_errno, const char *fmt, va_list ap)53{54const char *errmsg;5556/*57* Get the error message for the current errno.58* Do this early, before doing anything that might change the59* value in errno.60*/61errmsg = strerror(errno);6263/*64* Look up the program name.65* Strictly speaking we should pull off the rightmost66* path component of argv[0] and use that as the program67* name (this is how BSD err* prints) but it doesn't make68* much difference.69*/70if (hostcompat_progname != NULL) {71fprintf(stderr, "%s: ", hostcompat_progname);72}73else {74fprintf(stderr, "libhostcompat: hostcompat_init not called\n");75fprintf(stderr, "libhostcompat-program: ");76}7778/* process the printf format and args */79vfprintf(stderr, fmt, ap);8081if (use_errno) {82/* if we're using errno, print the error string from above. */83fprintf(stderr, ": %s\n", errmsg);84}85else {86/* otherwise, just a newline. */87fprintf(stderr, "\n");88}89}9091/*92* The va_list versions of the warn/err functions.93*/9495/* warn/vwarn: use errno, don't exit */96void97vwarn(const char *fmt, va_list ap)98{99hostcompat_printerr(1, fmt, ap);100}101102/* warnx/vwarnx: don't use errno, don't exit */103void104vwarnx(const char *fmt, va_list ap)105{106hostcompat_printerr(0, fmt, ap);107}108109/* err/verr: use errno, then exit */110void111verr(int exitcode, const char *fmt, va_list ap)112{113hostcompat_printerr(1, fmt, ap);114exit(exitcode);115}116117/* errx/verrx: don't use errno, but do then exit */118void119verrx(int exitcode, const char *fmt, va_list ap)120{121hostcompat_printerr(0, fmt, ap);122exit(exitcode);123}124125/*126* The non-va_list versions of the warn/err functions.127* Just hand off to the va_list versions.128*/129130void131warn(const char *fmt, ...)132{133va_list ap;134va_start(ap, fmt);135vwarn(fmt, ap);136va_end(ap);137}138139void140warnx(const char *fmt, ...)141{142va_list ap;143va_start(ap, fmt);144vwarnx(fmt, ap);145va_end(ap);146}147148void149err(int exitcode, const char *fmt, ...)150{151va_list ap;152va_start(ap, fmt);153verr(exitcode, fmt, ap);154va_end(ap);155}156157void158errx(int exitcode, const char *fmt, ...)159{160va_list ap;161va_start(ap, fmt);162verrx(exitcode, fmt, ap);163va_end(ap);164}165166#endif /* NEED_ERR */167168169