ARP 1.1 (Disk 85) (Mar 1988) : DriveTest / disk2.c

/*M*	Disk I/O benchmark functions	*/

/*	AMIGA disk driver interface routines	*/

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <exec/interrupts.h>
#include <exec/ports.h>
#include <exec/libraries.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/execbase.h>
#include <exec/devices.h>
#include <devices/trackdisk.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/filehandler.h>
#include <stdio.h>

SHORT error;
struct MsgPort *read_port;
struct IOExtTD *read_req;
long base_offset;

extern struct MsgPort *CreatePort();
extern struct IORequest *CreateExtIO();
extern struct DosLibrary *DOSBase;		/* DOS Library base pointer */

rawread(drive, nsec, begin_sec, buffer)
int drive, nsec, begin_sec;
char *buffer;
{
	if (Chk_Break ())					/* Check if ^C abort */
		Abort ("Aborted by ^C.\n");
	read_req->iotd_Req.io_Length = nsec * 512;
	read_req->iotd_Req.io_Data = (APTR) buffer;
	read_req->iotd_Req.io_Command = CMD_READ;
	read_req->iotd_Req.io_Offset = begin_sec * 512 + base_offset;
	read_req->iotd_Req.io_Flags = 0x10;
	SendIO(read_req);
	WaitIO(read_req);
	if (read_req->iotd_Req.io_Error)
		printf("Error: %d nsec=%d, sect=%d\n",
		    read_req->iotd_Req.io_Error, nsec, begin_sec);
	return ((int)read_req->iotd_Req.io_Error);
}

time()
{
	struct DateStamp ds;
	DateStamp (&ds);
	return (ds.ds_Tick + ds.ds_Minute * TICKS_PER_SECOND * 60);
}

getspace(ddrive, pfree, ptot, pbytes)
char *ddrive;
unsigned *pfree, *ptot, *pbytes;
{
	char * driver;
	struct RootNode *root_node;
	struct DosInfo *info_node;
	register struct DeviceNode *device_node;
	struct FileSysStartupMsg *startup_msg;
	unsigned long *envptr;
	struct InfoData *disk_info;
	long lock;

/* Clear ^C signal */
	Chk_Break ();
/* get driver name from device tables */
	root_node = (struct RootNode *) DOSBase->dl_Root;
	info_node = (struct DosInfo *) BADDR (root_node->rn_Info);
	device_node = (struct DeviceNode *) BADDR (info_node->di_DevInfo);
	while (device_node) {
		if (device_node->dn_Type == DLT_DEVICE ) {
			if (match (ddrive, BADDR (device_node->dn_Name) + 1) == 0)
				break;
		}
		device_node = (struct DeviceNode *) BADDR (device_node->dn_Next);
	}
	lock = Lock (ddrive, ACCESS_READ);
	if (device_node == 0 || lock == 0) {
		printf ("Device %s not found\n", ddrive);
		quit (1);
	}
	disk_info = (struct InfoData *) malloc (sizeof (struct InfoData));
	startup_msg = (struct FileSysStartupMsg *) BADDR (device_node->dn_Startup);
	driver = (char *) BADDR (startup_msg->fssm_Device) + 1;
	envptr = (long *) BADDR (startup_msg->fssm_Environ);
	base_offset = envptr[DE_LOWCYL] * envptr[DE_NUMHEADS] *
		envptr[DE_BLKSPERTRACK] * 512;
	if (disk_info != NULL) {
		if (Info (lock, disk_info) == 0) {
			UnLock (lock);
			return (-1);
		}
		*pfree = disk_info->id_NumBlocks - disk_info->id_NumBlocksUsed;
	}
	UnLock (lock);
	*ptot = (envptr[DE_UPPERCYL] - envptr[DE_LOWCYL] + 1) *
			envptr[DE_NUMHEADS] * envptr[DE_BLKSPERTRACK];
	*pbytes = 512;		/* bytes/sector */
	read_port = CreatePort(0, 0);
	if (read_port == NULL)
		quit(100);
	read_req = (struct IOExtTD *) CreateExtIO(read_port,
	    sizeof(struct IOExtTD));
	if (read_req == NULL)
		quit(200);
	error = OpenDevice(driver, startup_msg->fssm_Unit, read_req, -1);
	if (error) {
		printf("\nOpenDevice return was: %lx", error);
		return (-1);
	}
	return (1);
}

match (s1, s2)
register char *s1, *s2;
{
	register char c;
	while ((c = toupper (*s1++)) == toupper (*s2++))
		if (c == 0)
			return (0);
	if (c == ':' && !*--s2)
		return (0);
	return (1);
}

cleanup()
{
	if (read_req) {
		read_req->iotd_Req.io_Length = 0;
		read_req->iotd_Req.io_Command = TD_MOTOR;
		DoIO(read_req);
		DeleteExtIO(read_req);
	}
	if (read_port)
		DeletePort(read_port);
}

quit(code)
{
	cleanup();
	exit(code);
}

Abort (msg)
char	*msg;
{
	printf (msg);
	quit (0);
}

Chk_Break ()
{
	return (SetSignal (0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C);
}