Annotation of src/usr.bin/file/print.c, Revision 1.14
1.14 ! chl 1: /* $OpenBSD: print.c,v 1.13 2007/09/14 14:29:20 chl Exp $ */
1.1 deraadt 2: /*
1.9 ian 3: * Copyright (c) Ian F. Darwin 1986-1995.
4: * Software written by Ian F. Darwin and others;
5: * maintained 1995-present by Christos Zoulas and others.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice immediately at the beginning of the file, without modification,
12: * this list of conditions, and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
1.1 deraadt 28: */
1.11 tedu 29: /*
30: * print.c - debugging printout routines
31: */
1.1 deraadt 32:
1.11 tedu 33: #include "file.h"
1.1 deraadt 34: #include <stdio.h>
35: #include <errno.h>
36: #include <string.h>
1.7 millert 37: #include <stdarg.h>
1.1 deraadt 38: #include <stdlib.h>
1.11 tedu 39: #ifdef HAVE_UNISTD_H
1.1 deraadt 40: #include <unistd.h>
1.11 tedu 41: #endif
1.1 deraadt 42: #include <time.h>
43:
44: #ifndef lint
1.14 ! chl 45: FILE_RCSID("@(#)$Id: print.c,v 1.13 2007/09/14 14:29:20 chl Exp $")
1.1 deraadt 46: #endif /* lint */
47:
48: #define SZOF(a) (sizeof(a) / sizeof(a[0]))
49:
1.11 tedu 50: #ifndef COMPILE_ONLY
51: protected void
52: file_mdump(struct magic *m)
1.1 deraadt 53: {
1.14 ! chl 54: private const char optyp[] = { FILE_OPS };
! 55:
! 56: (void) fprintf(stderr, "[%u", m->lineno);
! 57: (void) fprintf(stderr, ">>>>>>>> %u" + 8 - (m->cont_level & 7),
1.1 deraadt 58: m->offset);
59:
1.11 tedu 60: if (m->flag & INDIR) {
61: (void) fprintf(stderr, "(%s,",
62: /* Note: type is unsigned */
1.14 ! chl 63: (m->in_type < file_nnames) ?
! 64: file_names[m->in_type] : "*bad*");
1.11 tedu 65: if (m->in_op & FILE_OPINVERSE)
66: (void) fputc('~', stderr);
1.14 ! chl 67: (void) fprintf(stderr, "%c%u),",
! 68: ((m->in_op & FILE_OPS_MASK) < SZOF(optyp)) ?
! 69: optyp[m->in_op & FILE_OPS_MASK] : '?',
1.11 tedu 70: m->in_offset);
71: }
1.1 deraadt 72: (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
1.11 tedu 73: /* Note: type is unsigned */
1.14 ! chl 74: (m->type < file_nnames) ? file_names[m->type] : "*bad*");
1.11 tedu 75: if (m->mask_op & FILE_OPINVERSE)
76: (void) fputc('~', stderr);
1.14 ! chl 77:
! 78: if (IS_STRING(m->type)) {
! 79: if (m->str_flags) {
! 80: (void) fputc('/', stderr);
! 81: if (m->str_flags & STRING_COMPACT_BLANK)
1.11 tedu 82: (void) fputc(CHAR_COMPACT_BLANK, stderr);
1.14 ! chl 83: if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK)
1.11 tedu 84: (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
1.14 ! chl 85: stderr);
! 86: if (m->str_flags & STRING_IGNORE_LOWERCASE)
! 87: (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
! 88: if (m->str_flags & STRING_IGNORE_UPPERCASE)
! 89: (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
! 90: if (m->str_flags & REGEX_OFFSET_START)
! 91: (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
! 92: }
! 93: if (m->str_count)
! 94: (void) fprintf(stderr, "/%u", m->str_count);
! 95: }
! 96: else {
! 97: if ((m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
! 98: (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
! 99: else
! 100: (void) fputc('?', stderr);
! 101:
! 102: if (m->num_mask) {
! 103: (void) fprintf(stderr, "%.8llx",
! 104: (unsigned long long)m->num_mask);
1.11 tedu 105: }
106: }
1.1 deraadt 107: (void) fprintf(stderr, ",%c", m->reln);
108:
109: if (m->reln != 'x') {
1.11 tedu 110: switch (m->type) {
111: case FILE_BYTE:
112: case FILE_SHORT:
113: case FILE_LONG:
114: case FILE_LESHORT:
115: case FILE_LELONG:
1.14 ! chl 116: case FILE_MELONG:
1.11 tedu 117: case FILE_BESHORT:
118: case FILE_BELONG:
119: (void) fprintf(stderr, "%d", m->value.l);
120: break;
1.14 ! chl 121: case FILE_BEQUAD:
! 122: case FILE_LEQUAD:
! 123: case FILE_QUAD:
! 124: (void) fprintf(stderr, "%lld",
! 125: (unsigned long long)m->value.q);
! 126: break;
! 127: case FILE_PSTRING:
1.11 tedu 128: case FILE_STRING:
129: case FILE_REGEX:
1.14 ! chl 130: case FILE_BESTRING16:
! 131: case FILE_LESTRING16:
! 132: case FILE_SEARCH:
! 133: file_showstr(stderr, m->value.s, (size_t)m->vallen);
1.11 tedu 134: break;
135: case FILE_DATE:
136: case FILE_LEDATE:
137: case FILE_BEDATE:
1.14 ! chl 138: case FILE_MEDATE:
1.11 tedu 139: (void)fprintf(stderr, "%s,",
140: file_fmttime(m->value.l, 1));
141: break;
142: case FILE_LDATE:
143: case FILE_LELDATE:
144: case FILE_BELDATE:
1.14 ! chl 145: case FILE_MELDATE:
1.11 tedu 146: (void)fprintf(stderr, "%s,",
147: file_fmttime(m->value.l, 0));
148: break;
1.14 ! chl 149: case FILE_QDATE:
! 150: case FILE_LEQDATE:
! 151: case FILE_BEQDATE:
! 152: (void)fprintf(stderr, "%s,",
! 153: file_fmttime((uint32_t)m->value.q, 1));
! 154: break;
! 155: case FILE_QLDATE:
! 156: case FILE_LEQLDATE:
! 157: case FILE_BEQLDATE:
! 158: (void)fprintf(stderr, "%s,",
! 159: file_fmttime((uint32_t)m->value.q, 0));
! 160: break;
! 161: case FILE_DEFAULT:
! 162: /* XXX - do anything here? */
! 163: break;
1.11 tedu 164: default:
165: (void) fputs("*bad*", stderr);
166: break;
167: }
1.1 deraadt 168: }
169: (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
1.8 ian 170: }
1.11 tedu 171: #endif
1.8 ian 172:
1.11 tedu 173: /*VARARGS*/
174: protected void
1.14 ! chl 175: file_magwarn(struct magic_set *ms, const char *f, ...)
1.8 ian 176: {
177: va_list va;
1.11 tedu 178: va_start(va, f);
179:
180: /* cuz we use stdout for most, stderr here */
181: (void) fflush(stdout);
1.1 deraadt 182:
1.14 ! chl 183: (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
! 184: (unsigned long)ms->line);
1.11 tedu 185: (void) vfprintf(stderr, f, va);
186: va_end(va);
1.14 ! chl 187: (void) fputc('\n', stderr);
1.1 deraadt 188: }
189:
1.14 ! chl 190: protected const char *
1.11 tedu 191: file_fmttime(uint32_t v, int local)
1.1 deraadt 192: {
1.13 chl 193: char *pp;
1.11 tedu 194: time_t t = (time_t)v;
195: struct tm *tm;
196:
197: if (local) {
198: pp = ctime(&t);
199: } else {
200: #ifndef HAVE_DAYLIGHT
201: private int daylight = 0;
202: #ifdef HAVE_TM_ISDST
203: private time_t now = (time_t)0;
204:
205: if (now == (time_t)0) {
206: struct tm *tm1;
207: (void)time(&now);
208: tm1 = localtime(&now);
1.14 ! chl 209: if (tm1 == NULL)
! 210: return "*Invalid time*";
1.11 tedu 211: daylight = tm1->tm_isdst;
212: }
213: #endif /* HAVE_TM_ISDST */
214: #endif /* HAVE_DAYLIGHT */
215: if (daylight)
216: t += 3600;
217: tm = gmtime(&t);
1.14 ! chl 218: if (tm == NULL)
! 219: return "*Invalid time*";
1.11 tedu 220: pp = asctime(tm);
221: }
1.7 millert 222:
1.12 gilles 223: pp[strcspn(pp, "\n")] = '\0';
1.11 tedu 224: return pp;
1.1 deraadt 225: }