#include <exec/types.h>
#include <exec/execbase.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <pragma/exec_lib.h>
#include <pragma/dos_lib.h>
#include <string.h>

#define SIZE	512

/*
Example of not using any C startup code at all!
Compiler options:
-----------------
	-mm to put strings in code segment
	-r4 allow us of a4 for register variables.
	-m0b to stop generation of XREF .begin to get crt0.a68 code from c.lib

*/

int main(void)
{
	char path[SIZE];
	char var[SIZE];
	BPTR dirlock;
	int	setvar = 1;
	int bufsize = SIZE-10;
	char *varname = "currentdir";
	struct DOSBase *DOSBase;
	struct ExecBase *SysBase;
	char *p;

	/* set SysBase pointer direct from $4 */
 	SysBase = (struct ExecBase *)*((long *)0x00000004);

	if ( (DOSBase = (struct DOSBase *)OpenLibrary("dos.library",36)) )
	{
		dirlock = Lock("",SHARED_LOCK);
		if (dirlock)
		{
			if (NameFromLock(dirlock,path,bufsize))
			{
				if (GetVar(varname,var,bufsize,GVF_GLOBAL_ONLY) > 0)
				{
					setvar = strcmp(path,var);
				}
			}
			if (setvar)
			{
				p = path;
				while (*p)
				{
					p++;
				}
				*p++ = '\n';
				*p = '\0';
				DeleteVar(varname,GVF_GLOBAL_ONLY);
				SetVar(varname,path,-1,GVF_GLOBAL_ONLY);
			}
			UnLock(dirlock);
		}
		CloseLibrary((struct Library *)DOSBase);
	}

	return 0;
}

