home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1988 / 02 / holub / fdump.c
Text File  |  1979-12-31  |  3KB  |  142 lines

  1.  
  2.  
  3.  
  4.  
  5.  #include <stdio.h>
  6.  #include <fcntl.h>
  7.  #include <ctype.h>
  8.  
  9.  /*  Binary file dump utility. Usage is:
  10.   *   fdump file...   Dump all files listed on command line
  11.   *   fdump +N file   Start N bytes from start of file
  12.   *   fdump -N file   Start N bytes from end of file.
  13.   */
  14.  
  15.  extern long     lseek( int, long, int );
  16.  
  17.  /*--------------------------------------------------------*/
  18.  
  19.  #define BUFSIZE 16
  20.  #define CR      0x0d
  21.  #define LF      0x0a
  22.  #define C_CR    0x11    /* Print left arrow for CR      */
  23.  #define C_LF    0x19    /* Print down arrow for LF      */
  24.  
  25.  #define isprinting(c) (' ' <= (c)  &&  (c) <= '~')
  26.  
  27.  static   long   Bytenum = 0L;
  28.  
  29.  /*--------------------------------------------------------*/
  30.  
  31.  main(argc, argv)
  32.  int     argc;
  33.  char    **argv;
  34.  {
  35.      register int fd;
  36.      long         offset = 0L;
  37.  
  38.      if( argc == 1 )
  39.          usage();
  40.  
  41.      ++argv;
  42.      --argc;
  43.  
  44.      if( **argv == '-' || **argv == '+' )
  45.      {
  46.          if( !isdigit( argv[0][1] ) )
  47.              usage();
  48.  
  49.          offset = atoi( argv[0] );
  50.  
  51.          ++argv;
  52.          --argc;
  53.      }
  54.  
  55.      for(; --argc >= 0 ; ++argv )
  56.      {
  57.          if( (fd = open( *argv, O_RDONLY | O_BINARY)) == -1 ) 
  58.              perror( *argv );
  59.          else
  60.          {
  61.              if( offset > 0 )
  62.                      Bytenum = lseek( fd, offset, SEEK_SET );
  63.  
  64.              else if( offset < 0 )
  65.                      Bytenum = lseek( fd, offset, SEEK_END );
  66.  
  67.              dofile( fd );
  68.              close ( fd );
  69.          }
  70.      }
  71.  
  72.      exit(0);
  73.  }
  74.  
  75.  /*--------------------------------------------------------*/
  76.  
  77.  usage()
  78.  {
  79.      fprintf(stderr, "Usage fdump [+|- N] file...\n");
  80.      fprintf(stderr, "+N   Start dump at byte N\n");
  81.      fprintf(stderr, "-N   Start dump N bytes from EOF\n");
  82.      exit( 1 );
  83.  }
  84.  
  85.  /*--------------------------------------------------------*/
  86.  
  87.  dofile( fd )
  88.  int     fd;
  89.  {
  90.          static   char   buf[ BUFSIZE ];
  91.          register char   nbytes;
  92.  
  93.          while( (nbytes = read(fd, buf, BUFSIZE)) > 0 )
  94.                  doline( buf, nbytes );
  95.  }
  96.  
  97.  /*--------------------------------------------------------*/
  98.  
  99.  doline( buf, nbytes )
  100.  char    *buf;
  101.  {
  102.      static   char       hex[] = "0123456789abcdef" ;
  103.      register int        i;
  104.      register char       *p;
  105.  
  106.      printf("%06lx: ", Bytenum );
  107.      Bytenum += nbytes;
  108.  
  109.      for( i = 1, p = buf ; i <= BUFSIZE ; i++ , p++ )
  110.      {
  111.              if( i <= nbytes )
  112.              {
  113.                      putchar( hex[ (*p >> 4) & 0xf ] );
  114.                      putchar( hex[  *p       & 0xf ] );
  115.              }
  116.              else
  117.              {
  118.                      putchar( ' ' );
  119.                      putchar( ' ' );
  120.              }
  121.  
  122.              putchar( ' ' );
  123.      }
  124.  
  125.      putchar(' ');
  126.  
  127.      for( i = 1, p = buf ; i <= nbytes ; i++ , p++ )
  128.      {
  129.              if( *p == CR )
  130.                      putchar( C_CR );
  131.  
  132.              else if( *p == LF )
  133.                      putchar( C_LF );
  134.              else
  135.                      putchar( isprinting(*p) ? *p : '.' );
  136.      }
  137.  
  138.      putchar('\n');
  139.      putchar('\r');
  140.  }
  141.  
  142.