			/*   jk-net v0.5 copyright by infinity 1993,94   */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/lists.h>
#include <exec/nodes.h>

#include <libraries/dos.h>
#include <libraries/dosextens.h>
 
#include <proto/dos.h>
#include <proto/exec.h>

#define LOAD_code 0x01
#define SAVE_code 0x02
#define DIR_code 0x03
#define QUIT_code 0x04
#define CRAP_code 0xFF

void open_par(void);
void close_par(void);
int check_code(void);
void load(void);
void save(void);
void dir(void);
int copy_dir(char *,char *,int);
void quit(void);

ULONG par_read_id;
ULONG par_write_id;

void open_par()
{
	if ((par_read_id = Open("PAR:",MODE_OLDFILE)) == NULL)
		{
			printf("Can't open PAR: for read\n");
			exit(0);
		}

	if ((par_write_id = Open("PAR:",MODE_NEWFILE)) == NULL)
		{
			printf("Can't open PAR: for write\n");
			if (par_read_id) Close(par_read_id);
			exit(0);
		}
}

void close_par()
{
	if (par_read_id) Close(par_read_id);
	if (par_write_id) Close(par_write_id);
}

int check_code(void)
{
	UBYTE code=NULL;

	Read(par_read_id,&code,sizeof(code));

	switch(code)
		{
			case LOAD_code:
				load();
				return(FALSE);
			case SAVE_code:
				save();
				return(FALSE);
			case DIR_code:
				dir();
				return(FALSE);
			case QUIT_code:
				quit();
				return(TRUE);
			case CRAP_code:
				break;
			default:
				printf("Illegal code from C64\n");
		}
	return(FALSE);
}

void load()
{
	int i;
	ULONG file_read_id;
	ULONG lenght;
	UWORD wlenght;
	char name[32];
	char name_64[17];
	UBYTE *buffer;
	UBYTE *buffer2;
	UWORD nolla=NULL;

	strcpy(name,"C64_HD:");

	printf("Load code from C64\n");
	Read(par_read_id,&name_64,16);

	i=0;
	name_64[16]=' ';
	while(name_64[++i] != ' ');
	name_64[i]=NULL;

	i=0;
	while(name_64[i])
		name[i+7]=name_64[i++];
	name[i+7]=NULL;

	printf("name: %s\n",name);

	if ((file_read_id = Open(name,MODE_OLDFILE)) == NULL)
		{
			printf("Can't open %s for read\n",name);
			Write(par_write_id,&nolla,2);
			return;
		}

	if	((buffer = AllocMem(2+65536,MEMF_PUBLIC)) == NULL)
		{
			printf("Can't allocate memory\n");
			Write(par_write_id,&nolla,2);
			return;
		}

	if	((buffer2 = AllocMem(4+65536,MEMF_PUBLIC)) == NULL)
		{
			printf("Can't allocate memory\n");
			Write(par_write_id,&nolla,2);
			return;
		}

	lenght=Read(file_read_id,buffer,2+65536);
	wlenght=(UWORD)(lenght-2);
	printf("lenght: %d\n",wlenght);

	buffer2[0]=(UBYTE)(wlenght%256);
	buffer2[1]=(UBYTE)(wlenght/256);

	i=0;
	while(i!=2+lenght)
		buffer2[i+2]=buffer[i++];

	Write(par_write_id,buffer2,2+lenght);

	Close(file_read_id);

	FreeMem(buffer,2+65536);
	FreeMem(buffer2,4+65536);
}

void save()
{
	int i;
	ULONG file_write_id;
	ULONG lenght;
	UWORD wlenght;
	char name[32];
	char name_64[17];
	UBYTE *buffer;

	strcpy(name,"C64_HD:");

	printf("Save code from C64\n");
	Read(par_read_id,&name_64,16);

	i=0;
	name_64[16]=' ';
	while(name_64[++i] != ' ');
	name_64[i]=NULL;

	i=0;
	while(name_64[i])
		name[i+7]=name_64[i++];
	name[i+7]=NULL;

	printf("name: %s\n",name);

	if ((file_write_id = Open(name,MODE_NEWFILE)) == NULL)
		{
			printf("Can't open %s for write\n",name);
			return;
		}

	Read(par_read_id,&wlenght,2);
	lenght=(ULONG)wlenght;
	printf("lenght: %d\n",lenght);

	if ((buffer = AllocMem(2+lenght,MEMF_PUBLIC)) == NULL)
		{
			printf("Can't allocate memory\n");
			return;
		}

	Read(par_read_id,buffer,2+lenght);
	Write(file_write_id,buffer,2+lenght);

	Close(file_write_id);

	FreeMem(buffer,2+lenght);
}

void dir()
{
	struct FileInfoBlock fib;
	char dir[65535];
	char text[40];
	ULONG lenght = 0;
	ULONG lock = NULL;
	int point = 0;
	int i;

	strcpy(text,"0 \x12\"AMIGA           \" ID 2A\x92\xd\0");
	point=copy_dir(text,dir,point);

	printf("Dir code from C64\n");
 
	if ((lock = Lock("C64_HD:\0",ACCESS_READ)) == NULL)
		{
			printf("Can't find C64_HD:\n");
			return;
		}

	if (Examine(lock,&fib)==NULL)
		{
			printf("Can't examine C64_HD:\n");
			if (lock) UnLock(lock);
			return;
		}

	if (fib.fib_DirEntryType < 0)
		{
			printf("Not a dircetory\n");
			if (lock) UnLock(lock);
			return;
		}

	while (ExNext(lock,&fib))
		{
			sprintf(text,"%-5d\"%-16s  PRG\xd\0",fib.fib_Size/254+1,fib.fib_FileName);

			i=6;
			while(text[++i]!=' ');

			text[i]='"';

			point=copy_dir(text,dir,point);

			lenght += fib.fib_Size;
		}

	if (lock) UnLock(lock);

	sprintf(text,"%d BLOCKS USED.\xd\0",lenght/254);
	point=copy_dir(text,dir,point);

	dir[point]=(char)NULL;

	Write(par_write_id,&dir,point+2);
}

int copy_dir(char *text,char *dir,int i)
{
	int j=0;

	while(dir[i++]=text[j++]);
	return(i-1);
}

void quit()
{
	printf("Quit code from C64\n");
}

main()
{
	int stop=FALSE;

	open_par();

	while(!stop)
		(stop=check_code());

	close_par();

	return(0);
}
