home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advmsdos / chap08 / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-01  |  2.4 KB  |  78 lines

  1. /*
  2.     DUMP.C      Displays the binary contents of a file in
  3.                 hex and ASCII on the standard output device.
  4.  
  5.     Compile:    C>CL DUMP.C
  6.  
  7.     Usage:      C>DUMP unit:path\filename.ext  [ >device ]
  8.  
  9.     Copyright (C) 1988 Ray Duncan
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <io.h>
  14. #include <fcntl.h>
  15.  
  16. #define REC_SIZE 16             /* input file record size */
  17.  
  18. main(int argc, char *argv[])
  19. {   
  20.     int fd;                     /* input file handle */
  21.     int status = 0;             /* status from file read */
  22.     long fileptr = 0L;          /* current file byte offset */
  23.     char filebuf[REC_SIZE];     /* data from file */
  24.  
  25.     if(argc != 2)               /* abort if missing filename */
  26.     {   fprintf(stderr,"\ndump: wrong number of parameters\n");
  27.         exit(1);
  28.     }
  29.  
  30.                                 /* open file in binary mode,
  31.                                    abort if open fails */
  32.     if((fd = open(argv[1],O_RDONLY | O_BINARY) ) == -1)
  33.     {   fprintf(stderr, "\ndump: can't find file %s \n", argv[1]);
  34.         exit(1);
  35.     }
  36.  
  37.                                 /* read and dump records 
  38.                                    until end of file */
  39.     while((status = read(fd,filebuf,REC_SIZE) ) != 0)
  40.     {   dump_rec(filebuf, fileptr, status);
  41.         fileptr += REC_SIZE;
  42.     }
  43.  
  44.     close(fd);                  /* close input file */
  45.     exit(0);                    /* return success code */
  46. }
  47.  
  48.  
  49. /*
  50.     Display record (16 bytes) in hex and ASCII on standard output
  51. */
  52.  
  53. dump_rec(char *filebuf, long fileptr, int length)
  54. {   
  55.     int i;                      /* index to current record */
  56.  
  57.     if(fileptr % 128 == 0)      /* display heading if needed */
  58.         printf("\n\n       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");
  59.  
  60.     printf("\n%04lX ",fileptr); /* display file offset */
  61.  
  62.                                 /* display hex equivalent of 
  63.                                    each byte from file */
  64.     for(i = 0; i < length; i++)
  65.         printf(" %02X", (unsigned char) filebuf[i]);
  66.  
  67.     if(length != 16)            /* spaces if partial record */
  68.         for (i=0; i<(16-length); i++) printf("   ");
  69.  
  70.                                 /* display ASCII equivalent of 
  71.                                    each byte from file */
  72.     printf("  ");
  73.     for(i = 0; i < length; i++)
  74.     {   if(filebuf[i] < 32 || filebuf[i] > 126) putchar('.');
  75.         else putchar(filebuf[i]);
  76.     }
  77.