home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / DUMP.ZIP / DUMP.C < prev    next >
Text File  |  1990-01-03  |  4KB  |  96 lines

  1. /*
  2.     DUMP.C for OS/2 version 1.2.
  3.  
  4.     Displays the binary contents of a file in hex and ASCII on the
  5.     standard output device.  Demonstrates direct calls to OS/2 API
  6.     from a C program.  Supports long-names.  Uses the new DosOpen2
  7.     API for compatibility with OS/2 2.0.
  8.  
  9.     This program does not require Microsoft or IBM Programmer's Toolkit
  10.     header files, but must be linked using LINK.EXE and DOSCALLS.LIB 
  11.     from retail OS/2 version 1.2.
  12.  
  13.     Compile:    cl -c /Zi dump.c
  14.                 link /CO dump,dump,,doscalls,dump.def;
  15.  
  16.     Usage is:   dump pathname.ext [>destination]
  17.  
  18.     Copyright (C) 1989 Ray Duncan
  19. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #define RECSIZE 16                          // size of file records 
  24.  
  25. #define MAXPATHNAME     260                 // max length of pathname
  26. #define MAXFILENAME     255                 // max length of filename
  27.  
  28. #define API unsigned extern far pascal      // OS/2 API function prototypes
  29. API DosClose(unsigned);                     
  30. API DosOpen2(char far *, unsigned far *, unsigned far *, unsigned long,
  31.              unsigned, unsigned, unsigned long, void far *, unsigned long);           
  32. API DosQPathInfo(void far *, unsigned, char far *, int, unsigned long);
  33. API DosRead(unsigned, void far *, unsigned, unsigned far *);
  34.  
  35. main(int argc, char *argv[])
  36. {   
  37.     char fbuff[RECSIZE];                    // data block from file 
  38.     char qname[MAXPATHNAME];                // receives qualified filename
  39.     unsigned long foffset = 0L;             // file offset in bytes 
  40.     unsigned handle, action, length;        // DosOpen2, DosRead variables
  41.     unsigned OpenFlag = 0x01;               // fail if file not found 
  42.     unsigned long OpenMode = 0x40;          // read only, deny none 
  43.  
  44.     if(argc < 2)                            // check command tail 
  45.     {   
  46.         fprintf(stderr, "\ndump: missing filename\n");
  47.         exit(1);
  48.     }
  49.  
  50.     if(DosOpen2(argv[1], &handle, &action, 0L, 0, OpenFlag, OpenMode, NULL, 0L)) 
  51.     {   
  52.         fprintf(stderr, "\ndump: can't find %s\n", argv[1]);
  53.         exit(1);
  54.     }  
  55.                                             // get fully qualified filename
  56.     if(!DosQPathInfo(argv[1], 5, qname, sizeof(qname), 0L))
  57.         printf("\n%s", strlwr(qname));      // and display it
  58.  
  59.     while((DosRead(handle, fbuff, RECSIZE, &length) == 0) 
  60.            && (length != 0))
  61.     {   
  62.         dump_rec(fbuff, foffset, length);
  63.         foffset += RECSIZE;
  64.     }
  65.  
  66.     printf("\n");                           // extra blank line 
  67.     DosClose(handle);                       // close the input file 
  68.     exit(0);                                // return success code 
  69. }
  70.  
  71. /*
  72.     Display record (16 bytes) in hex and ASCII on standard output.
  73. */
  74. dump_rec(char *buffer, long foffset, int length)
  75. {   
  76.     int i;                                  // index to current record 
  77.  
  78.     if(foffset % 128 == 0)                  // maybe print heading 
  79.         printf("\n\n       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");
  80.         
  81.     printf("\n%04lX ", foffset);            // file offset 
  82.  
  83.     for(i = 0; i < length; i++)             // print hex equiv. of bytes
  84.         printf(" %02X", (unsigned char) buffer[i]);
  85.  
  86.     if(length != 16)                        // space over if last rec. 
  87.         for(i=0; i<(16-length); i++) printf("   ");
  88.  
  89.     printf("  ");
  90.     for(i = 0; i < length; i++)             // print ASCII equiv. of bytes 
  91.     {   
  92.         if(buffer[i] < 32 || buffer[i] > 126) putchar('.');
  93.         else putchar(buffer[i]);
  94.     }
  95.