/*-- AutoRev header do NOT edit!
*
*   Program         :   PLSTmatch.c
*   Copyright       :   © Copyright 1992 Authentic Visuals
*   Author          :   G. J. Hillebrand
*   Creation Date   :   14-May-92
*   Current version :   1.00
*   Translator      :   DICE v2.6
*
*   REVISION HISTORY
*
*   Date          Version         Comment
*   ---------     -------         ------------------------------------------
*   14-May-92     1.00            Initial release.
*
*-- REV_END --*/

#include <stdio.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

#define ABSSUB(a,b)	((a-b)<0?-(a-b):(a-b))

struct FileInfoBlock fileinfoblock;
APTR *the_lock, *fh;
UBYTE *hdr;
LONG size;
int the_length;

void
main(char argc,char *argv[])
{
int i,j,sum, the_diff;
char text[24];

	if (argc>=2)
	{
		if ((the_lock = Lock("PLST", ACCESS_READ)) ||
			(the_lock = Lock("ST-00:PLST", ACCESS_READ)))
		{
			if (Examine(the_lock,&fileinfoblock))
			{
				size = (ULONG) fileinfoblock.fib_Size;
				hdr = (UBYTE *)AllocVec(size,MEMF_PUBLIC);
			}

			if (fh = OpenFromLock(the_lock))
			{
				Read(fh,hdr,size);
				Close (fh);
				UnLock(the_lock);
			}
			else
			{
				fprintf(stderr,"%s: %s\n","Error","Can not load PLST");
				exit(5);
			}
		}
		else
		{
			fprintf(stderr,"%s: %s\n","Error","Can not find PLST");
			exit(10);
		}

		if (*argv[1]=='$')
			sscanf (&argv[1][1],"%x",&the_length);
		else
			sscanf (argv[1],"%d",&the_length);

		if (argc == 2)
			the_diff =0;
		else
			sscanf (argv[2],"%d",&the_diff);

		for (i=0;i<size;i += 30)
		{
			sum = 2*(256*hdr[i+22] + hdr[i+23]);
				/* PLST contains length/2 */
			if (ABSSUB(sum,the_length) <= the_diff)
			{
				for (j=0;j<22 && hdr[i+j]!=0;j++)
					text[j]=hdr[i+j];
				text[j]='\0';
				printf ("%s = %x\n",text,sum);
			}
		}
		FreeVec (hdr);
	}
	else
	{
		printf ("PLSTmatch -  ©1992 G.J. Hillebrand\n");
		printf ("USAGE: PLSTmatch <[$]sample_length> [<margin>]\n");
	}
}

