/*
** openffile.c
**
** FileFinder Package Utilities: OpenFoundFile()
**
** $Id: openffile.c,v 1.0 94/08/09 17:05:37 GF Exp Locker: GF $
**
**************************************************************************
**
** Copyright © 1994 by Gabriele Falcioni. All Rights Reserved.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** For inquiries, comments, donations, bug reports, write to:
**
** Gabriele Falcioni
** via E. Cialdini, 50
** I-60122 Ancona
** ITALY
**
*/

#include <string.h>

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

#include <dos/dos.h>
#include <dos/stdio.h>
#include <dos/dosasl.h>
#include <dos/rdargs.h>

#include <proto/exec.h>
#include <proto/utility.h>
#include <proto/dos.h>

#include "filefinder.h"

/* -------------------------------------------------------------------- */

/****** filefinder.lib/OpenFoundFile *************************************
*
*	NAME
*		OpenFoundFile -- Open a (FileFinder) matching file.
*
*	SYNOPSIS
*		fh = OpenFoundFile(ff,mode);
*		d0                 a0 d0
*
*		BPTR OpenFoundFile(struct FileFinder *,LONG);
*
*	FUNCTION
*		It Open()s the file returned by the FileFinder matcher.
*
*	INPUTS
*		ff   - a pointer to a valid FileFinder structure.
*		mode - open mode, the same of Open() AmigaDOS call.
*
*	RESULT
*		fh - BCPL pointer to a file handle or NULL if Open() failed.
*
*	SEE ALSO
*		CreateFileFinderA(), DeleteFileFinder(), FindFiles(),
*		dos.library/Open(), dos.library/Close()
*
*************************************************************************/

BPTR ASM OpenFoundFile(REG(a0) struct FileFinder *ff, REG(d0) LONG mode)
{
	LONG errcode;
	BPTR olddir,newfile;
	struct AnchorPath *ap;

	ap = GetAnchorPath(ff);

	olddir = CurrentDir(ap->ap_Current->an_Lock);

	newfile = Open(ap->ap_Info.fib_FileName,mode);
	errcode = IoErr();

	(VOID)CurrentDir(olddir);

	SetIoErr(errcode);
	return newfile;
}
