2 * print.c - debugging printout routines
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
7 * This software is not subject to any license of the American Telephone
8 * and Telegraph Company or of the Regents of the University of California.
10 * Permission is granted to anyone to use this software for any purpose on
11 * any computer system, and to alter it and redistribute it freely, subject
12 * to the following restrictions:
14 * 1. The author is not responsible for the consequences of use of this
15 * software, no matter how awful, even if they arise from flaws in it.
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits must appear in the documentation.
21 * 3. Altered versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software. Since few users
23 * ever read sources, credits must appear in the documentation.
25 * 4. This notice may not be removed or altered.
42 FILE_RCSID("@(#)$Id: print.c,v 1.38 2002/07/03 18:37:44 christos Exp $")
45 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
49 mdump(struct magic *m)
51 static const char *typ[] = { "invalid", "byte", "short", "invalid",
52 "long", "string", "date", "beshort",
53 "belong", "bedate", "leshort", "lelong",
54 "ledate", "pstring", "ldate", "beldate",
56 static const char optyp[] = { '@', '&', '|', '^', '+', '-',
58 (void) fputc('[', stderr);
59 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
62 if (m->flag & INDIR) {
63 (void) fprintf(stderr, "(%s,",
64 /* Note: type is unsigned */
65 (m->in_type < SZOF(typ)) ?
66 typ[m->in_type] : "*bad*");
67 if (m->in_op & OPINVERSE)
68 (void) fputc('~', stderr);
69 (void) fprintf(stderr, "%c%d),",
70 ((m->in_op&0x7F) < SZOF(optyp)) ?
71 optyp[m->in_op&0x7F] : '?',
74 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
75 /* Note: type is unsigned */
76 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
77 if (m->mask_op & OPINVERSE)
78 (void) fputc('~', stderr);
80 ((m->mask_op&0x7F) < SZOF(optyp)) ?
81 (void) fputc(optyp[m->mask_op&0x7F], stderr) :
82 (void) fputc('?', stderr);
83 if(STRING != m->type || PSTRING != m->type)
84 (void) fprintf(stderr, "%.8x", m->mask);
86 if (m->mask & STRING_IGNORE_LOWERCASE)
87 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
88 if (m->mask & STRING_COMPACT_BLANK)
89 (void) fputc(CHAR_COMPACT_BLANK, stderr);
90 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
91 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
96 (void) fprintf(stderr, ",%c", m->reln);
107 (void) fprintf(stderr, "%d", m->value.l);
112 showstr(stderr, m->value.s, -1);
117 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 1));
122 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 0));
125 (void) fputs("*bad*", stderr);
129 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
134 * ckfputs - fputs, but with error checking
135 * ckfprintf - fprintf, but with error checking
138 ckfputs(const char *str, FILE *fil)
140 if (fputs(str,fil) == EOF)
141 error("write failed.\n");
146 ckfprintf(FILE *f, const char *fmt, ...)
151 (void) vfprintf(f, fmt, va);
153 error("write failed.\n");
158 * error - print best error message possible and exit
162 error(const char *f, ...)
167 /* cuz we use stdout for most, stderr here */
168 (void) fflush(stdout);
170 if (progname != NULL)
171 (void) fprintf(stderr, "%s: ", progname);
172 (void) vfprintf(stderr, f, va);
179 magwarn(const char *f, ...)
184 /* cuz we use stdout for most, stderr here */
185 (void) fflush(stdout);
187 if (progname != NULL)
188 (void) fprintf(stderr, "%s: %s, %d: ",
189 progname, magicfile, lineno);
190 (void) vfprintf(stderr, f, va);
198 fmttime(long v, int local)
201 time_t t = (time_t)v;
207 #ifndef HAVE_DAYLIGHT
208 static int daylight = 0;
210 static time_t now = (time_t)0;
212 if (now == (time_t)0) {
215 tm1 = localtime(&now);
216 daylight = tm1->tm_isdst;
218 #endif /* HAVE_TM_ISDST */
219 #endif /* HAVE_DAYLIGHT */
226 if ((rt = strchr(pp, '\n')) != NULL)