
/******************************************************************************

    MODULE
	Exists.c

    DESCRIPTION
	Get one filename on Commandline
	and write to StdOut if it exists

    NOTES
	Kickstart 2.0+ required
	compiles w/ SAS/C v6.51

    BUGS
	We use 'Lock', so Exists fails, if the
	file exists and is exclusively locked

    TODO
	Use another way than 'Lock' to determine
	the existance

    EXAMPLES
	> Exists Sys:System/CLI
	1

    SEE ALSO

    INDEX

    HISTORY
	20-03-94 b_noll created
	03-02-95 b_noll created (had forgotten the first version =8-})
	11-02-95 b_noll added FULL Keyword
	20-02-95 b_noll restructured source
	21-02-95 b_noll added version/format-prefix/offset
	20-03-95 b_noll added args diagnostics

    AUTHOR
	Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
	b_noll@informatik.uni-kl.de

******************************************************************************/

/**************************************
	    Includes
**************************************/

#ifndef   EXEC_LIBRARIES_H
# include <exec/libraries.h>
#endif /* EXEC_LIBRARIES_H */

#ifndef   CLIB_EXEC_PROTOS_H
# include <clib/exec_protos.h>
#endif /* CLIB_EXEC_PROTOS_H */

#ifndef   DOS_DOS_H
# include <dos/dos.h>
#endif /* DOS_DOS_H */

#ifndef   CLIB_DOS_PROTOS_H
# include <clib/dos_protos.h>
#endif /* CLIB_DOS_PROTOS_H */

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


/**************************************
	 Defines & Structures
**************************************/

#ifndef ABSEXECBASE
#define ABSEXECBASE ((struct ExecBase **)4L)
#endif

struct _arg {
/* ******************** USER FORMAT ******************** */
#define FORMAT "FILE/A,NAME/S,FULL/S"

    STRPTR file;
    ULONG  name;
    ULONG  full;

/* ******************** USER FORMAT ******************** */
}; /* struct _argv */

#define MAXPATHLEN 256
#define MAXLINELEN 256

#define VERSIONPREFIX	"\0$VER: "
#define VERSIONOFFSET	0
#define FORMATPREFIX	"\0$ARG: "
#define FORMATOFFSET	7

/**************************************
	    Implementation
**************************************/

long _main (void)
{
    const char* version = VERSIONPREFIX "Exists 1.3 " __AMIGADATE__ + VERSIONOFFSET;
    long retval = RETURN_FAIL;
    struct ExecBase*SysBase = *ABSEXECBASE;
    struct Library* DOSBase;

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
	struct _arg argv = { 0 };
	APTR   args;
	retval	 = RETURN_ERROR;
	if (args = (void*)ReadArgs(FORMATPREFIX FORMAT + FORMATOFFSET, (LONG*)&argv, NULL)) {
/* ******************** USER BODY ******************** */

	    APTR pw;
	    struct Process *p;
	    STRPTR ostr;
	    BPTR lock;

	    if ((p = (struct Process *)FindTask(NULL))) {
		pw = p->pr_WindowPtr;
		p->pr_WindowPtr = (APTR)-1;
	    } /* if */

	    /* ---- Existance check: rv==0->Exists, rv==5->n/e */
	    retval = RETURN_WARN;
	    {
#if 1
		UBYTE buffer[MAXPATHLEN];

		if ((lock = Lock(argv.file, ACCESS_READ))) {
		    retval = RETURN_OK;

		    if (argv.full) {
			NameFromLock(lock, buffer, sizeof(buffer));
			ostr = buffer;
		    } else if (argv.name) {
			ostr = argv.file;
		    } else {
			ostr = "1";
		    } /* if */

		    UnLock (lock);
		} else if (IoErr() == ERROR_OBJECT_IN_USE) {
		    retval = RETURN_OK;
		    ostr = (argv.name || argv.full) ? argv.file : "1";
		} else {
		    ostr = (argv.name || argv.full) ? "" : "0";
		} /* if */
#else

		struct AnchorPath  *ap;
		UBYTE  apb[sizeof (*ap) + 8 + MATHPATHLEN];
		ap		  = (void *)(((ULONG)apb + 7) & ~7);
		ap->ap_Strlen	  = MAXPATHLEN;
		ap->ap_BreakBits  = 0;
		ap->ap_FoundBreak = 0;
		ap->ap_Flags	  = 0;
		error = MatchFirst(argv.file, ap);
		MathEnd (ap);
		if (!error) {
		    retval = RETURN_OK;
		    ostr = (argv.full) ? ap->ap_Buf :
					 (argv.name) ? argv.file : "1";
		} else {
		    ostr = (argv.name || argv.full) ? "" : "0";
		} /* if */

#endif

		/* ----- Result output, either boolean(0/1) or named (name/"") */
		PutStr( ostr );
		PutStr( "\n" );
	    }

	    if (p != NULL) {
		p->pr_WindowPtr = pw;
	    } /* if */

/* ******************** USER BODY ******************** */
	    FreeArgs (args);
	} /* if */

	if (retval > RETURN_WARN)
	    PrintFault(IoErr(), "Exists");

	CloseLibrary (DOSBase);
    } /* if */
    return (retval);
} /* _main */

/******************************************************************************
*****  END Exists.c
******************************************************************************/

