/* CheckDrive 1.4 © Johan Eliasson 1992             *
 * To be compiled with SAS C (any version, I think) *
 * > lc -L CheckDrive.c will do.                    */

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
#include <exec/memory.h>
#include <exec/types.h>
#include <devices/trackdisk.h>
#include <libraries/dosextens.h>
#include <BBBF.h>
#include <BBBF.pragma.h>

#define HELLO "CheckDrive 1.4 © Johan Eliasson 1992\nReading Bootblock.brainfile..."
#define USAGE "Usage: CheckDrive <drive>\ndrive = number of drive to check"

UBYTE *VersionString = "$VER: CheckDrive 1.4 (11.11.92)";

struct Library *BootblockBase;

struct Bootblock *BB;

ULONG chip boot[256];
ULONG      drive;
USHORT     virus;
char       string[80];
char       version[30];
long       retcode;
long       readerror;

/* ****************************** main() ****************** */

void main(int argc,char *argv[])
{
  void die(),error();

  if ((argc == 1) || (argv[1][0] == '?'))
    die(USAGE);

  Write(Output(),HELLO,strlen(HELLO));

/* ***************** Open Bootblock.library ************* */

  if (!(BootblockBase = (struct Library *) OpenLibrary("Bootblock.library",1)))
    die("\nCheckDrive 1.4 needs the bootblock.library v1.0+ !");

/* ****************** Read brainfile ******************* */

  readerror = ReadBBBF("L:Bootblock.brainfile");

  if (readerror == ERROR_OBJECT_NOT_FOUND) readerror = ReadBBBF("Bootblock.brainfile");

/* ****************** Check result************************** */

  switch(readerror)
  {
    case BBBF_LOADED         : printf("Ok!\n\n"); break;
    case BBBF_NOT_BBBF       : die("\n\nIt is not a Bootblock.brainfile!");
    case BBBF_CHECKSUM_ERROR : die("\n\nThe Bootblock.brainfile is corrupted!");
    case BBBF_ALREADY_LOADED : printf("\n\nThe brainfile is already loaded!\n"); break;
    case BBBF_OUT_OF_MEMORY  : die("\n\nOut of memory!");
    default                  : error(readerror);
  };

/* ***************** Get additional info **************** */

  if (GetBBBFInfo(&virus,version) == BBBF_NOT_LOADED)
    die("The brainfile is not loaded!");

/* ************************ Output info ********************** */

  printf("Brainfile version %s.\nKnows %d viruses.\n\n",version,virus);

/* ************************ Prepare for boot-read ***************** */

  if ((atol(argv[1]) <= 4) && (atol(argv[1]) >= 0))
  {
    drive = (ULONG) atol(argv[1]);

/* ********************** Read boot ***************************** */

    if (readerror = ReadBoot(drive,boot))
      error(readerror);

/* ********************** Check boot ************************** */

    BB = (struct Bootblock *) CheckBoot(boot,&retcode);

    switch(retcode)
    {
      case BBBF_NOT_LOADED : die("Brainfile is not loaded!");
      case BOOT_VIRUS      : sprintf(string,"WARNING! The disk in DF%d: is infected with the \'%s\' virus!",drive,BB->bootname);
                             die(string);
      case BOOT_UNKNOWN    : sprintf(string,"The disk in DF%d: is not infected with any known bootvirus!",drive);
                             die(string);
      case BOOT_NOT_BOOT   : sprintf(string,"The disk in DF%d: is not bootable!");
                             die(string);
    }
  }

  die(USAGE);
}

void error(short error)
{
  Fault(error,"Error",string,81);
  die(string);
}

void die(char *message)
{
  strcat(message,"\n");    /* Saved some bytes there! Or did I really? 8-) */
  Write(Output(),message,strlen(message));
  if (BootblockBase)
  {
    FreeBBBF();
    CloseLibrary(BootblockBase);
  }
  exit(0);
}

