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

1.6     ! mickey      1: /*     $OpenBSD: print.c,v 1.5 1997/08/24 18:33:12 millert 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>
1.6     ! mickey     41: #include <err.h>
1.1       deraadt    42: #include "file.h"
                     43:
                     44: #ifndef lint
1.6     ! mickey     45: static char *moduleid = "$OpenBSD: print.c,v 1.5 1997/08/24 18:33:12 millert Exp $";
1.1       deraadt    46: #endif  /* lint */
                     47:
                     48: #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
                     49:
                     50: void
                     51: mdump(m)
                     52: struct magic *m;
                     53: {
                     54:        static char *typ[] = {   "invalid", "byte", "short", "invalid",
                     55:                                 "long", "string", "date", "beshort",
                     56:                                 "belong", "bedate", "leshort", "lelong",
                     57:                                 "ledate" };
                     58:        (void) fputc('[', stderr);
                     59:        (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
                     60:                       m->offset);
                     61:
                     62:        if (m->flag & INDIR)
1.3       millert    63:                (void) fprintf(stderr, "(%s,%d),",
1.1       deraadt    64:                               (m->in.type >= 0 && m->in.type < SZOF(typ)) ?
                     65:                                        typ[(unsigned char) m->in.type] :
                     66:                                        "*bad*",
                     67:                               m->in.offset);
                     68:
                     69:        (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
                     70:                       (m->type >= 0 && m->type < SZOF(typ)) ?
                     71:                                typ[(unsigned char) m->type] :
                     72:                                "*bad*");
1.5       millert    73:        if (m->mask != ~0)
1.3       millert    74:                (void) fprintf(stderr, " & %.8x", m->mask);
1.1       deraadt    75:
                     76:        (void) fprintf(stderr, ",%c", m->reln);
                     77:
                     78:        if (m->reln != 'x') {
                     79:            switch (m->type) {
                     80:            case BYTE:
                     81:            case SHORT:
                     82:            case LONG:
                     83:            case LESHORT:
                     84:            case LELONG:
                     85:            case BESHORT:
                     86:            case BELONG:
1.3       millert    87:                    (void) fprintf(stderr, "%d", m->value.l);
1.1       deraadt    88:                    break;
                     89:            case STRING:
                     90:                    showstr(stderr, m->value.s, -1);
                     91:                    break;
                     92:            case DATE:
                     93:            case LEDATE:
                     94:            case BEDATE:
                     95:                    {
                     96:                            char *rt, *pp = ctime((time_t*) &m->value.l);
                     97:                            if ((rt = strchr(pp, '\n')) != NULL)
                     98:                                    *rt = '\0';
                     99:                            (void) fprintf(stderr, "%s,", pp);
                    100:                            if (rt)
                    101:                                    *rt = '\n';
                    102:                    }
                    103:                    break;
                    104:            default:
                    105:                    (void) fputs("*bad*", stderr);
                    106:                    break;
                    107:            }
                    108:        }
                    109:        (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
                    110: }
                    111:
                    112: /*
                    113:  * ckfputs - futs, but with error checking
                    114:  * ckfprintf - fprintf, but with error checking
                    115:  */
                    116: void
                    117: ckfputs(str, fil)
                    118:     const char *str;
                    119:     FILE *fil;
                    120: {
                    121:        if (fputs(str,fil) == EOF)
1.6     ! mickey    122:                err(1, "write failed");
1.1       deraadt   123: }
                    124:
                    125: /*VARARGS*/
                    126: void
1.4       mickey    127: #ifdef __STDC__
1.1       deraadt   128: ckfprintf(FILE *f, const char *fmt, ...)
                    129: #else
                    130: ckfprintf(va_alist)
                    131:        va_dcl
                    132: #endif
                    133: {
                    134:        va_list va;
1.4       mickey    135: #ifdef __STDC__
1.1       deraadt   136:        va_start(va, fmt);
                    137: #else
                    138:        FILE *f;
                    139:        const char *fmt;
                    140:        va_start(va);
                    141:        f = va_arg(va, FILE *);
                    142:        fmt = va_arg(va, const char *);
                    143: #endif
                    144:        (void) vfprintf(f, fmt, va);
                    145:        if (ferror(f))
1.6     ! mickey    146:                err(1, "write failed");
1.1       deraadt   147:        va_end(va);
                    148: }