/* ---------------------------------------------------------------------- */
/*                   Copyright (C) 1992 by Natrlich!                     */
/*                      This file is copyrighted!                         */
/*                Refer to the documentation for details.                 */
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------- */
/* rev. history   1.0 --                                      */
/*                1.1 -- checks for faulty headers            */
/*                1.2 -- no real changes                      */
/* ---------------------------------------------------------- */

#include "defines.h"
#include <stdio.h>
#include <stdlib.h>
#include OSBIND
#define __BIG_GENERATOR__  1
#include "code.h"

struct
{
   word  ffff,
         start,
         end;
} header;


word  __x;

#if __NSTDC__
void           fixheader( void),
               nerror( char *);
#endif

void fixheader()
{
   header.start = dpeek( &header.start);
   header.end   = dpeek( &header.end);
}

void nerror( s)
char  *s;
{
   fprintf( stderr, "chkffff: Error \"%s\"\n", s);
   exit(1);
}

#if OS == TOS
int   tossable;
#endif
int   what_the_fuck;


main( argc, argv)
int   argc;
char  **argv;
{
   static char    infile[ 256];
   extern char    *getenv();
   int            i = 0,
                  cnt = 0,
                  fp;
   unsigned long  space, bread,
                  total = 0;

   while( ++i < argc)
   {
      if( *argv[i] == '-')
         switch( argv[i][1])
         {
#if OS == TOS
            case 'T' :
            case 't' :
               tossable = 1;
               break;
#endif
#ifdef __DATE__
            case ':' :
               fputs( __DATE__, stderr);
# ifdef __TIME__
               fprintf( stderr, " %s", __TIME__);
# endif                              
               putc( '\n', stderr);
               break;
#endif               
            case 'V' :
            case 'v' :
               fprintf( stderr, "chkffff v1.2 (c) 1992 by Natuerlich! -- views $FF $FF binary headers\n");
               break;
               
            case 'W' :
            case 'w' :
               what_the_fuck = ! what_the_fuck;
         }
      else
      {
         if( infile[0])
            goto usage;
         strcpy( infile, argv[i]);
      }
   }
   if( ! infile[0])
      goto usage;

   if( (fp = Fopen( infile, OPEN_R)) < 0)
      nerror("File open failed");

   if( (bread = Fread( fp, 6L, &header)) < 6)
      nerror("File trunctated\n");
   if( header.ffff != 0xFFFF)
      nerror("Not a $FF $FF binary file");
   for(;;)
   {
      fixheader();
      space = (unsigned long) (1 + header.end - header.start);
      printf("Segment %2.2d -- From: $%4.4X  To: $%4.4X   %5.5ld bytes\n",
                  ++cnt, header.start, header.end, space);
      if( header.end < header.start && ! what_the_fuck)
         nerror("Segment wraps around $FFFF");
      total += space;
      if( Fseek( space, fp, 1) < 0)
         nerror("File ended unexpectedly early");
      if( (bread = Fread( fp, 4L, (char huge *) &header + 2)) <= 0)
         break;
      if( bread < 4)
         nerror("Garbage at end of segment");
      if( header.start == 0xFFFF)
      {
         header.ffff = header.start;
         header.start = header.end;
         if( (bread = Fread( fp, 2L, (char huge *) &header + 4)) <= 0 ||
             bread < 2)
            nerror("This is a trashed binary");
      }
   }
   printf("------------------------------------   %5.5ld bytes total\n", total);
#if OS == TOS
   keypress();
#endif
   return(0);

usage:
#if OS == TOS
   fputs( "usage: chkffff [-t] binary.file\n", stderr);
#else
   fputs( "usage: chkffff binary.file\n", stderr);
#endif
   fputs( "\t-v : version\n", stderr);
   fputs( "\t-w : segment wraps OK\n", stderr);
#if OS == TOS
   fputs( "\t-t : wait for keypress before exiting\n", stderr);
   keypress();
#endif
   return( 1);
}

keypress()
{
#if OS == TOS
   if( tossable)
   {
      while( Bconstat( 2))
         Bconin( 2);
      Bconin( 2);
   }
#endif
}

