/* CheckSpace 1.0 by Johan Billing 1994

   Contact addresses:

   Normal mail: Johan Billing
                Östra Storgatan 22
                260 60 Kvidinge
                Sweden

   E-mail:      johan.billing@kcc.ct.se

   FidoNet:     Johan Billing at 2:200/207.6@FidoNet

   The program and the source are placed in the public domain. Feel free to
   use the source in your own programs if you wish to.

   The source was made for the DICE compiler. To compile, write
   "dcc CheckSpace.c -r -oCheckSpace".

   NOTE! I use _main() and _exit() to keep the program size down. If this
   doesn't work with your compiler, change them to main() and exit(). You
   will also have to change __COMMODORE_DATE__ if you use a different
   compiler.

*/


#include <stdlib.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

#define VERSION "1.0"
UBYTE *ver="\0$VER: CheckSpace "VERSION" ("__COMMODORE_DATE__")";

#define DEVICE  0
#define SIZE    1

LONG argarray[2];
UBYTE *argstr="DEVICE/A,SIZE/A/N";

struct RDArgs *rdargs;

_main()
{
   int res;
   BPTR l;
   struct InfoData *id;
   ULONG size;

   if(!(rdargs=(struct RDArgs *)ReadArgs(argstr,argarray,NULL)))
   {
      PrintFault(IoErr(),NULL);
      _exit(10);
   }

   if(!(id=(struct InfoData *)AllocMem(sizeof(struct InfoData),MEMF_ANY)))
   {
      PutStr("Out of memory!\n");
      FreeArgs(rdargs);
      _exit(10);
   }

   if(!(l=Lock((UBYTE *)argarray[DEVICE],SHARED_LOCK)))
   {
      PrintFault(IoErr(),NULL);
      FreeMem(id,sizeof(struct InfoData));
      FreeArgs(rdargs);
      _exit(10);
   }

   if(!(Info(l,id)))
   {
      PrintFault(IoErr(),NULL);
      FreeMem(id,sizeof(struct InfoData));
      FreeArgs(rdargs);
      _exit(10);
   }

   size=(id->id_NumBlocks-id->id_NumBlocksUsed)*id->id_BytesPerBlock;

   if(size/1024 < (ULONG)*(ULONG *)argarray[SIZE])
      res=5;

   else
      res=0;

   UnLock(l);
   FreeMem(id,sizeof(struct InfoData));
   FreeArgs(rdargs);
   _exit(res);
}