4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * print.c - debugging printout routines
38 FILE_RCSID("@(#)$File: print.c,v 1.66 2009/02/03 20:27:51 christos Exp $")
52 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
56 file_mdump(struct magic
*m
)
58 private const char optyp
[] = { FILE_OPS
};
60 (void) fprintf(stderr
, "[%u", m
->lineno
);
61 (void) fprintf(stderr
, ">>>>>>>> %u" + 8 - (m
->cont_level
& 7),
64 if (m
->flag
& INDIR
) {
65 (void) fprintf(stderr
, "(%s,",
66 /* Note: type is unsigned */
67 (m
->in_type
< file_nnames
) ?
68 file_names
[m
->in_type
] : "*bad*");
69 if (m
->in_op
& FILE_OPINVERSE
)
70 (void) fputc('~', stderr
);
71 (void) fprintf(stderr
, "%c%u),",
72 ((size_t)(m
->in_op
& FILE_OPS_MASK
) <
74 optyp
[m
->in_op
& FILE_OPS_MASK
] : '?',
77 (void) fprintf(stderr
, " %s%s", (m
->flag
& UNSIGNED
) ? "u" : "",
78 /* Note: type is unsigned */
79 (m
->type
< file_nnames
) ? file_names
[m
->type
] : "*bad*");
80 if (m
->mask_op
& FILE_OPINVERSE
)
81 (void) fputc('~', stderr
);
83 if (IS_STRING(m
->type
)) {
85 (void) fputc('/', stderr
);
86 if (m
->str_flags
& STRING_COMPACT_BLANK
)
87 (void) fputc(CHAR_COMPACT_BLANK
, stderr
);
88 if (m
->str_flags
& STRING_COMPACT_OPTIONAL_BLANK
)
89 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK
,
91 if (m
->str_flags
& STRING_IGNORE_LOWERCASE
)
92 (void) fputc(CHAR_IGNORE_LOWERCASE
, stderr
);
93 if (m
->str_flags
& STRING_IGNORE_UPPERCASE
)
94 (void) fputc(CHAR_IGNORE_UPPERCASE
, stderr
);
95 if (m
->str_flags
& REGEX_OFFSET_START
)
96 (void) fputc(CHAR_REGEX_OFFSET_START
, stderr
);
99 (void) fprintf(stderr
, "/%u", m
->str_range
);
102 if ((size_t)(m
->mask_op
& FILE_OPS_MASK
) < SZOF(optyp
))
103 (void) fputc(optyp
[m
->mask_op
& FILE_OPS_MASK
], stderr
);
105 (void) fputc('?', stderr
);
108 (void) fprintf(stderr
, "%.8llx",
109 (unsigned long long)m
->num_mask
);
112 (void) fprintf(stderr
, ",%c", m
->reln
);
114 if (m
->reln
!= 'x') {
124 (void) fprintf(stderr
, "%d", m
->value
.l
);
129 (void) fprintf(stderr
, "%lld",
130 (unsigned long long)m
->value
.q
);
135 case FILE_BESTRING16
:
136 case FILE_LESTRING16
:
138 file_showstr(stderr
, m
->value
.s
, (size_t)m
->vallen
);
144 (void)fprintf(stderr
, "%s,",
145 file_fmttime(m
->value
.l
, 1));
151 (void)fprintf(stderr
, "%s,",
152 file_fmttime(m
->value
.l
, 0));
157 (void)fprintf(stderr
, "%s,",
158 file_fmttime((uint32_t)m
->value
.q
, 1));
163 (void)fprintf(stderr
, "%s,",
164 file_fmttime((uint32_t)m
->value
.q
, 0));
169 (void) fprintf(stderr
, "%G", m
->value
.f
);
174 (void) fprintf(stderr
, "%G", m
->value
.d
);
177 /* XXX - do anything here? */
180 (void) fputs("*bad*", stderr
);
184 (void) fprintf(stderr
, ",\"%s\"]\n", m
->desc
);
190 file_magwarn(struct magic_set
*ms
, const char *f
, ...)
194 /* cuz we use stdout for most, stderr here */
195 (void) fflush(stdout
);
198 (void) fprintf(stderr
, "%s, %lu: ", ms
->file
,
199 (unsigned long)ms
->line
);
200 (void) fprintf(stderr
, "Warning: ");
202 (void) vfprintf(stderr
, f
, va
);
204 (void) fputc('\n', stderr
);
207 protected const char *
208 file_fmttime(uint32_t v
, int local
)
211 time_t t
= (time_t)v
;
217 #ifndef HAVE_DAYLIGHT
218 private int daylight
= 0;
220 private time_t now
= (time_t)0;
222 if (now
== (time_t)0) {
225 tm1
= localtime(&now
);
227 return "*Invalid time*";
228 daylight
= tm1
->tm_isdst
;
230 #endif /* HAVE_TM_ISDST */
231 #endif /* HAVE_DAYLIGHT */
236 return "*Invalid time*";
240 pp
[strcspn(pp
, "\n")] = '\0';