/* elib.c -- an exec library compiled with Aztec 3.30b, small model		*/

/* created by jim mackraz using mylib.asm by neil katin
 * may be used and distributed providing this comment block
 * is retained in the source code
 */

#include "elib.h"

extern  PFL		libfunctab[];	/* my function table (libface.asm)		*/
extern	LONG	funkyInit();	/* hacked up version of Aztec crt0.a68	*/

LONG	myExpunge();

struct InitTable myInitTab =  {
	sizeof (struct MyBase),
	libfunctab,
	NULL,					/* will initialize my data in funkymain()	*/
	funkyInit
};

#define MYREVISION	0		/* would be nice to auto-increment this		*/

char myname[] = "mylib.library";
char myid[] = "mylib 1.0 (23 Oct 1986)\r\n";

extern struct Resident	myRomTag;

/*
 * this function is my C-language library initRoutine.  It is called
 * by funkyInit() after register saves and small model initialization is
 * done.
 */

LONG
funkymain(libbase, seglist)
struct	MyBase	*libbase;
ULONG			seglist;
{
	register	struct MyBase *base;

	/* cookie	*/
	base = libbase;
	base->mb_Cookie = 0xDEAD1234;	/* debug kind of stuff				*/
	base->mb_SegList = seglist;

	/* init. library structure (since I don't do automatic data init.)	*/
	base->mb_Lib.lib_Node.ln_Type = NT_LIBRARY;
	base->mb_Lib.lib_Node.ln_Name = (char *) myname;	
	base->mb_Lib.lib_Flags = LIBF_SUMUSED | LIBF_CHANGED;
	base->mb_Lib.lib_Version = myRomTag.rt_Version;
	base->mb_Lib.lib_Revision = MYREVISION;
	base->mb_Lib.lib_IdString = (APTR) myid;

	/* ----- do your own initialization here -----	*/
}

LONG
myOpen(base)	/* baseptr in A6, version in D0 */
struct	MyBase *base;
{
	/* mark us as having another customer					*/
	base->mb_Lib.lib_OpenCnt++;

	/* prevent delayed expunges (standard procedure)		*/
	base->mb_Lib.lib_Flags &= ~LIBF_DELEXP;

	return ((LONG) base);
}

LONG
myClose(base)
struct	MyBase *base;
{
	LONG	retval = 0;

	if ((--base->mb_Lib.lib_OpenCnt == 0) &&
			(base->mb_Lib.lib_Flags & LIBF_DELEXP))
	{
		/* no more people have me open,
		 * and I have a delayed expunge pending
		 */
		retval = myExpunge(); /* return segment list	*/
	}

	return (retval);
}

LONG
myExpunge(base)
struct	MyBase	*base;
{
	ULONG			seglist = 0;
	LONG			libsize;

	if (base->mb_Lib.lib_OpenCnt == 0)
	{
		/* really expunge: remove libbase and freemem	*/

		seglist	= base->mb_SegList;

		Remove(base);

		libsize = base->mb_Lib.lib_NegSize + base->mb_Lib.lib_PosSize;
		FreeMem((char *) base - base->mb_Lib.lib_NegSize, (LONG) libsize);
	}
	else
	{
		base->mb_Lib.lib_Flags &= LIBF_DELEXP;
	}


	/* return NULL or real seglist				*/
	return ((LONG) seglist);
}

LONG
GetDown()
{
	return (77);
}

ULONG
Double(arg)
ULONG arg;
{
	return (2 * arg);
}
