Annotation of src/usr.bin/file/print.c, Revision 1.7.4.1
1.7.4.1 ! margarid 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: *
6: * Copyright (c) Ian F. Darwin, 1987.
7: * Written by Ian F. Darwin.
8: *
9: * This software is not subject to any license of the American Telephone
10: * and Telegraph Company or of the Regents of the University of California.
11: *
12: * Permission is granted to anyone to use this software for any purpose on
13: * any computer system, and to alter it and redistribute it freely, subject
14: * to the following restrictions:
15: *
16: * 1. The author is not responsible for the consequences of use of this
17: * software, no matter how awful, even if they arise from flaws in it.
18: *
19: * 2. The origin of this software must not be misrepresented, either by
20: * explicit claim or by omission. Since few users ever read sources,
21: * credits must appear in the documentation.
22: *
23: * 3. Altered versions must be plainly marked as such, and must not be
24: * misrepresented as being the original software. Since few users
25: * ever read sources, credits must appear in the documentation.
26: *
27: * 4. This notice may not be removed or altered.
28: */
29:
30: #include <stdio.h>
31: #include <errno.h>
32: #include <string.h>
1.7 millert 33: #include <stdarg.h>
1.1 deraadt 34: #include <stdlib.h>
35: #include <unistd.h>
36: #include <time.h>
1.6 mickey 37: #include <err.h>
1.1 deraadt 38: #include "file.h"
39:
40: #ifndef lint
1.7.4.1 ! margarid 41: static char *moduleid = "$OpenBSD: print.c,v 1.8 2003/03/03 22:24:08 ian Exp $";
1.1 deraadt 42: #endif /* lint */
43:
44: #define SZOF(a) (sizeof(a) / sizeof(a[0]))
45:
46: void
47: mdump(m)
48: struct magic *m;
49: {
50: static char *typ[] = { "invalid", "byte", "short", "invalid",
51: "long", "string", "date", "beshort",
52: "belong", "bedate", "leshort", "lelong",
53: "ledate" };
54: (void) fputc('[', stderr);
55: (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
56: m->offset);
57:
58: if (m->flag & INDIR)
1.3 millert 59: (void) fprintf(stderr, "(%s,%d),",
1.1 deraadt 60: (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
61: typ[(unsigned char) m->in.type] :
62: "*bad*",
63: m->in.offset);
64:
65: (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
66: (m->type >= 0 && m->type < SZOF(typ)) ?
67: typ[(unsigned char) m->type] :
68: "*bad*");
1.5 millert 69: if (m->mask != ~0)
1.3 millert 70: (void) fprintf(stderr, " & %.8x", m->mask);
1.1 deraadt 71:
72: (void) fprintf(stderr, ",%c", m->reln);
73:
74: if (m->reln != 'x') {
75: switch (m->type) {
76: case BYTE:
77: case SHORT:
78: case LONG:
79: case LESHORT:
80: case LELONG:
81: case BESHORT:
82: case BELONG:
1.3 millert 83: (void) fprintf(stderr, "%d", m->value.l);
1.1 deraadt 84: break;
85: case STRING:
86: showstr(stderr, m->value.s, -1);
87: break;
88: case DATE:
89: case LEDATE:
90: case BEDATE:
91: {
92: char *rt, *pp = ctime((time_t*) &m->value.l);
93: if ((rt = strchr(pp, '\n')) != NULL)
94: *rt = '\0';
95: (void) fprintf(stderr, "%s,", pp);
96: if (rt)
97: *rt = '\n';
98: }
99: break;
100: default:
101: (void) fputs("*bad*", stderr);
102: break;
103: }
104: }
105: (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
1.7.4.1 ! margarid 106: }
! 107:
! 108: /*
! 109: * This "error" is here so we don't have to change all occurrences of
! 110: * error() to err(1,...) when importing new versions from Christos.
! 111: */
! 112: void error(const char *fmt, ...)
! 113: {
! 114: va_list va;
! 115: va_start(va, fmt);
! 116: verr(1, fmt, va);
1.1 deraadt 117: }
118:
119: /*
120: * ckfputs - futs, but with error checking
121: * ckfprintf - fprintf, but with error checking
122: */
123: void
124: ckfputs(str, fil)
125: const char *str;
126: FILE *fil;
127: {
128: if (fputs(str,fil) == EOF)
1.6 mickey 129: err(1, "write failed");
1.1 deraadt 130: }
131:
132: /*VARARGS*/
133: void
134: ckfprintf(FILE *f, const char *fmt, ...)
135: {
136: va_list va;
1.7 millert 137:
1.1 deraadt 138: va_start(va, fmt);
139: (void) vfprintf(f, fmt, va);
140: if (ferror(f))
1.6 mickey 141: err(1, "write failed");
1.1 deraadt 142: va_end(va);
143: }