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

Annotation of src/usr.bin/hexdump/display.c, Revision 1.13

1.13    ! millert     1: /*     $OpenBSD: display.c,v 1.12 2003/06/12 20:58:09 deraadt Exp $    */
1.9       pvalchev    2: /*     $NetBSD: display.c,v 1.12 2001/12/07 15:14:29 bjh21 Exp $       */
1.3       deraadt     3:
1.1       deraadt     4: /*
1.9       pvalchev    5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
1.1       deraadt     7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, 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.
1.11      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: /*static char sccsid[] = "from: @(#)display.c  5.11 (Berkeley) 3/9/91";*/
1.13    ! millert    35: static char rcsid[] = "$OpenBSD: display.c,v 1.12 2003/06/12 20:58:09 deraadt Exp $";
1.1       deraadt    36: #endif /* not lint */
                     37:
                     38: #include <sys/param.h>
                     39: #include <sys/stat.h>
1.9       pvalchev   40:
                     41: #include <ctype.h>
                     42: #include <err.h>
1.1       deraadt    43: #include <errno.h>
                     44: #include <stdio.h>
                     45: #include <stdlib.h>
                     46: #include <string.h>
1.9       pvalchev   47: #include <unistd.h>
                     48:
1.1       deraadt    49: #include "hexdump.h"
                     50:
                     51: enum _vflag vflag = FIRST;
                     52:
                     53: static off_t address;                  /* address/offset in stream */
                     54: static off_t eaddress;                 /* end address */
                     55:
1.10      millert    56: static inline void print(PR *, u_char *);
1.1       deraadt    57:
1.6       deraadt    58: void
1.12      deraadt    59: display(void)
1.1       deraadt    60: {
1.8       mpech      61:        FS *fs;
                     62:        FU *fu;
                     63:        PR *pr;
                     64:        int cnt;
                     65:        u_char *bp;
1.1       deraadt    66:        off_t saveaddress;
1.9       pvalchev   67:        u_char savech, *savebp;
1.1       deraadt    68:
1.9       pvalchev   69:        savech = 0;
                     70:        while ((bp = get()) != NULL)
1.1       deraadt    71:            for (fs = fshead, savebp = bp, saveaddress = address; fs;
                     72:                fs = fs->nextfs, bp = savebp, address = saveaddress)
                     73:                    for (fu = fs->nextfu; fu; fu = fu->nextfu) {
                     74:                        if (fu->flags&F_IGNORE)
                     75:                                break;
                     76:                        for (cnt = fu->reps; cnt; --cnt)
                     77:                            for (pr = fu->nextpr; pr; address += pr->bcnt,
                     78:                                bp += pr->bcnt, pr = pr->nextpr) {
                     79:                                    if (eaddress && address >= eaddress &&
1.9       pvalchev   80:                                        !(pr->flags & (F_TEXT|F_BPAD)))
1.1       deraadt    81:                                            bpad(pr);
                     82:                                    if (cnt == 1 && pr->nospace) {
                     83:                                        savech = *pr->nospace;
                     84:                                        *pr->nospace = '\0';
                     85:                                    }
1.9       pvalchev   86:                                    print(pr, bp);
1.1       deraadt    87:                                    if (cnt == 1 && pr->nospace)
                     88:                                        *pr->nospace = savech;
                     89:                            }
                     90:                    }
                     91:        if (endfu) {
                     92:                /*
1.9       pvalchev   93:                 * If eaddress not set, error or file size was multiple of
1.1       deraadt    94:                 * blocksize, and no partial block ever found.
                     95:                 */
                     96:                if (!eaddress) {
                     97:                        if (!address)
                     98:                                return;
                     99:                        eaddress = address;
                    100:                }
                    101:                for (pr = endfu->nextpr; pr; pr = pr->nextpr)
                    102:                        switch(pr->flags) {
                    103:                        case F_ADDRESS:
1.9       pvalchev  104:                                (void)printf(pr->fmt, (quad_t)eaddress);
1.1       deraadt   105:                                break;
                    106:                        case F_TEXT:
1.9       pvalchev  107:                                (void)printf("%s", pr->fmt);
1.1       deraadt   108:                                break;
                    109:                        }
                    110:        }
                    111: }
                    112:
1.9       pvalchev  113: static inline void
1.12      deraadt   114: print(PR *pr, u_char *bp)
1.9       pvalchev  115: {
                    116:           double f8;
                    117:            float f4;
                    118:          int16_t s2;
                    119:          int32_t s4;
                    120:          int64_t s8;
                    121:        u_int16_t u2;
                    122:        u_int32_t u4;
                    123:        u_int64_t u8;
                    124:
                    125:        switch(pr->flags) {
                    126:        case F_ADDRESS:
                    127:                (void)printf(pr->fmt, (quad_t)address);
                    128:                break;
                    129:        case F_BPAD:
                    130:                (void)printf(pr->fmt, "");
                    131:                break;
                    132:        case F_C:
                    133:                conv_c(pr, bp);
                    134:                break;
                    135:        case F_CHAR:
                    136:                (void)printf(pr->fmt, *bp);
                    137:                break;
                    138:        case F_DBL:
                    139:                switch(pr->bcnt) {
                    140:                case 4:
                    141:                        memmove(&f4, bp, sizeof(f4));
                    142:                        (void)printf(pr->fmt, f4);
                    143:                        break;
                    144:                case 8:
                    145:                        memmove(&f8, bp, sizeof(f8));
                    146:                        (void)printf(pr->fmt, f8);
                    147:                        break;
                    148:                }
                    149:                break;
                    150:        case F_INT:
                    151:                switch(pr->bcnt) {
                    152:                case 1:
                    153:                        (void)printf(pr->fmt, (quad_t)*bp);
                    154:                        break;
                    155:                case 2:
                    156:                        memmove(&s2, bp, sizeof(s2));
                    157:                        (void)printf(pr->fmt, (quad_t)s2);
                    158:                        break;
                    159:                case 4:
                    160:                        memmove(&s4, bp, sizeof(s4));
                    161:                        (void)printf(pr->fmt, (quad_t)s4);
                    162:                        break;
                    163:                case 8:
                    164:                        memmove(&s8, bp, sizeof(s8));
                    165:                        (void)printf(pr->fmt, s8);
                    166:                        break;
                    167:                }
                    168:                break;
                    169:        case F_P:
                    170:                (void)printf(pr->fmt, isprint(*bp) ? *bp : '.');
                    171:                break;
                    172:        case F_STR:
                    173:                (void)printf(pr->fmt, (char *)bp);
                    174:                break;
                    175:        case F_TEXT:
                    176:                (void)printf("%s", pr->fmt);
                    177:                break;
                    178:        case F_U:
                    179:                conv_u(pr, bp);
                    180:                break;
                    181:        case F_UINT:
                    182:                switch(pr->bcnt) {
                    183:                case 1:
                    184:                        (void)printf(pr->fmt, (u_quad_t)*bp);
                    185:                        break;
                    186:                case 2:
                    187:                        memmove(&u2, bp, sizeof(u2));
                    188:                        (void)printf(pr->fmt, (u_quad_t)u2);
                    189:                        break;
                    190:                case 4:
                    191:                        memmove(&u4, bp, sizeof(u4));
                    192:                        (void)printf(pr->fmt, (u_quad_t)u4);
                    193:                        break;
                    194:                case 8:
                    195:                        memmove(&u8, bp, sizeof(u8));
                    196:                        (void)printf(pr->fmt, u8);
                    197:                        break;
                    198:                }
                    199:                break;
                    200:        }
                    201: }
                    202:
1.6       deraadt   203: void
1.12      deraadt   204: bpad(PR *pr)
1.1       deraadt   205: {
1.9       pvalchev  206:        static const char *spec = " -0+#";
1.8       mpech     207:        char *p1, *p2;
1.1       deraadt   208:
                    209:        /*
1.9       pvalchev  210:         * Remove all conversion flags; '-' is the only one valid
1.1       deraadt   211:         * with %s, and it's not useful here.
                    212:         */
                    213:        pr->flags = F_BPAD;
1.9       pvalchev  214:        pr->cchar[0] = 's';
                    215:        pr->cchar[1] = '\0';
1.1       deraadt   216:        for (p1 = pr->fmt; *p1 != '%'; ++p1);
1.4       millert   217:        for (p2 = ++p1; *p1 && strchr(spec, *p1); ++p1);
1.9       pvalchev  218:        while ((*p2++ = *p1++) != '\0');
1.1       deraadt   219: }
                    220:
                    221: static char **_argv;
                    222:
                    223: u_char *
1.12      deraadt   224: get(void)
1.1       deraadt   225: {
                    226:        static int ateof = 1;
                    227:        static u_char *curp, *savp;
1.8       mpech     228:        int n;
1.1       deraadt   229:        int need, nread;
                    230:        u_char *tmpp;
                    231:
                    232:        if (!curp) {
1.9       pvalchev  233:                curp = emalloc(blocksize);
                    234:                savp = emalloc(blocksize);
1.1       deraadt   235:        } else {
                    236:                tmpp = curp;
                    237:                curp = savp;
                    238:                savp = tmpp;
1.9       pvalchev  239:                address += blocksize;
1.1       deraadt   240:        }
                    241:        for (need = blocksize, nread = 0;;) {
                    242:                /*
                    243:                 * if read the right number of bytes, or at EOF for one file,
                    244:                 * and no other files are available, zero-pad the rest of the
                    245:                 * block and set the end flag.
                    246:                 */
1.9       pvalchev  247:                if (!length || (ateof && !next(NULL))) {
1.1       deraadt   248:                        if (need == blocksize)
1.9       pvalchev  249:                                return(NULL);
                    250:                        if (!need && vflag != ALL &&
                    251:                            !memcmp(curp, savp, nread)) {
1.1       deraadt   252:                                if (vflag != DUP)
                    253:                                        (void)printf("*\n");
1.9       pvalchev  254:                                return(NULL);
1.1       deraadt   255:                        }
1.9       pvalchev  256:                        memset((char *)curp + nread, 0, need);
1.1       deraadt   257:                        eaddress = address + nread;
                    258:                        return(curp);
                    259:                }
                    260:                n = fread((char *)curp + nread, sizeof(u_char),
                    261:                    length == -1 ? need : MIN(length, need), stdin);
                    262:                if (!n) {
                    263:                        if (ferror(stdin))
1.7       mickey    264:                                warn("%s", _argv[-1]);
1.1       deraadt   265:                        ateof = 1;
                    266:                        continue;
                    267:                }
                    268:                ateof = 0;
                    269:                if (length != -1)
                    270:                        length -= n;
                    271:                if (!(need -= n)) {
1.9       pvalchev  272:                        if (vflag == ALL || vflag == FIRST ||
                    273:                            memcmp(curp, savp, blocksize)) {
1.1       deraadt   274:                                if (vflag == DUP || vflag == FIRST)
                    275:                                        vflag = WAIT;
                    276:                                return(curp);
                    277:                        }
                    278:                        if (vflag == WAIT)
                    279:                                (void)printf("*\n");
                    280:                        vflag = DUP;
1.9       pvalchev  281:                        address += blocksize;
1.1       deraadt   282:                        need = blocksize;
                    283:                        nread = 0;
                    284:                }
                    285:                else
                    286:                        nread += n;
                    287:        }
                    288: }
                    289:
1.6       deraadt   290: int
1.12      deraadt   291: next(char **argv)
1.1       deraadt   292: {
                    293:        static int done;
                    294:        int statok;
                    295:
                    296:        if (argv) {
                    297:                _argv = argv;
                    298:                return(1);
                    299:        }
                    300:        for (;;) {
                    301:                if (*_argv) {
                    302:                        if (!(freopen(*_argv, "r", stdin))) {
1.7       mickey    303:                                warn("%s", *_argv);
1.1       deraadt   304:                                exitval = 1;
                    305:                                ++_argv;
                    306:                                continue;
                    307:                        }
                    308:                        statok = done = 1;
                    309:                } else {
                    310:                        if (done++)
                    311:                                return(0);
                    312:                        statok = 0;
                    313:                }
                    314:                if (skip)
                    315:                        doskip(statok ? *_argv : "stdin", statok);
                    316:                if (*_argv)
                    317:                        ++_argv;
                    318:                if (!skip)
                    319:                        return(1);
                    320:        }
                    321:        /* NOTREACHED */
                    322: }
                    323:
1.6       deraadt   324: void
1.12      deraadt   325: doskip(const char *fname, int statok)
1.1       deraadt   326: {
1.9       pvalchev  327:        int cnt;
                    328:        struct stat sb;
1.1       deraadt   329:
                    330:        if (statok) {
1.9       pvalchev  331:                if (fstat(fileno(stdin), &sb))
                    332:                        err(1, "fstat %s", fname);
                    333:                if (S_ISREG(sb.st_mode) && skip >= sb.st_size) {
                    334:                        address += sb.st_size;
                    335:                        skip -= sb.st_size;
1.1       deraadt   336:                        return;
                    337:                }
                    338:        }
1.9       pvalchev  339:        if (S_ISREG(sb.st_mode)) {
1.13    ! millert   340:                if (fseeko(stdin, skip, SEEK_SET))
        !           341:                        err(1, "fseeko %s", fname);
1.9       pvalchev  342:                address += skip;
                    343:                skip = 0;
                    344:        } else {
                    345:                for (cnt = 0; cnt < skip; ++cnt)
                    346:                        if (getchar() == EOF)
                    347:                                break;
                    348:                address += cnt;
                    349:                skip -= cnt;
                    350:        }
1.1       deraadt   351: }
                    352:
1.9       pvalchev  353: void *
1.12      deraadt   354: emalloc(int allocsize)
1.1       deraadt   355: {
1.9       pvalchev  356:        void *p;
1.1       deraadt   357:
1.9       pvalchev  358:        if ((p = malloc((u_int)allocsize)) == NULL)
                    359:                nomem();
                    360:        memset(p, 0, allocsize);
1.1       deraadt   361:        return(p);
1.9       pvalchev  362: }
                    363:
                    364: void
1.12      deraadt   365: nomem(void)
1.9       pvalchev  366: {
                    367:        err(1, NULL);
1.1       deraadt   368: }