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

    MODULE
	Exists.c

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

    NOTES
	Kickstart 2.0+ required
	compiles w/ Dice 2.07R - inline-pragmas 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

    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>

/**************************************
	    Global Variables
**************************************/

#define MAXPATHLEN 256

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

long _main (void)
{
    const char* version = "$VER: Exists 1.1 (11.02.95)";
    long retval = 20;
    struct Library* SysBase = *((struct Library**)4L);
    struct Library* DOSBase;

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
	STRPTR argv[] = { NULL, NULL, NULL };
	APTR   args;
	retval	 = 10;
	if (args = (void*)ReadArgs("FILE/A,NAME/S,FULL/S", (LONG*)argv, NULL)) {
	    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 = 5;
	    if ((lock = Lock(argv[0], ACCESS_READ))) {
		UBYTE buffer[MAXPATHLEN];
		retval = 0;

		if (argv[2]) {
		    NameFromLock(lock, buffer, sizeof(buffer));
		    ostr = buffer;
		} else if (argv[1]) {
		    ostr = argv[0];
		} else {
		    ostr = "1";
		} /* if */


		UnLock (lock);
	    } else {
		ostr = (argv[1] || argv[2]) ? "" : "0";
	    } /* if */

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

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

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

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

