/* hexdump.c ************* Copyright 1988 by Vermont Creative Software ************** REVISIONS 27-Apr-88 -- vlr -- created 22-Sep-89 -- vlr -- fixed bug that kept it from printing line numbers correctly hexdump Program to do a dump of a file in hexadecimal and ASCII format, similar to the way XTREE PRO displays a file in hex format. Call hexdump [sourcefile.ext] [destinationfile.ext] destinationfile.ext (optional): The file to which the results should be written. */ #include #include #include #include #define RECSIZE 4096 int main(argc, argv) int argc; char **argv; { FILE *input; FILE *output; char *buffer; int sect = 4; char *hexptr; char *ascptr; int breakpt = 4; int spacect = 2; int i, j; long count1, count2; char ch; int intch; char tmpbuf[20]; char *tmpptr; long linenum; int reclen; buffer = calloc(1, RECSIZE); output = stdout; if (argc < 2) goto BAD_USAGE; else { if ((input = fopen(argv[1], "rb")) == NULL) { printf("\n\nERROR: Could not open input file %s\n", argv[1]); goto END; } if (argc > 2) if ((output = fopen(argv[2], "a+")) == NULL) { printf("\n\nERROR: Could not open output file %s\n", argv[2]); goto END; } } linenum = 0L; while (! feof(input)) { reclen = fread(buffer, 1, RECSIZE, input); hexptr = buffer; ascptr = buffer; count1 = 0L; count2 = 0L; while (count1 < reclen) { sprintf(tmpbuf, "%.6lX", linenum); linenum += (long) (sect * breakpt); fprintf(output, " %s ", tmpbuf); for (j = 0; j < sect; j++) { for (i = 0; i < breakpt; i++) { if (count1 == reclen) fprintf(output, " "); else { intch = (int) (*hexptr); sprintf(tmpbuf, "%.2X", intch); tmpptr = tmpbuf; tmpptr += strlen(tmpbuf) - 2; fprintf(output, "%s ", tmpptr); hexptr++; count1++; } } fprintf(output, " "); } fprintf(output, " "); for (j = 0; j < sect * breakpt; j++) { if (count2 == reclen) fprintf(output, " "); else { ch = *ascptr; if (isprint((int) ch)) fprintf(output, "%c", ch); else fprintf(output, "."); ascptr++; count2++; } } fprintf(output, "\n"); } } fprintf(output, " \n"); goto END; BAD_USAGE: printf("\nCorrect usage: hexdump [input file name] [output file name (optional)]\n"); END: fclose(input); fclose(output); return(0); }  N8