home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch08 / dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  2.7 KB  |  86 lines

  1. /*
  2.     DUMP.C
  3.  
  4.     Displays the binary contents of a file in hex and ASCII on
  5.     the standard output device.  Demonstrates direct calls to
  6.     OS/2 API from a C program.
  7.  
  8.     Compile with:  C> cl dump.c
  9.  
  10.     Usage is:  C> dump pathname.ext [>destination]
  11.  
  12.     Copyright (C) 1987 Ray Duncan
  13. */
  14.  
  15. #include <stdio.h>
  16.  
  17. #define RECSIZE 16                  /* size of file records */
  18.  
  19. #define API unsigned extern far pascal 
  20.  
  21. API DosClose(unsigned);             /* function prototypes */
  22.  
  23. API DosOpen(char far *, unsigned far *, unsigned far *, unsigned long,
  24.             unsigned, unsigned, unsigned, unsigned long);           
  25.  
  26. API DosRead(unsigned, void far *, unsigned, unsigned far *);
  27.  
  28. main(int argc, char *argv[])
  29. {   
  30.     char file_buf[RECSIZE];         /* data block from file */
  31.     unsigned long foffset = 0L;     /* file offset in bytes */
  32.     unsigned handle;                /* DosOpen variable */
  33.     unsigned action, length;        /* DosRead variables */
  34.     unsigned flag = 0x01;           /* fail if file not found */
  35.     unsigned mode = 0x40;           /* read only, deny none */
  36.  
  37.     if(argc < 2)                    /* check command tail */
  38.     {   
  39.         fprintf(stderr, "\ndump: missing file name\n");
  40.         exit(1);
  41.     }
  42.                                     /* open file or exit */
  43.     if(DosOpen(argv[1], &handle, &action, 0L, 0, flag, mode, 0L)) 
  44.     {   
  45.         fprintf(stderr, "\ndump: can't find file %s\n", argv[1]);
  46.         exit(1);
  47.     }  
  48.                                     /* read and dump records */
  49.     while((DosRead(handle, file_buf, RECSIZE, &length) == 0) 
  50.            && (length != 0))
  51.     {   
  52.         dump_rec(file_buf, foffset, length);
  53.         foffset += RECSIZE;
  54.     }
  55.     printf("\n");                   /* extra blank line */
  56.     DosClose(handle);               /* close the input file */
  57.     exit(0);                        /* return success code */
  58. }
  59.  
  60. /*
  61.     Display record (16 bytes) in hex and ASCII on standard output.
  62. */
  63. dump_rec(char *buffer, long foffset, int length)
  64. {   
  65.     int i;                          /* index to current record */
  66.  
  67.     if(foffset % 128 == 0)          /* maybe print heading */
  68.         printf("\n\n       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");
  69.         
  70.     printf("\n%04lX ", foffset);    /* file offset */
  71.  
  72.     for(i = 0; i < length; i++)     /* print hex equiv. of each byte */
  73.         printf(" %02X", (unsigned char) buffer[i]);
  74.  
  75.     if(length != 16)                /* space over if last rec. */
  76.         for(i=0; i<(16-length); i++) printf("   ");
  77.  
  78.     printf("  ");
  79.     for(i = 0; i < length; i++)     /* print ASCII equiv. of bytes */
  80.     {   
  81.         if(buffer[i] < 32 || buffer[i] > 126) putchar('.');
  82.         else putchar(buffer[i]);
  83.     }
  84. 
  85.