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

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