Annotation of src/usr.bin/file/print.c, Revision 1.9
1.9 ! ian 1: /* $OpenBSD: print.c,v 1.8 2003/03/03 22:24:08 ian Exp $ */
1.3 millert 2:
1.1 deraadt 3: /*
4: * print.c - debugging printout routines
5: *
1.9 ! ian 6: * Copyright (c) Ian F. Darwin 1986-1995.
! 7: * Software written by Ian F. Darwin and others;
! 8: * maintained 1995-present by Christos Zoulas and others.
! 9: *
! 10: * Redistribution and use in source and binary forms, with or without
! 11: * modification, are permitted provided that the following conditions
! 12: * are met:
! 13: * 1. Redistributions of source code must retain the above copyright
! 14: * notice immediately at the beginning of the file, without modification,
! 15: * this list of conditions, and the following disclaimer.
! 16: * 2. Redistributions in binary form must reproduce the above copyright
! 17: * notice, this list of conditions and the following disclaimer in the
! 18: * documentation and/or other materials provided with the distribution.
! 19: * 3. All advertising materials mentioning features or use of this software
! 20: * must display the following acknowledgement:
! 21: * This product includes software developed by Ian F. Darwin and others.
! 22: * 4. The name of the author may not be used to endorse or promote products
! 23: * derived from this software without specific prior written permission.
! 24: *
! 25: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 26: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 27: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 28: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
! 29: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 30: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 31: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 32: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 33: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 34: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 35: * SUCH DAMAGE.
1.1 deraadt 36: */
37:
38: #include <stdio.h>
39: #include <errno.h>
40: #include <string.h>
1.7 millert 41: #include <stdarg.h>
1.1 deraadt 42: #include <stdlib.h>
43: #include <unistd.h>
44: #include <time.h>
1.6 mickey 45: #include <err.h>
1.1 deraadt 46: #include "file.h"
47:
48: #ifndef lint
1.9 ! ian 49: static char *moduleid = "$OpenBSD: print.c,v 1.8 2003/03/03 22:24:08 ian Exp $";
1.1 deraadt 50: #endif /* lint */
51:
52: #define SZOF(a) (sizeof(a) / sizeof(a[0]))
53:
54: void
55: mdump(m)
56: struct magic *m;
57: {
58: static char *typ[] = { "invalid", "byte", "short", "invalid",
59: "long", "string", "date", "beshort",
60: "belong", "bedate", "leshort", "lelong",
61: "ledate" };
62: (void) fputc('[', stderr);
63: (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
64: m->offset);
65:
66: if (m->flag & INDIR)
1.3 millert 67: (void) fprintf(stderr, "(%s,%d),",
1.1 deraadt 68: (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
69: typ[(unsigned char) m->in.type] :
70: "*bad*",
71: m->in.offset);
72:
73: (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
74: (m->type >= 0 && m->type < SZOF(typ)) ?
75: typ[(unsigned char) m->type] :
76: "*bad*");
1.5 millert 77: if (m->mask != ~0)
1.3 millert 78: (void) fprintf(stderr, " & %.8x", m->mask);
1.1 deraadt 79:
80: (void) fprintf(stderr, ",%c", m->reln);
81:
82: if (m->reln != 'x') {
83: switch (m->type) {
84: case BYTE:
85: case SHORT:
86: case LONG:
87: case LESHORT:
88: case LELONG:
89: case BESHORT:
90: case BELONG:
1.3 millert 91: (void) fprintf(stderr, "%d", m->value.l);
1.1 deraadt 92: break;
93: case STRING:
94: showstr(stderr, m->value.s, -1);
95: break;
96: case DATE:
97: case LEDATE:
98: case BEDATE:
99: {
100: char *rt, *pp = ctime((time_t*) &m->value.l);
101: if ((rt = strchr(pp, '\n')) != NULL)
102: *rt = '\0';
103: (void) fprintf(stderr, "%s,", pp);
104: if (rt)
105: *rt = '\n';
106: }
107: break;
108: default:
109: (void) fputs("*bad*", stderr);
110: break;
111: }
112: }
113: (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
1.8 ian 114: }
115:
116: /*
117: * This "error" is here so we don't have to change all occurrences of
118: * error() to err(1,...) when importing new versions from Christos.
119: */
120: void error(const char *fmt, ...)
121: {
122: va_list va;
123: va_start(va, fmt);
124: verr(1, fmt, va);
1.1 deraadt 125: }
126:
127: /*
128: * ckfputs - futs, but with error checking
129: * ckfprintf - fprintf, but with error checking
130: */
131: void
132: ckfputs(str, fil)
133: const char *str;
134: FILE *fil;
135: {
136: if (fputs(str,fil) == EOF)
1.6 mickey 137: err(1, "write failed");
1.1 deraadt 138: }
139:
140: /*VARARGS*/
141: void
142: ckfprintf(FILE *f, const char *fmt, ...)
143: {
144: va_list va;
1.7 millert 145:
1.1 deraadt 146: va_start(va, fmt);
147: (void) vfprintf(f, fmt, va);
148: if (ferror(f))
1.6 mickey 149: err(1, "write failed");
1.1 deraadt 150: va_end(va);
151: }