home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 170_01 / dump2.c < prev    next >
Text File  |  1979-12-31  |  5KB  |  184 lines

  1. /*  dump.c  (10/83)  Debug style dump of a file from any starting position.
  2.  
  3.  
  4.   Usage: dump filespec [block [page]] | [segment:[offset]] [count]
  5.      Where a block is 64K bytes and a page is 256 bytes.
  6.      Segment:offset are standard 8086 notation in hexadecimal.
  7.      Count is the number of bytes to dump in decimal.
  8.  
  9.    dump b:\calc\data.dbm 1 16
  10.     Positions file to absolute offset of 1000:1000 or 69632.
  11.    dump bigfile :4F0 256
  12.     Positions file to absolute offset of 4F0H or 1264 and
  13.     dumps 256 bytes.
  14.  
  15.      The original source for dump was found on Lynn Long's BBS in Tulsa.  I
  16. modified it to include file positioning.  It has proven to be a great tool at
  17. times when I have to work with binary files that often exceed several million
  18. characters.
  19.    I have been using the Lattice 'C' compiler for about a year now and have
  20. been very pleased with it.  I have re-written the level 1 I/O routines to take
  21. advantage of DOS 2.0 path names, etc. and modified the _main module to allow
  22. me to load and execute programs from within 'c'.
  23.  
  24.             Ted Reuss     c/o South Texas Software, Inc.
  25.           Home: 713/961-3926      4544 Post Oak Place, Suite 176
  26.           Offi: 713/877-8205      Houston, Tx 77027
  27.  
  28. */
  29. #include <stdio.h>
  30. #include <ctype.h>        /* Lattice character type macros */
  31.  
  32. #define FAIL        1
  33. #define SUCCESS     0
  34. #define TRUE      (-1)
  35. #define FALSE        0
  36. #define NULL        0        /* null pointer value */
  37. #define FOREVER     for (;;)
  38. #define PAUSE        if (getch()=='\0') getch();
  39. #define STDIN        0
  40. #define STDOUT        1
  41. #define STDERR        2
  42.  
  43. #define BUFSIZE 512
  44. typedef unsigned    ushort;
  45. typedef int        void;
  46.  
  47. int _fmode = 0x8000;        /* Lattice 'c' - forces binary I/O */
  48. extern short errno;        /* DOS 2.0 error number */
  49. long lseek();
  50.  
  51. char   buffer[BUFSIZE];     /* input buffer */
  52. ushort block = 0;        /* block number ( 64k bytes/block ) */
  53. ushort page = 0;        /* page number ( 256 bytes/page ) */
  54. ushort segment = 0;
  55. ushort offset = 0;
  56. long   filpos = 0;        /* beginning file position */
  57. long   count = 0x7FFFFFFFL;    /* number of bytes to dump */
  58.  
  59. main(argc,argv)         /* DUMP ENTRY */
  60. int  argc;
  61. char *argv[];
  62. {
  63.    char c;
  64.    ushort i, numin, tot, file, cfrom, flag = 0;
  65.  
  66.    if (argc < 2)
  67.       abort( "Usage:dump filespec [block [page]] | [seg:[ofs]]", 0, 0 );
  68.  
  69.    if ((file = open( argv[1], 0 )) == -1)
  70.       abort( "cannot open", argv[1], errno );
  71.  
  72.    if (argc > 2) {
  73.       if ((flag = stpchr( argv[2], ':' )) != NULL) {
  74.      i = stch_i( argv[2], &segment );
  75.      stch_i( argv[2]+i+1, &offset );
  76.       }
  77.       if (sscanf( argv[2], "%d", &block ) != 1)
  78.      abort( "invalid block", argv[2], 0 );
  79.    }
  80.    if (argc > 3)
  81.       if (sscanf( argv[3], "%d", &page ) != 1)
  82.      abort( "invalid page", argv[3], 0 );
  83.  
  84.    if ( flag ) {
  85.       filpos = (long)segment*16L + (long)offset;
  86.       tot = offset;
  87.    }
  88.    else {
  89.       filpos = (block * 65536L) + (page * 256);
  90.       tot = page * 256;
  91.       segment = block * 4096;
  92.    }
  93.  
  94.    if (lseek( file, filpos, 0 ) == -1L)
  95.       abort( "positioning to", argv[2], errno );
  96.  
  97.    if (argc > 4)
  98.       if (sscanf( argv[4], "%ld", &count ) != 1)
  99.      abort( "invalid count", argv[4], 0 );
  100.  
  101.  
  102.    do {                    /* read & dump BUFSIZE bytes */
  103.       numin = read( file, buffer, BUFSIZE );
  104.       if (numin == -1)
  105.      abort( "cannot read", argv[1], errno );
  106.       cfrom=0;
  107.       while (cfrom < numin) {
  108.  
  109.      ohw(segment);               /* print offset in hex */
  110.      putchar(':');
  111.      ohw(cfrom+tot);
  112.      putchar(' ');
  113.  
  114.      for (i=0; i < 16; i++) {       /* print 16 bytes in hex */
  115.         putchar(' ');
  116.         ohb(buffer[cfrom++]);
  117.      }
  118.  
  119.      putchar(' '); putchar(' '); putchar(' ');
  120.  
  121.      cfrom -= 16;
  122.      for (i=0; i < 16; i++) {       /* print 16 bytes in ASCII */
  123.         c = buffer[cfrom] & 0x7f;
  124.         if ( isprint(c) )           /* if printable character */
  125.            putchar(c);
  126.         else
  127.            putchar('.');               /* else print period */
  128.         cfrom++;
  129.      }
  130.  
  131.      putchar('\r'); putchar('\n');     /* print CR/LF */
  132.  
  133.      if ((count -= 16) <= 0)         /* is count exhausted? */
  134.         exit(0);
  135.       }                    /* end of while */
  136.       tot += numin;
  137.       if ( tot == 0 )
  138.      segment += 4096;
  139.    }                       /* end of do */
  140.    while (numin == BUFSIZE);
  141. }                       /* end of main */
  142.  
  143. void ohw(wrd)                 /*      print a word in hex     */
  144. ushort wrd;
  145. {
  146.    ohb( wrd>>8 );
  147.    ohb( wrd );
  148. }
  149.  
  150. void ohb(byt)                 /*      print a byte in hex     */
  151. char byt;
  152. {
  153.    onib( byt>>4 );
  154.    onib( byt );
  155. }
  156.  
  157. void onib(nib)             /*     print a nibble as a hex character   */
  158. char nib;
  159. {
  160.    nib &= 15;
  161.    putchar((nib >= 10) ? nib-10+'A': nib+'0');
  162. }
  163.  
  164. void abort( msg1 ,msg2 ,errno)       /*  print error msg1, msg2, and nbr */
  165. char *msg1,*msg2;           /*    Does not close files.  */
  166. short errno;
  167. {
  168.    char stemp[10];
  169.  
  170.    write( STDERR, "ERR: ", 5 );
  171.    if (msg1)
  172.       write( STDERR, msg1, strlen(msg1) );
  173.    if (msg2)
  174.       write( STDERR, " ", 1 );
  175.       write( STDERR, msg2, strlen(msg2) );
  176.    if (errno)    {
  177.       sprintf( stemp," #%d", errno );
  178.       write( STDERR, stemp, strlen(stemp) );
  179.    }
  180.    write( STDERR, "\r\n", 2 );
  181.    _exit( FAIL );
  182. }
  183. /** END DUMP **/
  184.