home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / chkffff.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  4KB  |  168 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1992 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------- */
  7. /* rev. history   1.0 --                                      */
  8. /*                1.1 -- checks for faulty headers            */
  9. /*                1.2 -- no real changes                      */
  10. /* ---------------------------------------------------------- */
  11.  
  12. #include "defines.h"
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include OSBIND
  16. #define __BIG_GENERATOR__  1
  17. #include "code.h"
  18.  
  19. struct
  20. {
  21.    word  ffff,
  22.          start,
  23.          end;
  24. } header;
  25.  
  26.  
  27. word  __x;
  28.  
  29. #if __NSTDC__
  30. void           fixheader( void),
  31.                nerror( char *);
  32. #endif
  33.  
  34. void fixheader()
  35. {
  36.    header.start = dpeek( &header.start);
  37.    header.end   = dpeek( &header.end);
  38. }
  39.  
  40. void nerror( s)
  41. char  *s;
  42. {
  43.    fprintf( stderr, "chkffff: Error \"%s\"\n", s);
  44.    exit(1);
  45. }
  46.  
  47. #if OS == TOS
  48. int   tossable;
  49. #endif
  50. int   what_the_fuck;
  51.  
  52.  
  53. main( argc, argv)
  54. int   argc;
  55. char  **argv;
  56. {
  57.    static char    infile[ 256];
  58.    extern char    *getenv();
  59.    int            i = 0,
  60.                   cnt = 0,
  61.                   fp;
  62.    unsigned long  space, bread,
  63.                   total = 0;
  64.  
  65.    while( ++i < argc)
  66.    {
  67.       if( *argv[i] == '-')
  68.          switch( argv[i][1])
  69.          {
  70. #if OS == TOS
  71.             case 'T' :
  72.             case 't' :
  73.                tossable = 1;
  74.                break;
  75. #endif
  76. #ifdef __DATE__
  77.             case ':' :
  78.                fputs( __DATE__, stderr);
  79. # ifdef __TIME__
  80.                fprintf( stderr, " %s", __TIME__);
  81. # endif                              
  82.                putc( '\n', stderr);
  83.                break;
  84. #endif               
  85.             case 'V' :
  86.             case 'v' :
  87.                fprintf( stderr, "chkffff v1.2 (c) 1992 by Natuerlich! -- views $FF $FF binary headers\n");
  88.                break;
  89.                
  90.             case 'W' :
  91.             case 'w' :
  92.                what_the_fuck = ! what_the_fuck;
  93.          }
  94.       else
  95.       {
  96.          if( infile[0])
  97.             goto usage;
  98.          strcpy( infile, argv[i]);
  99.       }
  100.    }
  101.    if( ! infile[0])
  102.       goto usage;
  103.  
  104.    if( (fp = Fopen( infile, OPEN_R)) < 0)
  105.       nerror("File open failed");
  106.  
  107.    if( (bread = Fread( fp, 6L, &header)) < 6)
  108.       nerror("File trunctated\n");
  109.    if( header.ffff != 0xFFFF)
  110.       nerror("Not a $FF $FF binary file");
  111.    for(;;)
  112.    {
  113.       fixheader();
  114.       space = (unsigned long) (1 + header.end - header.start);
  115.       printf("Segment %2.2d -- From: $%4.4X  To: $%4.4X   %5.5ld bytes\n",
  116.                   ++cnt, header.start, header.end, space);
  117.       if( header.end < header.start && ! what_the_fuck)
  118.          nerror("Segment wraps around $FFFF");
  119.       total += space;
  120.       if( Fseek( space, fp, 1) < 0)
  121.          nerror("File ended unexpectedly early");
  122.       if( (bread = Fread( fp, 4L, (char huge *) &header + 2)) <= 0)
  123.          break;
  124.       if( bread < 4)
  125.          nerror("Garbage at end of segment");
  126.       if( header.start == 0xFFFF)
  127.       {
  128.          header.ffff = header.start;
  129.          header.start = header.end;
  130.          if( (bread = Fread( fp, 2L, (char huge *) &header + 4)) <= 0 ||
  131.              bread < 2)
  132.             nerror("This is a trashed binary");
  133.       }
  134.    }
  135.    printf("------------------------------------   %5.5ld bytes total\n", total);
  136. #if OS == TOS
  137.    keypress();
  138. #endif
  139.    return(0);
  140.  
  141. usage:
  142. #if OS == TOS
  143.    fputs( "usage: chkffff [-t] binary.file\n", stderr);
  144. #else
  145.    fputs( "usage: chkffff binary.file\n", stderr);
  146. #endif
  147.    fputs( "\t-v : version\n", stderr);
  148.    fputs( "\t-w : segment wraps OK\n", stderr);
  149. #if OS == TOS
  150.    fputs( "\t-t : wait for keypress before exiting\n", stderr);
  151.    keypress();
  152. #endif
  153.    return( 1);
  154. }
  155.  
  156. keypress()
  157. {
  158. #if OS == TOS
  159.    if( tossable)
  160.    {
  161.       while( Bconstat( 2))
  162.          Bconin( 2);
  163.       Bconin( 2);
  164.    }
  165. #endif
  166. }
  167.  
  168.