#include <pragmas.h>
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/memory.h>
#include <libraries/dos.h>

extern struct MinList templist;

/* See 'pp.c' for a more detailed description of this struct */
struct filenode
{
	struct MinNode mn;

	BPTR filehandle;			/* Our new file */
	char *new_filename;
	BPTR orig_file;
	short dirty;
};

/* Add a file node to the list of files which we have created. These
** exist temporarily in 'temppath' and we need to get rid of these along
** the way, as they are Close()'d.
*/
addfilenode(register BPTR fn, register char *filename, register BPTR orighndl)
{
	register struct filenode *memgot;

	if
	(
		(memgot = AllocMem(sizeof(*memgot),MEMF_CLEAR))		 &&
		(memgot->new_filename  = AllocMem(strlen(filename)+1,0))
	)
	{
		memgot->filehandle = fn;
		strcpy(memgot->new_filename,filename);
		memgot->orig_file = orighndl;
		Forbid();
		AddTail((struct List *)&templist, (struct Node *)memgot);
		Permit();
		return(1);
	}
	else
		return(0);
}

/* Find a filenode (keyed by its filehandle) */
struct filenode *findfilenode(register BPTR fn)
{
	register struct filenode *search;

	/* This function does a Forbid() on entry, so that the list is
	** not changed while we're searching in it
	*/

	Forbid();

	/* Linear search is employed */
	for
	(
		search = (struct filenode *)(templist.mlh_Head);
		search->mn.mln_Succ;
		search = (struct filenode *)(search->mn.mln_Succ)
	)
		if (search->filehandle == fn)
			break;

	Permit();

	return(search->mn.mln_Succ? search : NULL);
}
