/*
 *  Days.c - (C) 1996 Martin Samuelsson - sam@augs.se
 * Source included for educational purposes. DCC Days.c -r
 * 'It may not be the best you've ever seen
 *  -but it's way better than nothing!'
 */

#include <dos/dostags.h>
#include <clib/dos_protos.h>

static UBYTE   *VersTag = "\0$VER: Days 1.0 (10.08.96) (C) 1996 Martin Samuelsson";
static UBYTE   *Usage = "Filename/A";
struct FileInfoBlock	*MyFIB;
struct DateStamp MyDS;
struct RDArgs  *readargs;
BPTR lock;
BOOL success;

int
MyError(unsigned char *string, int level)
{
 printf("%s\n",string);
 Closedown(level);
}

int
Closedown(int level)
{
 if(MyFIB != NULL) {
  FreeDosObject(DOS_FIB, MyFIB);
 }
 if(lock != NULL) {
  UnLock(lock);
 }
 if(readargs != NULL) {
  FreeArgs(readargs);
 }
 exit(level);
}

void
Age(struct DateStamp *Old, struct DateStamp *New)
{
 MyDS.ds_Days=New->ds_Days-Old->ds_Days;
 MyDS.ds_Minute=New->ds_Minute-Old->ds_Minute;
 MyDS.ds_Tick=New->ds_Tick-Old->ds_Tick;
 if(MyDS.ds_Tick < 0) {
  MyDS.ds_Minute--;
  MyDS.ds_Tick = MyDS.ds_Tick + 5000;
 }
 if(MyDS.ds_Minute < 0) {
  MyDS.ds_Days--;
  MyDS.ds_Minute = MyDS.ds_Minute + 1440;
 }
}

void main()
{

    LONG            rargs[2];

 if (readargs = ReadArgs(Usage, rargs, NULL)) {
  MyFIB = (struct FileInfoBlock *)AllocDosObjectTags(DOS_FIB, TAG_DONE);
  if(MyFIB == NULL) {
   MyError("FileInfoBlock error.",20);
  }
  lock  = Lock( (char *)rargs[0], ACCESS_READ );
  if(lock == NULL) {
   MyError("Can't lock input file.",20);
  }
  success = Examine(lock, MyFIB);
  if(success == NULL) {
   MyError("Could not examine input file.",20);
  }
  DateStamp(&MyDS);
  Age(&MyFIB->fib_Date, &MyDS);
  printf("%d\n",MyDS.ds_Days);
 } else {
  FreeArgs(readargs);
  printf("Usage: %s\n",Usage);
 }
 Closedown(0);
}
