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

file/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 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 & 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 & 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  * error - print best error message possible and exit
00137  */
00138 /*VARARGS*/
00139 void
00140 error(int status, /*@unused@*/ int errnum, const char * format, ...)
00141 {
00142         va_list va;
00143 
00144         va_start(va,  format);
00145         /* cuz we use stdout for most, stderr here */
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 /*VARARGS*/
00160 void
00161 magwarn(const char *f, ...)
00162 {
00163         fmagic fm = global_fmagic;
00164         va_list va;
00165 
00166         va_start(va, f);
00167         /* cuz we use stdout for most, stderr here */
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 /*@-globstate@*/ /* FIX: __progname might be null */
00177         return;
00178 /*@=globstate@*/
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 /*@-boundswrite@*/
00190         rc = vsnprintf(fm->obp, fm->nob, f, va);
00191 /*@=boundswrite@*/
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 /* HAVE_TM_ISDST */
00225 #endif /* HAVE_DAYLIGHT */
00226                 if (daylight)
00227                         t += 3600;
00228                 tm = gmtime(&t);
00229                 if (tm != NULL)
00230                         pp = asctime(tm);
00231         }
00232 
00233 /*@-modobserver@*/
00234         if ((rt = strchr(pp, '\n')) != NULL)
00235                 *rt = '\0';
00236 /*@=modobserver@*/
00237         return pp;
00238 }
00239 #endif

Generated on Sun Oct 26 13:01:58 2003 for rpm by doxygen1.2.18