[BACK]Return to print.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/print.c, Revision 1.12

1.12    ! gilles      1: /*     $OpenBSD: print.c,v 1.11 2004/05/19 02:32:35 tedu Exp $ */
1.1       deraadt     2: /*
1.9       ian         3:  * Copyright (c) Ian F. Darwin 1986-1995.
                      4:  * Software written by Ian F. Darwin and others;
                      5:  * maintained 1995-present by Christos Zoulas and others.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice immediately at the beginning of the file, without modification,
                     12:  *    this list of conditions, and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
                     21:  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
1.1       deraadt    28:  */
1.11      tedu       29: /*
                     30:  * print.c - debugging printout routines
                     31:  */
1.1       deraadt    32:
1.11      tedu       33: #include "file.h"
1.1       deraadt    34: #include <stdio.h>
                     35: #include <errno.h>
                     36: #include <string.h>
1.7       millert    37: #include <stdarg.h>
1.1       deraadt    38: #include <stdlib.h>
1.11      tedu       39: #ifdef HAVE_UNISTD_H
1.1       deraadt    40: #include <unistd.h>
1.11      tedu       41: #endif
1.1       deraadt    42: #include <time.h>
                     43:
                     44: #ifndef lint
1.12    ! gilles     45: FILE_RCSID("@(#)$Id: print.c,v 1.11 2004/05/19 02:32:35 tedu Exp $")
1.1       deraadt    46: #endif  /* lint */
                     47:
                     48: #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
                     49:
1.11      tedu       50: #ifndef COMPILE_ONLY
                     51: protected void
                     52: file_mdump(struct magic *m)
1.1       deraadt    53: {
1.11      tedu       54:        private const char *typ[] = { "invalid", "byte", "short", "invalid",
                     55:                                     "long", "string", "date", "beshort",
                     56:                                     "belong", "bedate", "leshort", "lelong",
                     57:                                     "ledate", "pstring", "ldate", "beldate",
                     58:                                     "leldate", "regex" };
                     59:        private const char optyp[] = { '@', '&', '|', '^', '+', '-',
                     60:                                      '*', '/', '%' };
1.1       deraadt    61:        (void) fputc('[', stderr);
                     62:        (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
                     63:                       m->offset);
                     64:
1.11      tedu       65:        if (m->flag & INDIR) {
                     66:                (void) fprintf(stderr, "(%s,",
                     67:                               /* Note: type is unsigned */
                     68:                               (m->in_type < SZOF(typ)) ?
                     69:                                        typ[m->in_type] : "*bad*");
                     70:                if (m->in_op & FILE_OPINVERSE)
                     71:                        (void) fputc('~', stderr);
                     72:                (void) fprintf(stderr, "%c%d),",
                     73:                               ((m->in_op&0x7F) < SZOF(optyp)) ?
                     74:                                        optyp[m->in_op&0x7F] : '?',
                     75:                                m->in_offset);
                     76:        }
1.1       deraadt    77:        (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
1.11      tedu       78:                       /* Note: type is unsigned */
                     79:                       (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
                     80:        if (m->mask_op & FILE_OPINVERSE)
                     81:                (void) fputc('~', stderr);
                     82:        if (m->mask) {
                     83:                if ((m->mask_op & 0x7F) < SZOF(optyp))
                     84:                        fputc(optyp[m->mask_op&0x7F], stderr);
                     85:                else
                     86:                        fputc('?', stderr);
                     87:                if(FILE_STRING != m->type || FILE_PSTRING != m->type)
                     88:                        (void) fprintf(stderr, "%.8x", m->mask);
                     89:                else {
                     90:                        if (m->mask & STRING_IGNORE_LOWERCASE)
                     91:                                (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
                     92:                        if (m->mask & STRING_COMPACT_BLANK)
                     93:                                (void) fputc(CHAR_COMPACT_BLANK, stderr);
                     94:                        if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
                     95:                                (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
                     96:                                stderr);
                     97:                }
                     98:        }
1.1       deraadt    99:
                    100:        (void) fprintf(stderr, ",%c", m->reln);
                    101:
                    102:        if (m->reln != 'x') {
1.11      tedu      103:                switch (m->type) {
                    104:                case FILE_BYTE:
                    105:                case FILE_SHORT:
                    106:                case FILE_LONG:
                    107:                case FILE_LESHORT:
                    108:                case FILE_LELONG:
                    109:                case FILE_BESHORT:
                    110:                case FILE_BELONG:
                    111:                        (void) fprintf(stderr, "%d", m->value.l);
                    112:                        break;
                    113:                case FILE_STRING:
                    114:                case FILE_PSTRING:
                    115:                case FILE_REGEX:
                    116:                        file_showstr(stderr, m->value.s, ~0U);
                    117:                        break;
                    118:                case FILE_DATE:
                    119:                case FILE_LEDATE:
                    120:                case FILE_BEDATE:
                    121:                        (void)fprintf(stderr, "%s,",
                    122:                            file_fmttime(m->value.l, 1));
                    123:                        break;
                    124:                case FILE_LDATE:
                    125:                case FILE_LELDATE:
                    126:                case FILE_BELDATE:
                    127:                        (void)fprintf(stderr, "%s,",
                    128:                            file_fmttime(m->value.l, 0));
                    129:                        break;
                    130:                default:
                    131:                        (void) fputs("*bad*", stderr);
                    132:                        break;
                    133:                }
1.1       deraadt   134:        }
                    135:        (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
1.8       ian       136: }
1.11      tedu      137: #endif
1.8       ian       138:
1.11      tedu      139: /*VARARGS*/
                    140: protected void
                    141: file_magwarn(const char *f, ...)
1.8       ian       142: {
                    143:        va_list va;
1.11      tedu      144:        va_start(va, f);
                    145:
                    146:        /* cuz we use stdout for most, stderr here */
                    147:        (void) fflush(stdout);
1.1       deraadt   148:
1.11      tedu      149:        (void) fprintf(stderr, "WARNING: ");
                    150:        (void) vfprintf(stderr, f, va);
                    151:        va_end(va);
                    152:        fputc('\n', stderr);
1.1       deraadt   153: }
                    154:
1.11      tedu      155: protected char *
                    156: file_fmttime(uint32_t v, int local)
1.1       deraadt   157: {
1.11      tedu      158:        char *pp, *rt;
                    159:        time_t t = (time_t)v;
                    160:        struct tm *tm;
                    161:
                    162:        if (local) {
                    163:                pp = ctime(&t);
                    164:        } else {
                    165: #ifndef HAVE_DAYLIGHT
                    166:                private int daylight = 0;
                    167: #ifdef HAVE_TM_ISDST
                    168:                private time_t now = (time_t)0;
                    169:
                    170:                if (now == (time_t)0) {
                    171:                        struct tm *tm1;
                    172:                        (void)time(&now);
                    173:                        tm1 = localtime(&now);
                    174:                        daylight = tm1->tm_isdst;
                    175:                }
                    176: #endif /* HAVE_TM_ISDST */
                    177: #endif /* HAVE_DAYLIGHT */
                    178:                if (daylight)
                    179:                        t += 3600;
                    180:                tm = gmtime(&t);
                    181:                pp = asctime(tm);
                    182:        }
1.7       millert   183:
1.12    ! gilles    184:        pp[strcspn(pp, "\n")] = '\0';
1.11      tedu      185:        return pp;
1.1       deraadt   186: }