home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CENVIW9.ZIP / HEXDUMP.CMM < prev    next >
Text File  |  1994-03-08  |  3KB  |  105 lines

  1. /**********************************************************************
  2.  *** HexDump.bat - Hexidecimal dump of the contents of a file. This ***
  3.  *** ver.1         examples program uses the CEnvi file routines.   ***
  4.  ***               It calls upon NotePad to display the results of  ***
  5.  ***               the dump.                                        ***
  6.  **********************************************************************/
  7.  
  8. #include <MsgBox.lib>
  9. #include <PickFile.lib>
  10.  
  11. main(argc,argv)
  12. {
  13.    // prompt for file name if file wasn't passed in
  14.    if ( argc == 1 ) {
  15.       FileName = PromptForFile();
  16.    } else {
  17.       if ( argc != 2 )
  18.          Instructions();
  19.       FileName = argv[1];
  20.    }
  21.  
  22.    if ( NULL == FileName  ||  0 == FileName[0] )
  23.       Instructions();
  24.  
  25.    // get the name of the temporary destination file to write to
  26.    DumpFileName = tmpnam();
  27.  
  28.    // perform the dump operation. True for success else false
  29.    if ( FileDump(FileName,DumpFileName) ) {
  30.       // dump worked OK, so spawn notepad to view the result
  31.       spawn(P_WAIT,"NotePad.exe",DumpFileName);
  32.    }
  33.  
  34.    // remove the temporary file, whether it exists or not
  35.    remove(DumpFileName);
  36.  
  37.    return(EXIT_SUCCESS);
  38. }
  39.  
  40. FileDump(SrcFile,DstFile)
  41. {
  42.    success = FALSE;  // assume failure
  43.    srcFP = fopen(SrcFile,"rb");
  44.    if ( NULL == srcFP )
  45.       Error("Could not open file %s for reading.",SrcFile);
  46.    else {
  47.       dstFP = fopen(DstFile,"wt");
  48.       if ( NULL == dstFP )
  49.          Error("Could not open file %s for writing.");
  50.       else {
  51.          fprintf(dstFP,"Hexidecimal dump of %s\n",SrcFile);
  52.          Unprintables = "\a\b\t\r\n\032\033"
  53.          success = TRUE;
  54.          for ( offset = 0; 0 < (count = fread(data,16,srcFP)); offset += 16 ) {
  55.             // display hex offset in file
  56.             fprintf(dstFP,"%06X  ",offset)
  57.             // display hex value for each number
  58.             for ( i = 0; i < count; i++ )
  59.                fprintf(dstFP,"%02X ",data[i])
  60.             // fill in any extra gaps if count < 16
  61.             while( i++ < 16 )
  62.                fprintf(dstFP,"   ")
  63.             // display ascii value for each printable character
  64.             // substitute a period for unprintable characters
  65.             data[count] = 0; // string must be null-terminated
  66.             while( (UnprintableIndex = strcspn(data,Unprintables)) < count )
  67.                data[UnprintableIndex] = '.';
  68.             fprintf(dstFP,"   %s\n",data)
  69.             if ( count < 16 )
  70.                break
  71.          }
  72.          fclose(dstFP);
  73.       }
  74.       fclose(srcFP);
  75.    }
  76.    return(success);
  77. }
  78.  
  79.  
  80. PromptForFile()
  81. {
  82.    return(GetOpenFileName(NULL,"HexDump File Selection"));
  83. }
  84.  
  85. Error(FormatString,arg1,arg2,arg3/*etc...*/)
  86. {
  87.    va_start(valist,FormatString);
  88.    vsprintf(message,FormatString,valist);
  89.    va_end(valist);
  90.    MessageBox(message);
  91. }
  92.  
  93.  
  94. Instructions()
  95. {
  96.    MessageBox(
  97.          "HexDump.cmm - This is a Cmm program to display a hexidecimal dump of\n"
  98.          "              any file.  Either execute with one argument, whcih is the\n"
  99.          "              name of the file to view, or with no arguments to be\n"
  100.          "              prompted for a file name."
  101.    );
  102.    exit(EXIT_FAILURE);
  103. }
  104.  
  105.