root/user/lib/libc/unix/err.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __senderr
  2. __senderrstr
  3. __printerr
  4. vwarn
  5. vwarnx
  6. verr
  7. verrx
  8. warn
  9. warnx
  10. err
  11. errx

   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdarg.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 #include <unistd.h>
  35 #include <err.h>
  36 #include <errno.h>
  37 
  38 /*
  39  * 4.4BSD error printing functions.
  40  */
  41 
  42 /*
  43  * This is initialized by crt0, though it actually lives in errno.c
  44  */
  45 extern char **__argv;
  46 
  47 /*
  48  * Routine to print error message text to stderr.
  49  */
  50 static
  51 void
  52 __senderr(void *junk, const char *data, size_t len)
  53 {
  54         (void)junk;  /* not needed or used */
  55 
  56         write(STDERR_FILENO, data, len);
  57 }
  58 
  59 /*
  60  * Shortcut to call __senderr on a null-terminated string.
  61  * (__senderr is set up to be called by __vprintf.)
  62  */
  63 static
  64 void
  65 __senderrstr(const char *str)
  66 {
  67         __senderr(NULL, str, strlen(str));
  68 }
  69 
  70 /*
  71  * Common routine for all the *err* and *warn* functions.
  72  */
  73 static
  74 void
  75 __printerr(int use_errno, const char *fmt, va_list ap)
  76 {
  77         const char *errmsg;
  78         const char *prog;
  79 
  80         /*
  81          * Get the error message for the current errno.
  82          * Do this early, before doing anything that might change the
  83          * value in errno.
  84          */
  85         errmsg = strerror(errno);
  86 
  87         /*
  88          * Look up the program name.
  89          * Strictly speaking we should pull off the rightmost
  90          * path component of argv[0] and use that as the program
  91          * name (this is how BSD err* prints) but it doesn't make
  92          * much difference.
  93          */
  94         if (__argv!=NULL && __argv[0]!=NULL) {
  95                 prog = __argv[0];
  96         }
  97         else {
  98                 prog = "(program name unknown)";
  99         }
 100 
 101         /* print the program name */
 102         __senderrstr(prog);
 103         __senderrstr(": ");
 104 
 105         /* process the printf format and args */
 106         __vprintf(__senderr, NULL, fmt, ap);
 107 
 108         /* if we're using errno, print the error string from above. */
 109         if (use_errno) {
 110                 __senderrstr(": ");
 111                 __senderrstr(errmsg);
 112         }
 113 
 114         /* and always add a newline. */
 115         __senderrstr("\n");
 116 }
 117 
 118 /*
 119  * The va_list versions of the warn/err functions.
 120  */
 121 
 122 /* warn/vwarn: use errno, don't exit */
 123 void
 124 vwarn(const char *fmt, va_list ap)
 125 {
 126         __printerr(1, fmt, ap);
 127 }
 128 
 129 /* warnx/vwarnx: don't use errno, don't exit */
 130 void
 131 vwarnx(const char *fmt, va_list ap)
 132 {
 133         __printerr(0, fmt, ap);
 134 }
 135 
 136 /* err/verr: use errno, then exit */
 137 void
 138 verr(int exitcode, const char *fmt, va_list ap)
 139 {
 140         __printerr(1, fmt, ap);
 141         exit(exitcode);
 142 }
 143 
 144 /* errx/verrx: don't use errno, but do then exit */
 145 void
 146 verrx(int exitcode, const char *fmt, va_list ap)
 147 {
 148         __printerr(0, fmt, ap);
 149         exit(exitcode);
 150 }
 151 
 152 /*
 153  * The non-va_list versions of the warn/err functions.
 154  * Just hand off to the va_list versions.
 155  */
 156 
 157 void
 158 warn(const char *fmt, ...)
 159 {
 160         va_list ap;
 161         va_start(ap, fmt);
 162         vwarn(fmt, ap);
 163         va_end(ap);
 164 }
 165 
 166 void
 167 warnx(const char *fmt, ...)
 168 {
 169         va_list ap;
 170         va_start(ap, fmt);
 171         vwarnx(fmt, ap);
 172         va_end(ap);
 173 }
 174 
 175 void
 176 err(int exitcode, const char *fmt, ...)
 177 {
 178         va_list ap;
 179         va_start(ap, fmt);
 180         verr(exitcode, fmt, ap);
 181         va_end(ap);
 182 }
 183 
 184 void
 185 errx(int exitcode, const char *fmt, ...)
 186 {
 187         va_list ap;
 188         va_start(ap, fmt);
 189         verrx(exitcode, fmt, ap);
 190         va_end(ap);
 191 }

/* [<][>][^][v][top][bottom][index][help] */