/*------------------------------------------------------------------------*
                 Paragon utility functions for data manipulation
*------------------------------------------------------------------------*/

#include <time.h>

/* bit_ptr is the pointer (starting from 0) of a bit in a mask.  "Mask"
   is a pointer to an array of integers.  Either a 1 or 0 is returned
   depending on if that bit is on or off;  this is used in Paragon for
   checking the values of privillege bits, file area read and write bits,
   etc. */

unsigned int lookbit(bit_ptr,mask)
int bit_ptr;
int mask[];
{

unsigned int cur_int;
int bit_val;
int word_loc;
int bit_loc;

word_loc = bit_ptr/16;
bit_loc = bit_ptr-16*word_loc;
cur_int = mask[word_loc];
return ((cur_int>>bit_loc)&~(~0<<1));
}

/* This takes parameters the same as lookbit.  It will toggle the value
   of that bit. */

void setbit(bit_ptr,mask)
int bit_ptr;
int mask[];
{

unsigned int cur_int;
unsigned int bit_val;
int word_loc;
int bit_loc;

word_loc = bit_ptr/16;
bit_loc = bit_ptr-16*word_loc;
cur_int = mask[word_loc];

bit_val = ((cur_int>>bit_loc)&~(~0<<1));

switch(bit_val)
	{
	case 0: mask[word_loc]=((mask[word_loc])|(1<<bit_loc)); break;
	case 1: mask[word_loc]=((mask[word_loc])&~(1<<bit_loc)); break;
	}
}

/*-----------------------------------------------------------------------*
  For certain reasons, Paragon uses a date which is slightly different
  date format than what could be considered "normal."  
  Tgettime() will return the time in this format;  Tgetdate() will return
  the date in this format.

  OK, wanna know what it is the way it is?  A lot of Paragon was written
  on the Atari ST before we moved over to the Amiga.  This time is actually
  equivalent to the ST time/date format, and we have written functions to
  emulate the Atari ST's XBIOS time and date functions...  It would have
  been a hassle to redo it right away, and never got around to being
  changed.
*------------------------------------------------------------------------*/

struct sttime{
int dummy;
unsigned int sec :5;
unsigned int min :6;
unsigned int hour :5;
} tgettim;

struct stdate{
int dummy;
unsigned int day :5;
unsigned int month :4;
unsigned int year :7;
} tgetdat;

unsigned int Tgettime()
{
unsigned long amigatime,dummy;
struct tm *systime;

unsigned int *retptr;
amigatime=time(&dummy);
systime=localtime(&amigatime);
tgettim.sec=systime->tm_sec/2;
tgettim.min=systime->tm_min;
tgettim.hour=systime->tm_hour;
retptr=&tgettim.dummy;
++retptr;
return *retptr;
}

unsigned int Tgetdate()
{
unsigned long amigatime,dummy;
struct tm *systime;
unsigned int *retptr;
amigatime=time(&dummy);
systime=localtime(&amigatime);
tgetdat.day=systime->tm_mday;
tgetdat.month=systime->tm_mon+1;
tgetdat.year=systime->tm_year-80;
retptr=&tgetdat.dummy;
++retptr;
return *retptr;
}
