home *** CD-ROM | disk | FTP | other *** search
- /*
- ** HEX FILE DUMP UTILITY
- ** Copyright 1987-89 Steven E. Margison
- ** Displays any file in hex and character representation
- ** in color or monochrome, depending upon type of video
- ** card installed. Displays 20 lines per screen and waits
- ** for a keypress.
- **
- ** Rev 1.10 02-25-89 for Turbo-C 2.0 S.E. Margison
- **
- ** Modified 1989 by Bob Stout
- **
- ** As distributed, this program requires (for compilation):
- ** "The MicroFirm Function LIbrary for MS/QC"
- ** which may be obtained without registration from many Bulletin
- ** Board Systems.
- **
- ** or by registration:
- ** $25 for Docs, C, S, M, L, H libraries, and complete library source
- ** in C and Assembler
- ** MicroFirm
- ** P.O. Box 428
- ** Alief, TX 77411
- **
- **
- ** Program requires at least one parameter, the name of the file to dump.
- ** Other options:
- ** -snnnn Start at hex offset nnnn into file (default is 0000)
- ** -hnn Highlite all bytes of hex value nn in display
- ** -r Right side display is actual IBM screen character.
- ** Without this option, only valid ASCII characters
- ** are displayed. All others display as a period.
- **
- ** More than a utility, this is a good demonstration of the direct
- ** video subroutines and video page switching mechanism (for CGA cards).
- ** Look upon this as a tutorial as well as a handy utility.
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <mflconio.h>
- #include <mflfiles.h>
- #include <mflsys.h>
-
- int adrclr, /* address field color */
- hexclr, /* hex data color */
- highclr, /* highlite color */
- prompt, /* prompt line color */
- ascclr; /* ascii color */
-
- int buffer[16];
- unsigned long offset = 0;
- FILE *f = NULL;
-
- int mono, cpage, eoflag, rawflag;
-
-
- void bad_options(void)
- {
- error("Use: dump file [-snnnn -hnn -r]");
- }
-
-
- main(int argc, char *argv[])
- {
- int i, lines, view, vflag;
-
- eoflag = vflag = FALSE; /* default highlite off */
- rawflag = FALSE; /* default normal display mode */
-
- if ((argc < 2) || (argc > 4))
- bad_options();
-
- while (--argc > 0)
- {
- if (argv[argc][0] == '-')
- {
- switch(argv[argc][1])
- {
- case 'r':
- case 'R':
- rawflag = TRUE;
- break;
- case 'h':
- case 'H':
- sscanf(&argv[argc][2], "%02x", &view);
- vflag = TRUE;
- break;
- case 's':
- case 'S':
- sscanf(&argv[argc][2], "%lx", &offset);
- break;
- } /* end of switch */
- continue;
- }
- else
- {
- if (f != NULL)
- error("Too many filenames specified");
- if ((f = fopen(argv[argc], "rb")) == NULL)
- cant(argv[argc]);
- }
- } /* end of while statement */
-
- if (f == NULL)
- bad_options();
- fseek(f, offset, 0);
-
- i = stuff(0); /* check video type */
- if (i == MONO)
- {
- mono = TRUE;
- prompt = BLINKING;
- adrclr = WHITE;
- hexclr = HIWHITE;
- ascclr = WHITE;
- highclr = HIGHBLINK;
- }
- else
- { /* for CGA cards */
- vmode(CLR80);
- mono = FALSE;
- cpage = 1; /* we'll write to page 1 first */
- prompt = BROWN;
- adrclr = LTGREEN;
- hexclr = YELLOW;
- ascclr = LTCYAN;
- highclr = HIWHITE;
- }
-
- dvid_init(); /* use direct video access routines */
- dvid_cls();
- dvid_move(0,0);
- dvid_flush();
- dvid_attrib(hexclr); /* set hex value color, our default */
- if (!mono)
- {
- dvid_setpage(0, 1); /* set page 0 as display page */
- dvid_setpage(1, 0); /* but set page 1 as writing page */
- }
- cursor_style(5, 0, 0); /* kill the cursor */
- lines = 20;
-
- for EVER
- {
- /* if this were a level 1 file using read() and open()
- ** the program would be faster. Just a hint! */
- for (i = 0; i < 16; i++)
- buffer[i] = fgetc(f);
-
- if (buffer[0] == -1)
- {
- if (lines == 20)
- break; /* we must've ended on even page */
- eoflag = TRUE;
- goto alldone;
- }
-
- dvid_printfa(adrclr, "%04lx: ",offset); /* display address */
- for (i = 0; i < 16; i++)
- {
- if (buffer[i] != -1)
- {
- if (vflag && (buffer[i] == view))
- { /* highlite this byte? */
- dvid_printfa(highclr, "%02x ",
- buffer[i]);
- }
- else dvid_printf("%02x ", buffer[i]);
- }
- else dvid_putstr(" ");
- }
- dvid_raw(rawflag); /* select our mode */
- dvid_attrib(ascclr); /* display ascii data color */
- dvid_putstr(" ");
- for (i = 0; i < 16; i++)
- {
- if (buffer[i] != -1)
- {
- if (!isprint(buffer[i]) && !rawflag)
- dvid_putchr('.');
- else dvid_putchr(buffer[i]);
- }
- else dvid_putchr(' ');
- }
- dvid_raw(FALSE); /* force non-raw mode */
- dvid_putchr('\n');
- if (lines-- == 0)
- {
- alldone: dvid_attrib(prompt);
- dvid_say(22, 10, "Press any key to continue, "
- "ESCape to quit...");
- if (!mono)
- dvid_setpage(cpage, 1); /* display this page */
- if (getkey() == ESC)
- {
- dvid_setpage(0, 1); /* return to page 0 */
- if (!mono) /* restore cursor */
- cursor_style(1, 0, 0);
- else cursor_style(2, 0, 0);
- aabort(1);
- }
- if (eoflag)
- break;
- cpage++; /* bump page #, maintain 0-3 */
- cpage &= 3;
-
- if (!mono) /* set the next page for write */
- dvid_setpage(cpage, 0);
- dvid_cls();
- dvid_flush();
- lines = 20;
- }
- offset += 16;
- dvid_attrib(hexclr);
- } /* end of forever loop */
-
- dvid_setpage(0, 1); /* restore page 0 */
- dvid_cls();
- dvid_attrib(prompt);
- dvid_say(22, 10, "End of File Encountered");
- if (!mono)
- cursor_style(1, 0, 0); /* restore cursor */
- else cursor_style(2, 0, 0);
- dvid_flush();
- dvid_done();
- }
-