/***************************************************************************
 * dos.c:	AmigaDOS and Requestor related functions.
 *
 * Part of...
 *
 *	FREE:	Display free space on your disk volumes.
 *		Author:  Daniel J. Barrett, barrett@cs.umass.edu.
 *
 * This program is Freely Distributable.  Make all the copies you want
 * and give them away.  Use this code in any way you like.
***************************************************************************/


#include "free.h"

/* Given a disk volume name, return how much space is on it.  The units
 * of space are either bytes or blocks, depending on whether the user
 * specified the option OPT_BLOCKS or not. */

long GetMem(char *volume)
{
	long freeSpace;
	struct FileLock *lock;

	if (lock = (struct FileLock *)Lock(volume, ACCESS_READ))
	{
		freeSpace = AvailSpace(lock);
		UnLock((BPTR)lock);
	}
	else
		freeSpace = NO_DRIVE;
	return(freeSpace);
}


/* This function comes from Tom Smythe's "avail()" function.
 * Given a lock on a disk, calculate how much space is left on the disk
 * and return that value.  The value is in bytes, by default; but if the
 * user specified the option OPT_BLOCKS on the command-line, it's in 
 * blocks. */

long AvailSpace(struct FileLock *disk)
{
	struct InfoData *info;
	long answer;

	info = AllocMem((long)sizeof(struct InfoData), MEMF_PUBLIC);
	if (!info)
		ExitCleanly(ERROR_CANT_MALLOC, RETURN_FAIL);

	else if (!Info((BPTR)disk, info))
	{
		ErrorMsg(ERROR_INFO);
		answer = NO_DRIVE;
	}

	else
	{
		answer = (info->id_NumBlocks - info->id_NumBlocksUsed);
		if (!(flags & FLAG_BLOCKS))
			answer *= info->id_BytesPerBlock;
	}

	if (info)
		FreeMem(info, (long)sizeof(struct InfoData));

	return(answer);
}


/* Return the amount of free RAM on the system, broken into CHIP and
 * FAST RAM. */

void GetFreeRam(long *chip, long *fast)
{
	Forbid();
	*chip = AvailMem(MEMF_CHIP);
	*fast = AvailMem(MEMF_FAST);
	Permit();
}


/***************************************************************************
* Enable and disable system requestors.
***************************************************************************/

static APTR oldWindowPtr;		/* Pointer to current window.  */
static struct Process *theProc;		/* Pointer to current process. */


/* Turn off system requestors for this process. */

void DisableRequestors(void)
{
	theProc = (struct Process *)FindTask(NULL);
	oldWindowPtr = theProc->pr_WindowPtr;
	theProc->pr_WindowPtr = (APTR)(-1L);
}


/*
 * Turn on system requestors for this process, after they have been
 * turned off by DisableRequestors(), above.
 */

void EnableRequestors(void)
{
	theProc->pr_WindowPtr = oldWindowPtr;
}
