[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.16

1.16    ! deraadt     1: /*     $OpenBSD: print.c,v 1.15 2009/04/24 18:54:34 chl 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: #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
                     45:
1.11      tedu       46: #ifndef COMPILE_ONLY
                     47: protected void
                     48: file_mdump(struct magic *m)
1.1       deraadt    49: {
1.14      chl        50:        private const char optyp[] = { FILE_OPS };
                     51:
                     52:        (void) fprintf(stderr, "[%u", m->lineno);
                     53:        (void) fprintf(stderr, ">>>>>>>> %u" + 8 - (m->cont_level & 7),
1.1       deraadt    54:                       m->offset);
                     55:
1.11      tedu       56:        if (m->flag & INDIR) {
                     57:                (void) fprintf(stderr, "(%s,",
                     58:                               /* Note: type is unsigned */
1.14      chl        59:                               (m->in_type < file_nnames) ?
                     60:                                        file_names[m->in_type] : "*bad*");
1.11      tedu       61:                if (m->in_op & FILE_OPINVERSE)
                     62:                        (void) fputc('~', stderr);
1.14      chl        63:                (void) fprintf(stderr, "%c%u),",
                     64:                               ((m->in_op & FILE_OPS_MASK) < SZOF(optyp)) ?
                     65:                                        optyp[m->in_op & FILE_OPS_MASK] : '?',
1.11      tedu       66:                                m->in_offset);
                     67:        }
1.1       deraadt    68:        (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
1.11      tedu       69:                       /* Note: type is unsigned */
1.14      chl        70:                       (m->type < file_nnames) ? file_names[m->type] : "*bad*");
1.11      tedu       71:        if (m->mask_op & FILE_OPINVERSE)
                     72:                (void) fputc('~', stderr);
1.14      chl        73:
                     74:        if (IS_STRING(m->type)) {
                     75:                if (m->str_flags) {
                     76:                        (void) fputc('/', stderr);
                     77:                        if (m->str_flags & STRING_COMPACT_BLANK)
1.11      tedu       78:                                (void) fputc(CHAR_COMPACT_BLANK, stderr);
1.14      chl        79:                        if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK)
1.11      tedu       80:                                (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
1.14      chl        81:                                    stderr);
                     82:                        if (m->str_flags & STRING_IGNORE_LOWERCASE)
                     83:                                (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
                     84:                        if (m->str_flags & STRING_IGNORE_UPPERCASE)
                     85:                                (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
                     86:                        if (m->str_flags & REGEX_OFFSET_START)
                     87:                                (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
                     88:                }
1.15      chl        89:                if (m->str_range)
                     90:                        (void) fprintf(stderr, "/%u", m->str_range);
1.14      chl        91:        }
                     92:        else {
                     93:                if ((m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
                     94:                        (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
                     95:                else
                     96:                        (void) fputc('?', stderr);
                     97:
                     98:                if (m->num_mask) {
                     99:                        (void) fprintf(stderr, "%.8llx",
                    100:                            (unsigned long long)m->num_mask);
1.11      tedu      101:                }
                    102:        }
1.1       deraadt   103:        (void) fprintf(stderr, ",%c", m->reln);
                    104:
                    105:        if (m->reln != 'x') {
1.11      tedu      106:                switch (m->type) {
                    107:                case FILE_BYTE:
                    108:                case FILE_SHORT:
                    109:                case FILE_LONG:
                    110:                case FILE_LESHORT:
                    111:                case FILE_LELONG:
1.14      chl       112:                case FILE_MELONG:
1.11      tedu      113:                case FILE_BESHORT:
                    114:                case FILE_BELONG:
                    115:                        (void) fprintf(stderr, "%d", m->value.l);
                    116:                        break;
1.14      chl       117:                case FILE_BEQUAD:
                    118:                case FILE_LEQUAD:
                    119:                case FILE_QUAD:
                    120:                        (void) fprintf(stderr, "%lld",
                    121:                            (unsigned long long)m->value.q);
                    122:                        break;
                    123:                case FILE_PSTRING:
1.11      tedu      124:                case FILE_STRING:
                    125:                case FILE_REGEX:
1.14      chl       126:                case FILE_BESTRING16:
                    127:                case FILE_LESTRING16:
                    128:                case FILE_SEARCH:
                    129:                        file_showstr(stderr, m->value.s, (size_t)m->vallen);
1.11      tedu      130:                        break;
                    131:                case FILE_DATE:
                    132:                case FILE_LEDATE:
                    133:                case FILE_BEDATE:
1.14      chl       134:                case FILE_MEDATE:
1.11      tedu      135:                        (void)fprintf(stderr, "%s,",
                    136:                            file_fmttime(m->value.l, 1));
                    137:                        break;
                    138:                case FILE_LDATE:
                    139:                case FILE_LELDATE:
                    140:                case FILE_BELDATE:
1.14      chl       141:                case FILE_MELDATE:
1.11      tedu      142:                        (void)fprintf(stderr, "%s,",
                    143:                            file_fmttime(m->value.l, 0));
                    144:                        break;
1.14      chl       145:                case FILE_QDATE:
                    146:                case FILE_LEQDATE:
                    147:                case FILE_BEQDATE:
                    148:                        (void)fprintf(stderr, "%s,",
                    149:                            file_fmttime((uint32_t)m->value.q, 1));
                    150:                        break;
                    151:                case FILE_QLDATE:
                    152:                case FILE_LEQLDATE:
                    153:                case FILE_BEQLDATE:
                    154:                        (void)fprintf(stderr, "%s,",
                    155:                            file_fmttime((uint32_t)m->value.q, 0));
                    156:                        break;
1.15      chl       157:                case FILE_FLOAT:
                    158:                case FILE_BEFLOAT:
                    159:                case FILE_LEFLOAT:
                    160:                        (void) fprintf(stderr, "%G", m->value.f);
                    161:                        break;
                    162:                case FILE_DOUBLE:
                    163:                case FILE_BEDOUBLE:
                    164:                case FILE_LEDOUBLE:
                    165:                        (void) fprintf(stderr, "%G", m->value.d);
                    166:                        break;
1.14      chl       167:                case FILE_DEFAULT:
                    168:                        /* XXX - do anything here? */
                    169:                        break;
1.11      tedu      170:                default:
                    171:                        (void) fputs("*bad*", stderr);
                    172:                        break;
                    173:                }
1.1       deraadt   174:        }
                    175:        (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
1.8       ian       176: }
1.11      tedu      177: #endif
1.8       ian       178:
1.11      tedu      179: /*VARARGS*/
                    180: protected void
1.14      chl       181: file_magwarn(struct magic_set *ms, const char *f, ...)
1.8       ian       182: {
                    183:        va_list va;
1.11      tedu      184:
                    185:        /* cuz we use stdout for most, stderr here */
                    186:        (void) fflush(stdout);
1.1       deraadt   187:
1.15      chl       188:        if (ms->file)
                    189:                (void) fprintf(stderr, "%s, %lu: ", ms->file,
                    190:                    (unsigned long)ms->line);
                    191:        (void) fprintf(stderr, "Warning: ");
                    192:        va_start(va, f);
1.11      tedu      193:        (void) vfprintf(stderr, f, va);
                    194:        va_end(va);
1.14      chl       195:        (void) fputc('\n', stderr);
1.1       deraadt   196: }
                    197:
1.14      chl       198: protected const char *
1.11      tedu      199: file_fmttime(uint32_t v, int local)
1.1       deraadt   200: {
1.13      chl       201:        char *pp;
1.11      tedu      202:        time_t t = (time_t)v;
                    203:        struct tm *tm;
                    204:
                    205:        if (local) {
                    206:                pp = ctime(&t);
                    207:        } else {
                    208: #ifndef HAVE_DAYLIGHT
                    209:                private int daylight = 0;
                    210: #ifdef HAVE_TM_ISDST
                    211:                private time_t now = (time_t)0;
                    212:
                    213:                if (now == (time_t)0) {
                    214:                        struct tm *tm1;
                    215:                        (void)time(&now);
                    216:                        tm1 = localtime(&now);
1.14      chl       217:                        if (tm1 == NULL)
                    218:                                return "*Invalid time*";
1.11      tedu      219:                        daylight = tm1->tm_isdst;
                    220:                }
                    221: #endif /* HAVE_TM_ISDST */
                    222: #endif /* HAVE_DAYLIGHT */
                    223:                if (daylight)
                    224:                        t += 3600;
                    225:                tm = gmtime(&t);
1.14      chl       226:                if (tm == NULL)
                    227:                        return "*Invalid time*";
1.11      tedu      228:                pp = asctime(tm);
                    229:        }
1.7       millert   230:
1.12      gilles    231:        pp[strcspn(pp, "\n")] = '\0';
1.11      tedu      232:        return pp;
1.1       deraadt   233: }