00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "system.h"
00029 #include "file.h"
00030 #include "debug.h"
00031
00032 FILE_RCSID("@(#)Id: print.c,v 1.38 2002/07/03 18:37:44 christos Exp ")
00033
00034
00035
00036
00037
00038 static struct fmagic_s myfmagic;
00039
00040 fmagic global_fmagic = &myfmagic;
00041
00042
00043 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
00044
00045 #ifndef COMPILE_ONLY
00046 void
00047 mdump(struct magic *m)
00048 {
00049
00050 static const char *typ[] = { "invalid", "byte", "short", "invalid",
00051 "long", "string", "date", "beshort",
00052 "belong", "bedate", "leshort", "lelong",
00053 "ledate", "pstring", "ldate", "beldate",
00054 "leldate", "regex" };
00055 static const char optyp[] = { '@', '&', '|', '^', '+', '-',
00056 '*', '/', '%' };
00057 (void) fputc('[', stderr);
00058
00059 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
00060 m->offset);
00061
00062
00063 if (m->flag & INDIR) {
00064 (void) fprintf(stderr, "(%s,",
00065
00066 (m->in_type < SZOF(typ)) ?
00067 typ[m->in_type] : "*bad*");
00068 if (m->in_op & OPINVERSE)
00069 (void) fputc('~', stderr);
00070 (void) fprintf(stderr, "%c%d),",
00071 ((m->in_op&0x7F) < SZOF(optyp)) ?
00072 optyp[m->in_op&0x7F] : '?',
00073 m->in_offset);
00074 }
00075 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
00076
00077 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
00078 if (m->mask_op & OPINVERSE)
00079 (void) fputc('~', stderr);
00080 if (m->mask) {
00081 ((m->mask_op&0x7F) < SZOF(optyp)) ?
00082 (void) fputc(optyp[m->mask_op&0x7F], stderr) :
00083 (void) fputc('?', stderr);
00084 if(STRING != m->type || PSTRING != m->type)
00085 (void) fprintf(stderr, "%.8x", m->mask);
00086 else {
00087 if (m->mask & STRING_IGNORE_LOWERCASE)
00088 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
00089 if (m->mask & STRING_COMPACT_BLANK)
00090 (void) fputc(CHAR_COMPACT_BLANK, stderr);
00091 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
00092 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
00093 stderr);
00094 }
00095 }
00096
00097 (void) fprintf(stderr, ",%c", m->reln);
00098
00099 if (m->reln != 'x') {
00100 switch (m->type) {
00101 case BYTE:
00102 case SHORT:
00103 case LONG:
00104 case LESHORT:
00105 case LELONG:
00106 case BESHORT:
00107 case BELONG:
00108 (void) fprintf(stderr, "%d", m->value.l);
00109 break;
00110 case STRING:
00111 case PSTRING:
00112 case REGEX:
00113 showstr(stderr, m->value.s, -1);
00114 break;
00115 case DATE:
00116 case LEDATE:
00117 case BEDATE:
00118 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 1));
00119 break;
00120 case LDATE:
00121 case LELDATE:
00122 case BELDATE:
00123 (void)fprintf(stderr, "%s,", fmttime(m->value.l, 0));
00124 break;
00125 default:
00126 (void) fputs("*bad*", stderr);
00127 break;
00128 }
00129 }
00130 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
00131 }
00132 #endif
00133
00134 #if !defined(HAVE_ERROR) || defined(__LCLINT__)
00135
00136
00137
00138
00139 void
00140 error(int status, int errnum, const char * format, ...)
00141 {
00142 va_list va;
00143
00144 va_start(va, format);
00145
00146 (void) fflush(stdout);
00147
00148 if (__progname != NULL)
00149 (void) fprintf(stderr, "%s: ", __progname);
00150 (void) vfprintf(stderr, format, va);
00151 va_end(va);
00152 #if NOTYET
00153 if (status)
00154 #endif
00155 exit(status);
00156 }
00157 #endif
00158
00159
00160 void
00161 magwarn(const char *f, ...)
00162 {
00163 fmagic fm = global_fmagic;
00164 va_list va;
00165
00166 va_start(va, f);
00167
00168 (void) fflush(stdout);
00169
00170 if (__progname != NULL)
00171 (void) fprintf(stderr, "%s: %s, %d: ",
00172 __progname, fm->magicfile, fm->lineno);
00173 (void) vfprintf(stderr, f, va);
00174 va_end(va);
00175 (void) fputc('\n', stderr);
00176
00177 return;
00178
00179 }
00180
00181 void
00182 fmagicPrintf(const fmagic fm, const char *f, ...)
00183 {
00184 va_list va;
00185 size_t nob;
00186 int rc;
00187
00188 va_start(va, f);
00189
00190 rc = vsnprintf(fm->obp, fm->nob, f, va);
00191
00192 va_end(va);
00193
00194 fm->obuf[sizeof(fm->obuf)-1] = '\0';
00195 nob = strlen(fm->obp);
00196 fm->obp += nob;
00197 fm->nob -= nob;
00198 }
00199
00200
00201 #ifndef COMPILE_ONLY
00202 char *
00203 fmttime(long v, int local)
00204 {
00205 char *pp = "???";
00206 char *rt;
00207 time_t t = (time_t)v;
00208 struct tm *tm;
00209
00210 if (local) {
00211 pp = ctime(&t);
00212 } else {
00213 #ifndef HAVE_DAYLIGHT
00214 static int daylight = 0;
00215 #ifdef HAVE_TM_ISDST
00216 static time_t now = (time_t)0;
00217
00218 if (now == (time_t)0) {
00219 struct tm *tm1;
00220 (void)time(&now);
00221 tm1 = localtime(&now);
00222 daylight = tm1->tm_isdst;
00223 }
00224 #endif
00225 #endif
00226 if (daylight)
00227 t += 3600;
00228 tm = gmtime(&t);
00229 if (tm != NULL)
00230 pp = asctime(tm);
00231 }
00232
00233
00234 if ((rt = strchr(pp, '\n')) != NULL)
00235 *rt = '\0';
00236
00237 return pp;
00238 }
00239 #endif