file/src/print.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Ian F. Darwin 1986-1995.
00003  * Software written by Ian F. Darwin and others;
00004  * maintained 1995-present by Christos Zoulas and others.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice immediately at the beginning of the file, without modification,
00011  *    this list of conditions, and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *  
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
00020  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00026  * SUCH DAMAGE.
00027  */
00028 /*
00029  * print.c - debugging printout routines
00030  */
00031 
00032 #include "file.h"
00033 #include <stdio.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #include <stdarg.h>
00037 #include <stdlib.h>
00038 #ifdef HAVE_UNISTD_H
00039 #include <unistd.h>
00040 #endif
00041 #include <time.h>
00042 
00043 #ifndef lint
00044 FILE_RCSID("@(#)$Id: print.c,v 1.47 2005/03/14 16:56:25 christos Exp $")
00045 #endif  /* lint */
00046 
00047 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
00048 
00049 #ifndef COMPILE_ONLY
00050 protected void
00051 file_mdump(struct magic *m)
00052 {
00053         /*@observer@*/
00054         private const char *typ[] = { "invalid", "byte", "short", "invalid",
00055                                      "long", "string", "date", "beshort",
00056                                      "belong", "bedate", "leshort", "lelong",
00057                                      "ledate", "pstring", "ldate", "beldate",
00058                                      "leldate", "regex" };
00059         private const char optyp[] = { '@', '&', '|', '^', '+', '-', 
00060                                       '*', '/', '%' };
00061         (void) fputc('[', stderr);
00062         (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
00063                        m->offset);
00064 
00065         if (m->flag & INDIR) {
00066                 (void) fprintf(stderr, "(%s,",
00067                                /* Note: type is unsigned */
00068                                (m->in_type < SZOF(typ)) ? 
00069                                         typ[m->in_type] : "*bad*");
00070                 if (m->in_op & FILE_OPINVERSE)
00071                         (void) fputc('~', stderr);
00072                 (void) fprintf(stderr, "%c%d),",
00073                                ((m->in_op&0x7F) < SZOF(optyp)) ? 
00074                                         optyp[m->in_op&0x7F] : '?',
00075                                 m->in_offset);
00076         }
00077         (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
00078                        /* Note: type is unsigned */
00079                        (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
00080         if (m->mask_op & FILE_OPINVERSE)
00081                 (void) fputc('~', stderr);
00082         if (m->mask) {
00083                 if ((m->mask_op & 0x7F) < SZOF(optyp)) 
00084                         fputc(optyp[m->mask_op&0x7F], stderr);
00085                 else
00086                         fputc('?', stderr);
00087                 if(FILE_STRING != m->type || FILE_PSTRING != m->type)
00088                         (void) fprintf(stderr, "%.8x", m->mask);
00089                 else {
00090                         if (m->mask & STRING_IGNORE_LOWERCASE) 
00091                                 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
00092                         if (m->mask & STRING_COMPACT_BLANK) 
00093                                 (void) fputc(CHAR_COMPACT_BLANK, stderr);
00094                         if (m->mask & STRING_COMPACT_OPTIONAL_BLANK) 
00095                                 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
00096                                 stderr);
00097                 }
00098         }
00099 
00100         (void) fprintf(stderr, ",%c", m->reln);
00101 
00102         if (m->reln != 'x') {
00103                 switch (m->type) {
00104                 case FILE_BYTE:
00105                 case FILE_SHORT:
00106                 case FILE_LONG:
00107                 case FILE_LESHORT:
00108                 case FILE_LELONG:
00109                 case FILE_BESHORT:
00110                 case FILE_BELONG:
00111                         (void) fprintf(stderr, "%d", m->value.l);
00112                         break;
00113                 case FILE_STRING:
00114                 case FILE_PSTRING:
00115                 case FILE_REGEX:
00116                         file_showstr(stderr, m->value.s, ~0U);
00117                         break;
00118                 case FILE_DATE:
00119                 case FILE_LEDATE:
00120                 case FILE_BEDATE:
00121                         (void)fprintf(stderr, "%s,",
00122                             file_fmttime(m->value.l, 1));
00123                         break;
00124                 case FILE_LDATE:
00125                 case FILE_LELDATE:
00126                 case FILE_BELDATE:
00127                         (void)fprintf(stderr, "%s,",
00128                             file_fmttime(m->value.l, 0));
00129                         break;
00130                 default:
00131                         (void) fputs("*bad*", stderr);
00132                         break;
00133                 }
00134         }
00135         (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
00136 }
00137 #endif
00138 
00139 /*VARARGS*/
00140 protected void
00141 file_magwarn(struct magic_set *ms, const char *f, ...)
00142 {
00143         va_list va;
00144         va_start(va, f);
00145 
00146         /* cuz we use stdout for most, stderr here */
00147         (void) fflush(stdout); 
00148 
00149         (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
00150             (unsigned long)ms->line);
00151         (void) vfprintf(stderr, f, va);
00152         va_end(va);
00153         fputc('\n', stderr);
00154 }
00155 
00156 protected const char *
00157 file_fmttime(uint32_t v, int local)
00158 {
00159         char *pp, *rt;
00160         time_t t = (time_t)v;
00161         struct tm *tm;
00162 
00163         if (local) {
00164                 pp = ctime(&t);
00165         } else {
00166 #ifndef HAVE_DAYLIGHT
00167                 private int daylight = 0;
00168 #ifdef HAVE_TM_ISDST
00169                 private time_t now = (time_t)0;
00170 
00171                 if (now == (time_t)0) {
00172                         struct tm *tm1;
00173                         (void)time(&now);
00174                         tm1 = localtime(&now);
00175                         if (tm1 == NULL)
00176                                 return "*Invalid time*";
00177                         daylight = tm1->tm_isdst;
00178                 }
00179 #endif /* HAVE_TM_ISDST */
00180 #endif /* HAVE_DAYLIGHT */
00181                 if (daylight)
00182                         t += 3600;
00183                 tm = gmtime(&t);
00184                 if (tm == NULL)
00185                         return "*Invalid time*";
00186                 pp = asctime(tm);
00187         }
00188 
00189         if ((rt = strchr(pp, '\n')) != NULL)
00190                 *rt = '\0';
00191         return pp;
00192 }

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2