print.c revision 103373
1/* 2 * print.c - debugging printout routines 3 * 4 * Copyright (c) Ian F. Darwin, 1987. 5 * Written by Ian F. Darwin. 6 * 7 * This software is not subject to any license of the American Telephone 8 * and Telegraph Company or of the Regents of the University of California. 9 * 10 * Permission is granted to anyone to use this software for any purpose on 11 * any computer system, and to alter it and redistribute it freely, subject 12 * to the following restrictions: 13 * 14 * 1. The author is not responsible for the consequences of use of this 15 * software, no matter how awful, even if they arise from flaws in it. 16 * 17 * 2. The origin of this software must not be misrepresented, either by 18 * explicit claim or by omission. Since few users ever read sources, 19 * credits must appear in the documentation. 20 * 21 * 3. Altered versions must be plainly marked as such, and must not be 22 * misrepresented as being the original software. Since few users 23 * ever read sources, credits must appear in the documentation. 24 * 25 * 4. This notice may not be removed or altered. 26 */ 27 28#include "file.h" 29#include <string.h> 30#ifdef __STDC__ 31# include <stdarg.h> 32#else 33# include <varargs.h> 34#endif 35#include <stdlib.h> 36#ifdef HAVE_UNISTD_H 37#include <unistd.h> 38#endif 39#include <time.h> 40 41#ifndef lint 42FILE_RCSID("@(#)$Id: print.c,v 1.38 2002/07/03 18:37:44 christos Exp $") 43#endif /* lint */ 44 45#define SZOF(a) (sizeof(a) / sizeof(a[0])) 46 47#ifndef COMPILE_ONLY 48void 49mdump(struct magic *m) 50{ 51 static const char *typ[] = { "invalid", "byte", "short", "invalid", 52 "long", "string", "date", "beshort", 53 "belong", "bedate", "leshort", "lelong", 54 "ledate", "pstring", "ldate", "beldate", 55 "leldate", "regex" }; 56 static const char optyp[] = { '@', '&', '|', '^', '+', '-', 57 '*', '/', '%' }; 58 (void) fputc('[', stderr); 59 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7), 60 m->offset); 61 62 if (m->flag & INDIR) { 63 (void) fprintf(stderr, "(%s,", 64 /* Note: type is unsigned */ 65 (m->in_type < SZOF(typ)) ? 66 typ[m->in_type] : "*bad*"); 67 if (m->in_op & OPINVERSE) 68 (void) fputc('~', stderr); 69 (void) fprintf(stderr, "%c%d),", 70 ((m->in_op&0x7F) < SZOF(optyp)) ? 71 optyp[m->in_op&0x7F] : '?', 72 m->in_offset); 73 } 74 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "", 75 /* Note: type is unsigned */ 76 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*"); 77 if (m->mask_op & OPINVERSE) 78 (void) fputc('~', stderr); 79 if (m->mask) { 80 ((m->mask_op&0x7F) < SZOF(optyp)) ? 81 (void) fputc(optyp[m->mask_op&0x7F], stderr) : 82 (void) fputc('?', stderr); 83 if(STRING != m->type || PSTRING != m->type) 84 (void) fprintf(stderr, "%.8x", m->mask); 85 else { 86 if (m->mask & STRING_IGNORE_LOWERCASE) 87 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr); 88 if (m->mask & STRING_COMPACT_BLANK) 89 (void) fputc(CHAR_COMPACT_BLANK, stderr); 90 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK) 91 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK, 92 stderr); 93 } 94 } 95 96 (void) fprintf(stderr, ",%c", m->reln); 97 98 if (m->reln != 'x') { 99 switch (m->type) { 100 case BYTE: 101 case SHORT: 102 case LONG: 103 case LESHORT: 104 case LELONG: 105 case BESHORT: 106 case BELONG: 107 (void) fprintf(stderr, "%d", m->value.l); 108 break; 109 case STRING: 110 case PSTRING: 111 case REGEX: 112 showstr(stderr, m->value.s, -1); 113 break; 114 case DATE: 115 case LEDATE: 116 case BEDATE: 117 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 1)); 118 break; 119 case LDATE: 120 case LELDATE: 121 case BELDATE: 122 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 0)); 123 break; 124 default: 125 (void) fputs("*bad*", stderr); 126 break; 127 } 128 } 129 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 130} 131#endif 132 133/* 134 * ckfputs - fputs, but with error checking 135 * ckfprintf - fprintf, but with error checking 136 */ 137void 138ckfputs(const char *str, FILE *fil) 139{ 140 if (fputs(str,fil) == EOF) 141 error("write failed.\n"); 142} 143 144/*VARARGS*/ 145void 146ckfprintf(FILE *f, const char *fmt, ...) 147{ 148 va_list va; 149 150 va_start(va, fmt); 151 (void) vfprintf(f, fmt, va); 152 if (ferror(f)) 153 error("write failed.\n"); 154 va_end(va); 155} 156 157/* 158 * error - print best error message possible and exit 159 */ 160/*VARARGS*/ 161void 162error(const char *f, ...) 163{ 164 va_list va; 165 166 va_start(va, f); 167 /* cuz we use stdout for most, stderr here */ 168 (void) fflush(stdout); 169 170 if (progname != NULL) 171 (void) fprintf(stderr, "%s: ", progname); 172 (void) vfprintf(stderr, f, va); 173 va_end(va); 174 exit(1); 175} 176 177/*VARARGS*/ 178void 179magwarn(const char *f, ...) 180{ 181 va_list va; 182 183 va_start(va, f); 184 /* cuz we use stdout for most, stderr here */ 185 (void) fflush(stdout); 186 187 if (progname != NULL) 188 (void) fprintf(stderr, "%s: %s, %d: ", 189 progname, magicfile, lineno); 190 (void) vfprintf(stderr, f, va); 191 va_end(va); 192 fputc('\n', stderr); 193} 194 195 196#ifndef COMPILE_ONLY 197char * 198fmttime(long v, int local) 199{ 200 char *pp, *rt; 201 time_t t = (time_t)v; 202 struct tm *tm; 203 204 if (local) { 205 pp = ctime(&t); 206 } else { 207#ifndef HAVE_DAYLIGHT 208 static int daylight = 0; 209#ifdef HAVE_TM_ISDST 210 static time_t now = (time_t)0; 211 212 if (now == (time_t)0) { 213 struct tm *tm1; 214 (void)time(&now); 215 tm1 = localtime(&now); 216 daylight = tm1->tm_isdst; 217 } 218#endif /* HAVE_TM_ISDST */ 219#endif /* HAVE_DAYLIGHT */ 220 if (daylight) 221 t += 3600; 222 tm = gmtime(&t); 223 pp = asctime(tm); 224 } 225 226 if ((rt = strchr(pp, '\n')) != NULL) 227 *rt = '\0'; 228 return pp; 229} 230#endif 231