/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- hostcompat_printerr
- vwarn
- vwarnx
- verr
- verrx
- warn
- warnx
- err
- 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 /*
31 * 4.4BSD error printing functions.
32 */
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <string.h>
38
39 #include "host-err.h"
40
41 #ifdef NEED_ERR
42
43 /*
44 * This is initialized by hostcompat_init
45 */
46 extern const char *hostcompat_progname;
47
48 /*
49 * Common routine for all the *err* and *warn* functions.
50 */
51 static
52 void
53 hostcompat_printerr(int use_errno, const char *fmt, va_list ap)
54 {
55 const char *errmsg;
56
57 /*
58 * Get the error message for the current errno.
59 * Do this early, before doing anything that might change the
60 * value in errno.
61 */
62 errmsg = strerror(errno);
63
64 /*
65 * Look up the program name.
66 * Strictly speaking we should pull off the rightmost
67 * path component of argv[0] and use that as the program
68 * name (this is how BSD err* prints) but it doesn't make
69 * much difference.
70 */
71 if (hostcompat_progname != NULL) {
72 fprintf(stderr, "%s: ", hostcompat_progname);
73 }
74 else {
75 fprintf(stderr, "libhostcompat: hostcompat_init not called\n");
76 fprintf(stderr, "libhostcompat-program: ");
77 }
78
79 /* process the printf format and args */
80 vfprintf(stderr, fmt, ap);
81
82 if (use_errno) {
83 /* if we're using errno, print the error string from above. */
84 fprintf(stderr, ": %s\n", errmsg);
85 }
86 else {
87 /* otherwise, just a newline. */
88 fprintf(stderr, "\n");
89 }
90 }
91
92 /*
93 * The va_list versions of the warn/err functions.
94 */
95
96 /* warn/vwarn: use errno, don't exit */
97 void
98 vwarn(const char *fmt, va_list ap)
99 {
100 hostcompat_printerr(1, fmt, ap);
101 }
102
103 /* warnx/vwarnx: don't use errno, don't exit */
104 void
105 vwarnx(const char *fmt, va_list ap)
106 {
107 hostcompat_printerr(0, fmt, ap);
108 }
109
110 /* err/verr: use errno, then exit */
111 void
112 verr(int exitcode, const char *fmt, va_list ap)
113 {
114 hostcompat_printerr(1, fmt, ap);
115 exit(exitcode);
116 }
117
118 /* errx/verrx: don't use errno, but do then exit */
119 void
120 verrx(int exitcode, const char *fmt, va_list ap)
121 {
122 hostcompat_printerr(0, fmt, ap);
123 exit(exitcode);
124 }
125
126 /*
127 * The non-va_list versions of the warn/err functions.
128 * Just hand off to the va_list versions.
129 */
130
131 void
132 warn(const char *fmt, ...)
133 {
134 va_list ap;
135 va_start(ap, fmt);
136 vwarn(fmt, ap);
137 va_end(ap);
138 }
139
140 void
141 warnx(const char *fmt, ...)
142 {
143 va_list ap;
144 va_start(ap, fmt);
145 vwarnx(fmt, ap);
146 va_end(ap);
147 }
148
149 void
150 err(int exitcode, const char *fmt, ...)
151 {
152 va_list ap;
153 va_start(ap, fmt);
154 verr(exitcode, fmt, ap);
155 va_end(ap);
156 }
157
158 void
159 errx(int exitcode, const char *fmt, ...)
160 {
161 va_list ap;
162 va_start(ap, fmt);
163 verrx(exitcode, fmt, ap);
164 va_end(ap);
165 }
166
167 #endif /* NEED_ERR */