1 --- err.c.orig 2011-02-15 16:29:48.000000000 -0800
2 +++ err.c 2011-02-15 18:01:51.000000000 -0800
3 @@ -40,12 +40,107 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/err
8 #include "un-namespace.h"
12 +#endif /* __BLOCKS__ */
13 #include "libc_private.h"
15 -static FILE *err_file; /* file to use for error output */
16 -static void (*err_exit)(int);
17 +#define ERR_EXIT_UNDEF 0
19 +#define ERR_EXIT_BLOCK 1
20 +#endif /* __BLOCKS__ */
21 +#define ERR_EXIT_FUNC 2
26 +#endif /* __BLOCKS__ */
31 +#endif /* __BLOCKS__ */
34 +#ifdef BUILDING_VARIANT
36 +__private_extern__ FILE *_e_err_file; /* file to use for error output */
37 +__private_extern__ struct _e_err_exit _e_err_exit;
38 +__private_extern__ void _e_visprintf(FILE * __restrict, const char * __restrict, va_list);
40 +#else /* !BUILDING_VARIANT */
42 +__private_extern__ FILE *_e_err_file = NULL; /* file to use for error output */
43 +__private_extern__ struct _e_err_exit _e_err_exit = {ERR_EXIT_UNDEF};
46 + * zero means pass as is
47 + * 255 means use \nnn (octal)
48 + * otherwise use \x (x is value)
51 +static unsigned char escape[256] = {
53 + 0, /* Unused: strings can't contain nulls */
54 + /* SOH STX ETX EOT ENQ ACK BEL */
55 + 255, 255, 255, 255, 255, 255, 'a',
56 + /* BS HT NL VT NP CR SO SI */
57 + 'b', 0, 0, 'v', 'f', 'r', 255, 255,
58 + /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */
59 + 255, 255, 255, 255, 255, 255, 255, 255,
60 + /* CAN EM SUB ESC FS GS RS US */
61 + 255, 255, 255, 255, 255, 255, 255, 255,
62 + /* the rest are zero */
66 + * Make characters visible. If we can't allocate enough
67 + * memory, we fall back on vfprintf().
69 +__private_extern__ void
70 +_e_visprintf(FILE * __restrict stream, const char * __restrict format, va_list ap)
76 + va_copy(backup, ap);
77 + vasprintf(&str, format, ap);
79 + if ((visstr = malloc(4 * strlen(str) + 1)) != NULL) {
80 + unsigned char *fp = (unsigned char *)str;
81 + unsigned char *tp = (unsigned char *)visstr;
83 + switch(escape[*fp]) {
88 + sprintf((char *)tp, "\\%03o", *fp);
93 + *tp++ = escape[*fp];
99 + fputs(visstr, stream);
107 + vfprintf(stream, format, backup);
112 * This is declared to take a `void *' so that the caller is not required
113 @@ -56,16 +151,27 @@ void
114 err_set_file(void *fp)
121 + _e_err_file = stderr;
125 err_set_exit(void (*ef)(int))
128 + _e_err_exit.type = ERR_EXIT_FUNC;
129 + _e_err_exit.func = ef;
134 +err_set_exit_b(void (^ef)(int))
136 + _e_err_exit.type = ERR_EXIT_BLOCK;
137 + _e_err_exit.block = Block_copy(ef);
139 +#endif /* __BLOCKS__ */
140 +#endif /* !BUILDING_VARIANT */
142 __weak_reference(_err, err);
144 @@ -99,16 +205,21 @@ errc(int eval, int code, const char *fmt
146 verrc(int eval, int code, const char *fmt, va_list ap)
149 + if (_e_err_file == 0)
150 err_set_file((FILE *)0);
151 - fprintf(err_file, "%s: ", _getprogname());
152 + fprintf(_e_err_file, "%s: ", _getprogname());
154 - vfprintf(err_file, fmt, ap);
155 - fprintf(err_file, ": ");
156 + _e_visprintf(_e_err_file, fmt, ap);
157 + fprintf(_e_err_file, ": ");
159 - fprintf(err_file, "%s\n", strerror(code));
162 + fprintf(_e_err_file, "%s\n", strerror(code));
163 + if (_e_err_exit.type)
165 + if (_e_err_exit.type == ERR_EXIT_BLOCK)
166 + _e_err_exit.block(eval);
168 +#endif /* __BLOCKS__ */
169 + _e_err_exit.func(eval);
173 @@ -124,14 +235,19 @@ errx(int eval, const char *fmt, ...)
175 verrx(int eval, const char *fmt, va_list ap)
178 + if (_e_err_file == 0)
179 err_set_file((FILE *)0);
180 - fprintf(err_file, "%s: ", _getprogname());
181 + fprintf(_e_err_file, "%s: ", _getprogname());
183 - vfprintf(err_file, fmt, ap);
184 - fprintf(err_file, "\n");
187 + _e_visprintf(_e_err_file, fmt, ap);
188 + fprintf(_e_err_file, "\n");
189 + if (_e_err_exit.type)
191 + if (_e_err_exit.type == ERR_EXIT_BLOCK)
192 + _e_err_exit.block(eval);
194 +#endif /* __BLOCKS__ */
195 + _e_err_exit.func(eval);
199 @@ -164,14 +280,14 @@ warnc(int code, const char *fmt, ...)
201 vwarnc(int code, const char *fmt, va_list ap)
204 + if (_e_err_file == 0)
205 err_set_file((FILE *)0);
206 - fprintf(err_file, "%s: ", _getprogname());
207 + fprintf(_e_err_file, "%s: ", _getprogname());
209 - vfprintf(err_file, fmt, ap);
210 - fprintf(err_file, ": ");
211 + _e_visprintf(_e_err_file, fmt, ap);
212 + fprintf(_e_err_file, ": ");
214 - fprintf(err_file, "%s\n", strerror(code));
215 + fprintf(_e_err_file, "%s\n", strerror(code));
219 @@ -186,10 +302,10 @@ warnx(const char *fmt, ...)
221 vwarnx(const char *fmt, va_list ap)
224 + if (_e_err_file == 0)
225 err_set_file((FILE *)0);
226 - fprintf(err_file, "%s: ", _getprogname());
227 + fprintf(_e_err_file, "%s: ", _getprogname());
229 - vfprintf(err_file, fmt, ap);
230 - fprintf(err_file, "\n");
231 + _e_visprintf(_e_err_file, fmt, ap);
232 + fprintf(_e_err_file, "\n");