/*-----------------------------------------------------------------------*/ /* Program: show_hex */ /* */ /* Purpose: This program is used to display the blocks of a file in */ /* ascii or hexidecimal. */ /* */ /* Contains: GetBlock */ /* DisplayBlock */ /* main */ /* */ /* Author: John Gauch */ /* */ /* Date: Sept. 12, 1984 - Original program. */ /* June 2, 1994 - Added to KUIM as support program. */ /* */ /* Usage: show_hex [filename] */ /* */ /* Commands: A)scii - display block in ascii. */ /* H)ex - display block in hexadecimal. */ /* N)ext - display next block in file. */ /* P)revious - display previous block in file. */ /* S)pecific - display a given block in file. */ /* Q)uit - end program. */ /*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/ /* Globals: The following global constants and variables are used to */ /* keep track of file information and user commands. A read */ /* buffer is used to improve I/O efficiency. */ /*-----------------------------------------------------------------------*/ #include #include #include #include #define BufSize 512 /* Size of read buffer */ #define FromBegin 0 /* Offset type for lseek */ #define NameLen 50 /* Size of file name string */ #define CmdLen 10 /* Size of command line string */ #define Ascii 0 /* My code for ascii display */ #define Hex 1 /* My code for hex display */ char Buffer[BufSize]; /* Read buffer */ int BlockNum; /* Block offset into file (512 byte blocks) */ long Offset; /* Byte offset into file */ char FileName[NameLen]; /* File name string */ FILE *Fd; /* File identifier */ char Command[CmdLen]; /* Command line string */ int DispMode; /* Display mode (either ascii or hex) */ /*-----------------------------------------------------------------------*/ /* Purpose: This routine is used to read a data from the file into a */ /* buffer 512 bytes long. If only a partial block is read, */ /* the rest of the block is padded with nulls. */ /*-----------------------------------------------------------------------*/ void GetBlock() { int ReadCnt; /* Count of bytes read */ int i; /* Index into read buffer */ /* Interpret command */ switch (Command[0]) { case 'N': case 'n': Offset = Offset + BufSize; BlockNum = Offset / BufSize; break; case 'P': case 'p': Offset = Offset - BufSize; if (Offset < 0) Offset = 0; BlockNum = Offset / BufSize; break; case 'S': case 's': printf("\nEnter block number to be displayed: "); scanf("%d", &BlockNum); Offset = BlockNum * BufSize; break; case 'H': case 'h': DispMode = Hex; break; case 'A': case 'a': DispMode = Ascii; break; } /* Fill buffer with nulls */ for (i = 0; i < BufSize; i++) Buffer[i] = '\000'; /* Read block from file into buffer */ if (fseek(Fd, Offset, FromBegin) == -1) printf("Error - could not seek\n"); ReadCnt = fread(Buffer, BufSize, 1, Fd); } /*-----------------------------------------------------------------------*/ /* Purpose: This procedure is used to display the data in the buffer in */ /* ascii and hexadecimal. */ /*-----------------------------------------------------------------------*/ void DisplayBlock() { int Line; /* Line number in display (0..15 range) */ int Start; /* Read buffer address of first byte in line */ int End; /* Read buffer address of last byte in line */ int i; /* Index into read buffer */ char c; /* Temp variable */ /* Print first line */ system("clear"); printf("Filename: %-50s Block#: %-5d\n\n", FileName, BlockNum); /* Print data lines in hexadecimal OR ascii */ for (Line = 0; Line <= 15; Line++) { /* Print line header */ Start = (Offset % BufSize) + Line * 32; End = Start + 31; printf("%3d - %-3d ", Start, End); /* Print data lines in ascii */ if (DispMode == Ascii) { for (i = Start; i <= End; i++) { c = (char) Buffer[i] & 0177; if (iscntrl(c) || isspace(c)) printf(" "); else printf("%c ", c); } printf("\n"); } /* Print data lines in hexadecimal */ else if (DispMode == Hex) { for (i = Start; i <= End; i++) printf("%2x", Buffer[i] & 0377); printf("\n"); } } } /*-----------------------------------------------------------------------*/ /* Purpose: This program is used to display the contents of a file in */ /* hexadecimal and ascii (where possible). */ /*-----------------------------------------------------------------------*/ main(int Argc, char *Argv[]) { /* Clear screen and print title */ printf("\fSHOW_HEX Program - Version 2.0\n\n"); /* Determine name of file to display */ if (Argc > 1) strcpy(FileName, Argv[1]); else { printf("\nEnter name of file to be displayed: "); scanf("%s", FileName); } /* Open file */ Offset = 0; DispMode = Ascii; Fd = fopen(FileName, "r"); if (Fd == NULL) printf("Error - could not open file\n"); /* Loop handling user commands */ do { /* Read and display block */ GetBlock(); DisplayBlock(); /* Print command prompt and read command line */ printf("\nA)scii H)ex N)ext P)revious S)pecific Q)uit ? "); scanf("%s", Command); } while ((Command[0] != 'Q') && (Command[0] != 'q')); /* Close file */ fclose(Fd); return (0); }