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

1.5     ! millert     1: /*     $OpenBSD: print.c,v 1.4 1997/07/25 21:05:26 mickey Exp $        */
1.3       millert     2:
1.1       deraadt     3: /*
                      4:  * print.c - debugging printout routines
                      5:  *
                      6:  * Copyright (c) Ian F. Darwin, 1987.
                      7:  * Written by Ian F. Darwin.
                      8:  *
                      9:  * This software is not subject to any license of the American Telephone
                     10:  * and Telegraph Company or of the Regents of the University of California.
                     11:  *
                     12:  * Permission is granted to anyone to use this software for any purpose on
                     13:  * any computer system, and to alter it and redistribute it freely, subject
                     14:  * to the following restrictions:
                     15:  *
                     16:  * 1. The author is not responsible for the consequences of use of this
                     17:  *    software, no matter how awful, even if they arise from flaws in it.
                     18:  *
                     19:  * 2. The origin of this software must not be misrepresented, either by
                     20:  *    explicit claim or by omission.  Since few users ever read sources,
                     21:  *    credits must appear in the documentation.
                     22:  *
                     23:  * 3. Altered versions must be plainly marked as such, and must not be
                     24:  *    misrepresented as being the original software.  Since few users
                     25:  *    ever read sources, credits must appear in the documentation.
                     26:  *
                     27:  * 4. This notice may not be removed or altered.
                     28:  */
                     29:
                     30: #include <stdio.h>
                     31: #include <errno.h>
                     32: #include <string.h>
1.4       mickey     33: #ifdef __STDC__
1.1       deraadt    34: # include <stdarg.h>
                     35: #else
                     36: # include <varargs.h>
                     37: #endif
                     38: #include <stdlib.h>
                     39: #include <unistd.h>
                     40: #include <time.h>
                     41: #include "file.h"
                     42:
                     43: #ifndef lint
1.5     ! millert    44: static char *moduleid = "$OpenBSD: print.c,v 1.4 1997/07/25 21:05:26 mickey Exp $";
1.1       deraadt    45: #endif  /* lint */
                     46:
                     47: #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
                     48:
                     49: void
                     50: mdump(m)
                     51: struct magic *m;
                     52: {
                     53:        static char *typ[] = {   "invalid", "byte", "short", "invalid",
                     54:                                 "long", "string", "date", "beshort",
                     55:                                 "belong", "bedate", "leshort", "lelong",
                     56:                                 "ledate" };
                     57:        (void) fputc('[', stderr);
                     58:        (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
                     59:                       m->offset);
                     60:
                     61:        if (m->flag & INDIR)
1.3       millert    62:                (void) fprintf(stderr, "(%s,%d),",
1.1       deraadt    63:                               (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
                     64:                                        typ[(unsigned char) m->in.type] :
                     65:                                        "*bad*",
                     66:                               m->in.offset);
                     67:
                     68:        (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
                     69:                       (m->type >= 0 && m->type < SZOF(typ)) ?
                     70:                                typ[(unsigned char) m->type] :
                     71:                                "*bad*");
1.5     ! millert    72:        if (m->mask != ~0)
1.3       millert    73:                (void) fprintf(stderr, " & %.8x", m->mask);
1.1       deraadt    74:
                     75:        (void) fprintf(stderr, ",%c", m->reln);
                     76:
                     77:        if (m->reln != 'x') {
                     78:            switch (m->type) {
                     79:            case BYTE:
                     80:            case SHORT:
                     81:            case LONG:
                     82:            case LESHORT:
                     83:            case LELONG:
                     84:            case BESHORT:
                     85:            case BELONG:
1.3       millert    86:                    (void) fprintf(stderr, "%d", m->value.l);
1.1       deraadt    87:                    break;
                     88:            case STRING:
                     89:                    showstr(stderr, m->value.s, -1);
                     90:                    break;
                     91:            case DATE:
                     92:            case LEDATE:
                     93:            case BEDATE:
                     94:                    {
                     95:                            char *rt, *pp = ctime((time_t*) &m->value.l);
                     96:                            if ((rt = strchr(pp, '\n')) != NULL)
                     97:                                    *rt = '\0';
                     98:                            (void) fprintf(stderr, "%s,", pp);
                     99:                            if (rt)
                    100:                                    *rt = '\n';
                    101:                    }
                    102:                    break;
                    103:            default:
                    104:                    (void) fputs("*bad*", stderr);
                    105:                    break;
                    106:            }
                    107:        }
                    108:        (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
                    109: }
                    110:
                    111: /*
                    112:  * ckfputs - futs, but with error checking
                    113:  * ckfprintf - fprintf, but with error checking
                    114:  */
                    115: void
                    116: ckfputs(str, fil)
                    117:     const char *str;
                    118:     FILE *fil;
                    119: {
                    120:        if (fputs(str,fil) == EOF)
                    121:                error("write failed.\n");
                    122: }
                    123:
                    124: /*VARARGS*/
                    125: void
1.4       mickey    126: #ifdef __STDC__
1.1       deraadt   127: ckfprintf(FILE *f, const char *fmt, ...)
                    128: #else
                    129: ckfprintf(va_alist)
                    130:        va_dcl
                    131: #endif
                    132: {
                    133:        va_list va;
1.4       mickey    134: #ifdef __STDC__
1.1       deraadt   135:        va_start(va, fmt);
                    136: #else
                    137:        FILE *f;
                    138:        const char *fmt;
                    139:        va_start(va);
                    140:        f = va_arg(va, FILE *);
                    141:        fmt = va_arg(va, const char *);
                    142: #endif
                    143:        (void) vfprintf(f, fmt, va);
                    144:        if (ferror(f))
                    145:                error("write failed.\n");
                    146:        va_end(va);
                    147: }
                    148:
                    149: /*
                    150:  * error - print best error message possible and exit
                    151:  */
                    152: /*VARARGS*/
                    153: void
1.4       mickey    154: #ifdef __STDC__
1.1       deraadt   155: error(const char *f, ...)
                    156: #else
                    157: error(va_alist)
                    158:        va_dcl
                    159: #endif
                    160: {
                    161:        va_list va;
1.4       mickey    162: #ifdef __STDC__
1.1       deraadt   163:        va_start(va, f);
                    164: #else
                    165:        const char *f;
                    166:        va_start(va);
                    167:        f = va_arg(va, const char *);
                    168: #endif
                    169:        /* cuz we use stdout for most, stderr here */
                    170:        (void) fflush(stdout);
                    171:
                    172:        if (progname != NULL)
                    173:                (void) fprintf(stderr, "%s: ", progname);
                    174:        (void) vfprintf(stderr, f, va);
                    175:        va_end(va);
                    176:        exit(1);
                    177: }
                    178:
                    179: /*VARARGS*/
                    180: void
1.4       mickey    181: #ifdef __STDC__
1.1       deraadt   182: magwarn(const char *f, ...)
                    183: #else
                    184: magwarn(va_alist)
                    185:        va_dcl
                    186: #endif
                    187: {
                    188:        va_list va;
1.4       mickey    189: #ifdef __STDC__
1.1       deraadt   190:        va_start(va, f);
                    191: #else
                    192:        const char *f;
                    193:        va_start(va);
                    194:        f = va_arg(va, const char *);
                    195: #endif
                    196:        /* cuz we use stdout for most, stderr here */
                    197:        (void) fflush(stdout);
                    198:
                    199:        if (progname != NULL)
                    200:                (void) fprintf(stderr, "%s: %s, %d: ",
                    201:                               progname, magicfile, lineno);
                    202:        (void) vfprintf(stderr, f, va);
                    203:        va_end(va);
                    204:        fputc('\n', stderr);
                    205: }