2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice immediately at the beginning of the file, without modification,
11 * this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * print.c - debugging printout routines
44 FILE_RCSID("@(#)$Id: print.c,v 1.50 2006/03/02 22:07:53 christos Exp $")
47 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
51 file_mdump(struct magic *m)
53 private const char *typ[] = { FILE_FORMAT_NAME };
54 private const char optyp[] = { FILE_OPS };
56 (void) fputc('[', stderr);
57 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
60 if (m->flag & INDIR) {
61 (void) fprintf(stderr, "(%s,",
62 /* Note: type is unsigned */
63 (m->in_type < SZOF(typ)) ?
64 typ[m->in_type] : "*bad*");
65 if (m->in_op & FILE_OPINVERSE)
66 (void) fputc('~', stderr);
67 (void) fprintf(stderr, "%c%d),",
68 ((m->in_op&0x7F) < SZOF(optyp)) ?
69 optyp[m->in_op&0x7F] : '?',
72 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
73 /* Note: type is unsigned */
74 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
75 if (m->mask_op & FILE_OPINVERSE)
76 (void) fputc('~', stderr);
78 if ((m->mask_op & 0x7F) < SZOF(optyp))
79 fputc(optyp[m->mask_op&0x7F], stderr);
82 if (FILE_STRING != m->type || FILE_PSTRING != m->type)
83 (void) fprintf(stderr, "%.8x", m->mask);
85 if (m->mask & STRING_IGNORE_LOWERCASE)
86 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
87 if (m->mask & STRING_COMPACT_BLANK)
88 (void) fputc(CHAR_COMPACT_BLANK, stderr);
89 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
90 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
95 (void) fprintf(stderr, ",%c", m->reln);
107 (void) fprintf(stderr, "%d", m->value.l);
112 case FILE_BESTRING16:
113 case FILE_LESTRING16:
115 file_showstr(stderr, m->value.s, m->vallen);
121 (void)fprintf(stderr, "%s,",
122 file_fmttime(m->value.l, 1));
128 (void)fprintf(stderr, "%s,",
129 file_fmttime(m->value.l, 0));
132 (void) fputs("*bad*", stderr);
136 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
142 file_magwarn(struct magic_set *ms, const char *f, ...)
147 /* cuz we use stdout for most, stderr here */
148 (void) fflush(stdout);
150 (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
151 (unsigned long)ms->line);
152 (void) vfprintf(stderr, f, va);
157 protected const char *
158 file_fmttime(uint32_t v, int local)
161 time_t t = (time_t)v;
167 #ifndef HAVE_DAYLIGHT
168 private int daylight = 0;
170 private time_t now = (time_t)0;
172 if (now == (time_t)0) {
175 tm1 = localtime(&now);
177 return "*Invalid time*";
178 daylight = tm1->tm_isdst;
180 #endif /* HAVE_TM_ISDST */
181 #endif /* HAVE_DAYLIGHT */
186 return "*Invalid time*";
190 if ((rt = strchr(pp, '\n')) != NULL)