2 Copyright (c) 2004 The Regents of the University of Michigan.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the University nor the names of its
15 contributors may be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 static int verbosity = 0;
42 static char message_buf[500];
44 void initerr(char *progname, int set_verbosity, int set_fg)
46 verbosity = set_verbosity;
49 openlog(progname, LOG_PID, LOG_DAEMON);
53 void printerr(int priority, char *format, ...)
57 int buf_used, buf_available;
60 /* Don't bother formatting a message we're never going to print! */
61 if (priority > verbosity)
64 buf_used = strlen(message_buf);
65 /* subtract 4 to leave room for "...\n" if necessary */
66 buf_available = sizeof(message_buf) - buf_used - 4;
67 buf = message_buf + buf_used;
70 * Aggregate lines: only print buffer when we get to the
71 * end of a line or run out of space
73 va_start(args, format);
74 ret = vsnprintf(buf, buf_available, format, args);
79 if (ret >= buf_available) {
80 /* Indicate we're truncating */
81 strcat(message_buf, "...\n");
84 if (message_buf[strlen(message_buf) - 1] == '\n')
89 fprintf(stderr, "%s", message_buf);
91 syslog(LOG_ERR, "%s", message_buf);
93 /* reset the buffer */
94 memset(message_buf, 0, sizeof(message_buf));
97 void print_hexl(int pri, unsigned char *cp, int length)
102 printerr(pri, "length %d\n",length);
105 for (i = 0; i < length; i += 0x10) {
106 printerr(pri, " %04x: ", (u_int)i);
108 jm = jm > 16 ? 16 : jm;
110 for (j = 0; j < jm; j++) {
112 printerr(pri,"%02x ", (u_int)cp[i+j]);
114 printerr(pri,"%02x", (u_int)cp[i+j]);
116 for (; j < 16; j++) {
124 for (j = 0; j < jm; j++) {
126 c = isprint(c) ? c : '.';
127 printerr(pri,"%c", c);