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.
38 FILE_RCSID("@(#)$Id: print.c,v 1.39 2002/07/09 15:46:23 christos Exp $")
41 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
45 mdump(struct magic *m)
47 static const char *typ[] = { "invalid", "byte", "short", "invalid",
48 "long", "string", "date", "beshort",
49 "belong", "bedate", "leshort", "lelong",
50 "ledate", "pstring", "ldate", "beldate",
52 static const char optyp[] = { '@', '&', '|', '^', '+', '-',
54 (void) fputc('[', stderr);
55 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
58 if (m->flag & INDIR) {
59 (void) fprintf(stderr, "(%s,",
60 /* Note: type is unsigned */
61 (m->in_type < SZOF(typ)) ?
62 typ[m->in_type] : "*bad*");
63 if (m->in_op & OPINVERSE)
64 (void) fputc('~', stderr);
65 (void) fprintf(stderr, "%c%d),",
66 ((m->in_op&0x7F) < SZOF(optyp)) ?
67 optyp[m->in_op&0x7F] : '?',
70 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
71 /* Note: type is unsigned */
72 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
73 if (m->mask_op & OPINVERSE)
74 (void) fputc('~', stderr);
76 ((m->mask_op&0x7F) < SZOF(optyp)) ?
77 (void) fputc(optyp[m->mask_op&0x7F], stderr) :
78 (void) fputc('?', stderr);
79 if(STRING != m->type || PSTRING != m->type)
80 (void) fprintf(stderr, "%.8x", m->mask);
82 if (m->mask & STRING_IGNORE_LOWERCASE)
83 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
84 if (m->mask & STRING_COMPACT_BLANK)
85 (void) fputc(CHAR_COMPACT_BLANK, stderr);
86 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
87 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
92 (void) fprintf(stderr, ",%c", m->reln);
103 (void) fprintf(stderr, "%d", m->value.l);
108 showstr(stderr, m->value.s, -1);
113 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 1));
118 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 0));
121 (void) fputs("*bad*", stderr);
125 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
130 * ckfputs - fputs, but with error checking
131 * ckfprintf - fprintf, but with error checking
134 ckfputs(const char *str, FILE *fil)
136 if (fputs(str,fil) == EOF)
137 error("write failed.\n");
142 ckfprintf(FILE *f, const char *fmt, ...)
147 (void) vfprintf(f, fmt, va);
149 error("write failed.\n");
154 * error - print best error message possible and exit
158 error(const char *f, ...)
163 /* cuz we use stdout for most, stderr here */
164 (void) fflush(stdout);
166 if (progname != NULL)
167 (void) fprintf(stderr, "%s: ", progname);
168 (void) vfprintf(stderr, f, va);
175 magwarn(const char *f, ...)
180 /* cuz we use stdout for most, stderr here */
181 (void) fflush(stdout);
183 if (progname != NULL)
184 (void) fprintf(stderr, "%s: %s, %d: ",
185 progname, magicfile, lineno);
186 (void) vfprintf(stderr, f, va);
194 fmttime(long v, int local)
197 time_t t = (time_t)v;
203 #ifndef HAVE_DAYLIGHT
204 static int daylight = 0;
206 static time_t now = (time_t)0;
208 if (now == (time_t)0) {
211 tm1 = localtime(&now);
212 daylight = tm1->tm_isdst;
214 #endif /* HAVE_TM_ISDST */
215 #endif /* HAVE_DAYLIGHT */
222 if ((rt = strchr(pp, '\n')) != NULL)