Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

file/src/print.c

Go to the documentation of this file.
00001 /*
00002  * print.c - debugging printout routines
00003  *
00004  * Copyright (c) Ian F. Darwin, 1987.
00005  * Written by Ian F. Darwin.
00006  *
00007  * This software is not subject to any license of the American Telephone
00008  * and Telegraph Company or of the Regents of the University of California.
00009  *
00010  * Permission is granted to anyone to use this software for any purpose on
00011  * any computer system, and to alter it and redistribute it freely, subject
00012  * to the following restrictions:
00013  *
00014  * 1. The author is not responsible for the consequences of use of this
00015  *    software, no matter how awful, even if they arise from flaws in it.
00016  *
00017  * 2. The origin of this software must not be misrepresented, either by
00018  *    explicit claim or by omission.  Since few users ever read sources,
00019  *    credits must appear in the documentation.
00020  *
00021  * 3. Altered versions must be plainly marked as such, and must not be
00022  *    misrepresented as being the original software.  Since few users
00023  *    ever read sources, credits must appear in the documentation.
00024  *
00025  * 4. This notice may not be removed or altered.
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 /*@access fmagic @*/
00035 
00036 /*@-compmempass@*/
00037 /*@unchecked@*/
00038 static struct fmagic_s myfmagic;
00039 /*@unchecked@*/
00040 fmagic global_fmagic = &myfmagic;
00041 /*@=compmempass@*/
00042 
00043 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
00044 
00045 #ifndef COMPILE_ONLY
00046 void
00047 file_mdump(struct magic *m)
00048 {
00049         /*@observer@*/
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 /*@-formatconst@*/
00059         (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
00060                        m->offset);
00061 /*@=formatconst@*/
00062 
00063         if (m->flag & INDIR) {
00064                 (void) fprintf(stderr, "(%s,",
00065                                /* Note: type is unsigned */
00066                                (m->in_type < SZOF(typ)) ? 
00067                                         typ[m->in_type] : "*bad*");
00068                 if (m->in_op & FILE_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                        /* Note: type is unsigned */
00077                        (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
00078         if (m->mask_op & FILE_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 (m->type != FILE_STRING || m->type != FILE_PSTRING)
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 FILE_BYTE:
00102                 case FILE_SHORT:
00103                 case FILE_LONG:
00104                 case FILE_LESHORT:
00105                 case FILE_LELONG:
00106                 case FILE_BESHORT:
00107                 case FILE_BELONG:
00108                         (void) fprintf(stderr, "%d", m->value.l);
00109                         break;
00110                 case FILE_STRING:
00111                 case FILE_PSTRING:
00112                 case FILE_REGEX:
00113                         file_showstr(stderr, m->value.s, ~0U);
00114                         break;
00115                 case FILE_DATE:
00116                 case FILE_LEDATE:
00117                 case FILE_BEDATE:
00118                         (void)fprintf(stderr, "%s,",
00119                             file_fmttime(m->value.l, 1));
00120                         break;
00121                 case FILE_LDATE:
00122                 case FILE_LELDATE:
00123                 case FILE_BELDATE:
00124                         (void)fprintf(stderr, "%s,",
00125                             file_fmttime(m->value.l, 0));
00126                         break;
00127                 default:
00128                         (void) fputs("*bad*", stderr);
00129                         break;
00130                 }
00131         }
00132         (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
00133 }
00134 #endif
00135 
00136 #if !defined(HAVE_ERROR) || defined(__LCLINT__)
00137 /*
00138  * error - print best error message possible and exit
00139  */
00140 /*VARARGS*/
00141 void
00142 error(int status, /*@unused@*/ int errnum, const char * format, ...)
00143 {
00144         va_list va;
00145 
00146         va_start(va,  format);
00147         /* cuz we use stdout for most, stderr here */
00148         (void) fflush(stdout); 
00149 
00150         if (__progname != NULL) 
00151                 (void) fprintf(stderr, "%s: ", __progname);
00152         (void) vfprintf(stderr,  format, va);
00153         va_end(va);
00154 #if NOTYET
00155         if (status)
00156 #endif
00157                 exit(status);
00158 }
00159 #endif
00160 
00161 /*VARARGS*/
00162 void
00163 file_magwarn(const char *f, ...)
00164 {
00165         fmagic fm = global_fmagic;
00166         va_list va;
00167 
00168         va_start(va, f);
00169         /* cuz we use stdout for most, stderr here */
00170         (void) fflush(stdout); 
00171 
00172         if (__progname != NULL) 
00173                 (void) fprintf(stderr, "%s: %s, %d: ", 
00174                                __progname, fm->magicfile, fm->lineno);
00175         (void) vfprintf(stderr, f, va);
00176         va_end(va);
00177         (void) fputc('\n', stderr);
00178 /*@-globstate@*/ /* FIX: __progname might be null */
00179         return;
00180 /*@=globstate@*/
00181 }
00182 
00183 void
00184 file_printf(const fmagic fm, const char *f, ...)
00185 {
00186         va_list va;
00187         size_t nob;
00188         int rc;
00189 
00190         va_start(va, f);
00191 /*@-boundswrite@*/
00192         rc = vsnprintf(fm->obp, fm->nob, f, va);
00193 /*@=boundswrite@*/
00194         va_end(va);
00195 
00196         fm->obuf[sizeof(fm->obuf)-1] = '\0';
00197         nob = strlen(fm->obp);
00198         fm->obp += nob;
00199         fm->nob -= nob;
00200 }
00201 
00202 
00203 #ifndef COMPILE_ONLY
00204 const char *
00205 file_fmttime(uint32_t v, int local)
00206 {
00207         char *pp = "???";
00208         char *rt;
00209         time_t t = (time_t)v;
00210         struct tm *tm;
00211 
00212         if (local) {
00213                 pp = ctime(&t);
00214         } else {
00215 #if !defined(HAVE_DAYLIGHT) && !defined(__LCLINT__)
00216                 static int daylight = 0;
00217 #ifdef HAVE_TM_ISDST
00218                 static time_t now = (time_t)0;
00219 
00220                 if (now == (time_t)0) {
00221                         struct tm *tm1;
00222                         (void)time(&now);
00223                         tm1 = localtime(&now);
00224                         daylight = tm1->tm_isdst;
00225                 }
00226 #endif /* HAVE_TM_ISDST */
00227 #endif /* HAVE_DAYLIGHT */
00228                 if (daylight)
00229                         t += 3600;
00230                 tm = gmtime(&t);
00231                 if (tm != NULL)
00232                         pp = asctime(tm);
00233         }
00234 
00235 /*@-modobserver@*/
00236         if ((rt = strchr(pp, '\n')) != NULL)
00237                 *rt = '\0';
00238 /*@=modobserver@*/
00239         return pp;
00240 }
00241 #endif

Generated on Sat Jun 17 22:55:29 2006 for rpm by doxygen 1.3.5