Update to file-4.19.
[dragonfly.git] / contrib / file-4 / src / print.c
1 /*
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.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *  
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
26  * SUCH DAMAGE.
27  */
28 /*
29  * print.c - debugging printout routines
30  */
31
32 #include "file.h"
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #include <time.h>
42
43 #ifndef lint
44 FILE_RCSID("@(#)$Id: print.c,v 1.56 2006/12/08 20:31:07 christos Exp $")
45 #endif  /* lint */
46
47 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
48
49 #ifndef COMPILE_ONLY
50 protected void
51 file_mdump(struct magic *m)
52 {
53         private const char optyp[] = { FILE_OPS };
54
55         (void) fprintf(stderr, "[%zu", m->lineno);
56         (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
57                        m->offset);
58
59         if (m->flag & INDIR) {
60                 (void) fprintf(stderr, "(%s,",
61                                /* Note: type is unsigned */
62                                (m->in_type < file_nnames) ? 
63                                         file_names[m->in_type] : "*bad*");
64                 if (m->in_op & FILE_OPINVERSE)
65                         (void) fputc('~', stderr);
66                 (void) fprintf(stderr, "%c%d),",
67                                ((m->in_op&0x7F) < SZOF(optyp)) ? 
68                                         optyp[m->in_op&0x7F] : '?',
69                                 m->in_offset);
70         }
71         (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
72                        /* Note: type is unsigned */
73                        (m->type < file_nnames) ? file_names[m->type] : "*bad*");
74         if (m->mask_op & FILE_OPINVERSE)
75                 (void) fputc('~', stderr);
76         if (m->mask) {
77                 if ((m->mask_op & 0x7F) < SZOF(optyp)) 
78                         fputc(optyp[m->mask_op&0x7F], stderr);
79                 else
80                         fputc('?', stderr);
81                 if (FILE_STRING != m->type || FILE_PSTRING != m->type)
82                         (void) fprintf(stderr, "%.8llx",
83                             (unsigned long long)m->mask);
84                 else {
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,
91                                 stderr);
92                 }
93         }
94
95         (void) fprintf(stderr, ",%c", m->reln);
96
97         if (m->reln != 'x') {
98                 switch (m->type) {
99                 case FILE_BYTE:
100                 case FILE_SHORT:
101                 case FILE_LONG:
102                 case FILE_LESHORT:
103                 case FILE_LELONG:
104                 case FILE_MELONG:
105                 case FILE_BESHORT:
106                 case FILE_BELONG:
107                         (void) fprintf(stderr, "%d", m->value.l);
108                         break;
109                 case FILE_BEQUAD:
110                 case FILE_LEQUAD:
111                 case FILE_QUAD:
112                         (void) fprintf(stderr, "%lld",
113                             (unsigned long long)m->value.q);
114                         break;
115                 case FILE_PSTRING:
116                 case FILE_STRING:
117                 case FILE_REGEX:
118                 case FILE_BESTRING16:
119                 case FILE_LESTRING16:
120                 case FILE_SEARCH:
121                         file_showstr(stderr, m->value.s, (size_t)m->vallen);
122                         break;
123                 case FILE_DATE:
124                 case FILE_LEDATE:
125                 case FILE_BEDATE:
126                 case FILE_MEDATE:
127                         (void)fprintf(stderr, "%s,",
128                             file_fmttime(m->value.l, 1));
129                         break;
130                 case FILE_LDATE:
131                 case FILE_LELDATE:
132                 case FILE_BELDATE:
133                 case FILE_MELDATE:
134                         (void)fprintf(stderr, "%s,",
135                             file_fmttime(m->value.l, 0));
136                         break;
137                 case FILE_QDATE:
138                 case FILE_LEQDATE:
139                 case FILE_BEQDATE:
140                         (void)fprintf(stderr, "%s,",
141                             file_fmttime((uint32_t)m->value.q, 1));
142                         break;
143                 case FILE_QLDATE:
144                 case FILE_LEQLDATE:
145                 case FILE_BEQLDATE:
146                         (void)fprintf(stderr, "%s,",
147                             file_fmttime((uint32_t)m->value.q, 0));
148                         break;
149                 default:
150                         (void) fputs("*bad*", stderr);
151                         break;
152                 }
153         }
154         (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
155 }
156 #endif
157
158 /*VARARGS*/
159 protected void
160 file_magwarn(struct magic_set *ms, const char *f, ...)
161 {
162         va_list va;
163         va_start(va, f);
164
165         /* cuz we use stdout for most, stderr here */
166         (void) fflush(stdout); 
167
168         (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
169             (unsigned long)ms->line);
170         (void) vfprintf(stderr, f, va);
171         va_end(va);
172         fputc('\n', stderr);
173 }
174
175 protected const char *
176 file_fmttime(uint32_t v, int local)
177 {
178         char *pp, *rt;
179         time_t t = (time_t)v;
180         struct tm *tm;
181
182         if (local) {
183                 pp = ctime(&t);
184         } else {
185 #ifndef HAVE_DAYLIGHT
186                 private int daylight = 0;
187 #ifdef HAVE_TM_ISDST
188                 private time_t now = (time_t)0;
189
190                 if (now == (time_t)0) {
191                         struct tm *tm1;
192                         (void)time(&now);
193                         tm1 = localtime(&now);
194                         if (tm1 == NULL)
195                                 return "*Invalid time*";
196                         daylight = tm1->tm_isdst;
197                 }
198 #endif /* HAVE_TM_ISDST */
199 #endif /* HAVE_DAYLIGHT */
200                 if (daylight)
201                         t += 3600;
202                 tm = gmtime(&t);
203                 if (tm == NULL)
204                         return "*Invalid time*";
205                 pp = asctime(tm);
206         }
207
208         if ((rt = strchr(pp, '\n')) != NULL)
209                 *rt = '\0';
210         return pp;
211 }