1 /* $NetBSD: print.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $ */ 2 3 /* 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. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 17 * 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 28 * SUCH DAMAGE. 29 */ 30 /* 31 * print.c - debugging printout routines 32 */ 33 34 #include "file.h" 35 36 #ifndef lint 37 #if 0 38 FILE_RCSID("@(#)$File: print.c,v 1.66 2009/02/03 20:27:51 christos Exp $") 39 #else 40 __RCSID("$NetBSD: print.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $"); 41 #endif 42 #endif /* lint */ 43 44 #include <string.h> 45 #include <stdarg.h> 46 #include <stdlib.h> 47 #ifdef HAVE_UNISTD_H 48 #include <unistd.h> 49 #endif 50 #include <time.h> 51 52 #define SZOF(a) (sizeof(a) / sizeof(a[0])) 53 54 #ifndef COMPILE_ONLY 55 protected void 56 file_mdump(struct magic *m) 57 { 58 private const char optyp[] = { FILE_OPS }; 59 60 (void) fprintf(stderr, "[%u", m->lineno); 61 (void) fprintf(stderr, ">>>>>>>> %u" + 8 - (m->cont_level & 7), 62 m->offset); 63 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) < 73 SZOF(optyp)) ? 74 optyp[m->in_op & FILE_OPS_MASK] : '?', 75 m->in_offset); 76 } 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); 82 83 if (IS_STRING(m->type)) { 84 if (m->str_flags) { 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, 90 stderr); 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); 97 } 98 if (m->str_range) 99 (void) fprintf(stderr, "/%u", m->str_range); 100 } 101 else { 102 if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp)) 103 (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr); 104 else 105 (void) fputc('?', stderr); 106 107 if (m->num_mask) { 108 (void) fprintf(stderr, "%.8llx", 109 (unsigned long long)m->num_mask); 110 } 111 } 112 (void) fprintf(stderr, ",%c", m->reln); 113 114 if (m->reln != 'x') { 115 switch (m->type) { 116 case FILE_BYTE: 117 case FILE_SHORT: 118 case FILE_LONG: 119 case FILE_LESHORT: 120 case FILE_LELONG: 121 case FILE_MELONG: 122 case FILE_BESHORT: 123 case FILE_BELONG: 124 (void) fprintf(stderr, "%d", m->value.l); 125 break; 126 case FILE_BEQUAD: 127 case FILE_LEQUAD: 128 case FILE_QUAD: 129 (void) fprintf(stderr, "%lld", 130 (unsigned long long)m->value.q); 131 break; 132 case FILE_PSTRING: 133 case FILE_STRING: 134 case FILE_REGEX: 135 case FILE_BESTRING16: 136 case FILE_LESTRING16: 137 case FILE_SEARCH: 138 file_showstr(stderr, m->value.s, (size_t)m->vallen); 139 break; 140 case FILE_DATE: 141 case FILE_LEDATE: 142 case FILE_BEDATE: 143 case FILE_MEDATE: 144 (void)fprintf(stderr, "%s,", 145 file_fmttime(m->value.l, 1)); 146 break; 147 case FILE_LDATE: 148 case FILE_LELDATE: 149 case FILE_BELDATE: 150 case FILE_MELDATE: 151 (void)fprintf(stderr, "%s,", 152 file_fmttime(m->value.l, 0)); 153 break; 154 case FILE_QDATE: 155 case FILE_LEQDATE: 156 case FILE_BEQDATE: 157 (void)fprintf(stderr, "%s,", 158 file_fmttime((uint32_t)m->value.q, 1)); 159 break; 160 case FILE_QLDATE: 161 case FILE_LEQLDATE: 162 case FILE_BEQLDATE: 163 (void)fprintf(stderr, "%s,", 164 file_fmttime((uint32_t)m->value.q, 0)); 165 break; 166 case FILE_FLOAT: 167 case FILE_BEFLOAT: 168 case FILE_LEFLOAT: 169 (void) fprintf(stderr, "%G", m->value.f); 170 break; 171 case FILE_DOUBLE: 172 case FILE_BEDOUBLE: 173 case FILE_LEDOUBLE: 174 (void) fprintf(stderr, "%G", m->value.d); 175 break; 176 case FILE_DEFAULT: 177 /* XXX - do anything here? */ 178 break; 179 default: 180 (void) fputs("*bad*", stderr); 181 break; 182 } 183 } 184 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 185 } 186 #endif 187 188 /*VARARGS*/ 189 protected void 190 file_magwarn(struct magic_set *ms, const char *f, ...) 191 { 192 va_list va; 193 194 /* cuz we use stdout for most, stderr here */ 195 (void) fflush(stdout); 196 197 if (ms->file) 198 (void) fprintf(stderr, "%s, %lu: ", ms->file, 199 (unsigned long)ms->line); 200 (void) fprintf(stderr, "Warning: "); 201 va_start(va, f); 202 (void) vfprintf(stderr, f, va); 203 va_end(va); 204 (void) fputc('\n', stderr); 205 } 206 207 protected const char * 208 file_fmttime(uint32_t v, int local) 209 { 210 char *pp; 211 time_t t = (time_t)v; 212 struct tm *tm; 213 214 if (local) { 215 pp = ctime(&t); 216 } else { 217 #ifndef HAVE_DAYLIGHT 218 private int daylight = 0; 219 #ifdef HAVE_TM_ISDST 220 private time_t now = (time_t)0; 221 222 if (now == (time_t)0) { 223 struct tm *tm1; 224 (void)time(&now); 225 tm1 = localtime(&now); 226 if (tm1 == NULL) 227 return "*Invalid time*"; 228 daylight = tm1->tm_isdst; 229 } 230 #endif /* HAVE_TM_ISDST */ 231 #endif /* HAVE_DAYLIGHT */ 232 if (daylight) 233 t += 3600; 234 tm = gmtime(&t); 235 if (tm == NULL) 236 return "*Invalid time*"; 237 pp = asctime(tm); 238 } 239 240 pp[strcspn(pp, "\n")] = '\0'; 241 return pp; 242 } 243