/*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#include <stdio.h>30#include <stdarg.h>31#include <stdlib.h>32#include <string.h>33#include <unistd.h>34#include <err.h>35#include <errno.h>3637/*38* 4.4BSD error printing functions.39*/4041/*42* This is initialized by crt0, though it actually lives in errno.c43*/44extern char **__argv;4546/*47* Routine to print error message text to stderr.48*/49static50void51__senderr(void *junk, const char *data, size_t len)52{53(void)junk; /* not needed or used */5455write(STDERR_FILENO, data, len);56}5758/*59* Shortcut to call __senderr on a null-terminated string.60* (__senderr is set up to be called by __vprintf.)61*/62static63void64__senderrstr(const char *str)65{66__senderr(NULL, str, strlen(str));67}6869/*70* Common routine for all the *err* and *warn* functions.71*/72static73void74__printerr(int use_errno, const char *fmt, va_list ap)75{76const char *errmsg;77const char *prog;7879/*80* Get the error message for the current errno.81* Do this early, before doing anything that might change the82* value in errno.83*/84errmsg = strerror(errno);8586/*87* Look up the program name.88* Strictly speaking we should pull off the rightmost89* path component of argv[0] and use that as the program90* name (this is how BSD err* prints) but it doesn't make91* much difference.92*/93if (__argv!=NULL && __argv[0]!=NULL) {94prog = __argv[0];95}96else {97prog = "(program name unknown)";98}99100/* print the program name */101__senderrstr(prog);102__senderrstr(": ");103104/* process the printf format and args */105__vprintf(__senderr, NULL, fmt, ap);106107/* if we're using errno, print the error string from above. */108if (use_errno) {109__senderrstr(": ");110__senderrstr(errmsg);111}112113/* and always add a newline. */114__senderrstr("\n");115}116117/*118* The va_list versions of the warn/err functions.119*/120121/* warn/vwarn: use errno, don't exit */122void123vwarn(const char *fmt, va_list ap)124{125__printerr(1, fmt, ap);126}127128/* warnx/vwarnx: don't use errno, don't exit */129void130vwarnx(const char *fmt, va_list ap)131{132__printerr(0, fmt, ap);133}134135/* err/verr: use errno, then exit */136void137verr(int exitcode, const char *fmt, va_list ap)138{139__printerr(1, fmt, ap);140exit(exitcode);141}142143/* errx/verrx: don't use errno, but do then exit */144void145verrx(int exitcode, const char *fmt, va_list ap)146{147__printerr(0, fmt, ap);148exit(exitcode);149}150151/*152* The non-va_list versions of the warn/err functions.153* Just hand off to the va_list versions.154*/155156void157warn(const char *fmt, ...)158{159va_list ap;160va_start(ap, fmt);161vwarn(fmt, ap);162va_end(ap);163}164165void166warnx(const char *fmt, ...)167{168va_list ap;169va_start(ap, fmt);170vwarnx(fmt, ap);171va_end(ap);172}173174void175err(int exitcode, const char *fmt, ...)176{177va_list ap;178va_start(ap, fmt);179verr(exitcode, fmt, ap);180va_end(ap);181}182183void184errx(int exitcode, const char *fmt, ...)185{186va_list ap;187va_start(ap, fmt);188verrx(exitcode, fmt, ap);189va_end(ap);190}191192193