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

    MODULE
	VolName.c

    DESCRIPTION
	Get one filename on Commandline
	and write to StdOut its Volumename

    NOTES
	Kickstart 2.0+ required
	compiles w/ Dice 2.07R - inline-pragmas required
	compiles w/ SAS/C v6.51

    BUGS
	We may use 'Lock', so VolName fails, if the
	file does't exist or is exclusively locked

    TODO

    EXAMPLES

    SEE ALSO

    INDEX

    HISTORY
	08-02-95 b_noll created
	11-02-95 b_noll enabled 'DEVICE/S', changed 'PHYSICAL' to 'VOLUME'

    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
**************************************/

static void DLName2CSTR(BPTR bstr, STRPTR buf, WORD bsize);

#if !defined(_DCC)
/* SAS/C and GNU/C have an '__inline' directive */

static __inline void DLName2CSTR(BPTR bstr, STRPTR buf, WORD bsize) {
    STRPTR x,y;
    int z;
    x = (STRPTR)BADDR(bstr);
    y = buf;
    z = *(x++);
    if (z >= bsize) z = bsize - 1;
    while (z--) *(y++) = *(x++);
    *(y++) = ':';
    *y	   = 0;
} /* DLName2CSTR */
#endif

long _main (void)
{
    const char* version = "$VER: VolName 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,VOLUME/S,DEVICE/S", (LONG*)argv, NULL)) {
	    APTR	    processwin;
	    struct Process *process;
	    STRPTR	    filename = argv[0];
	    UBYTE	    buffer[MAXPATHLEN];

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

	    if (argv[2] || argv[1]) {

		BPTR lock;

		if (lock = Lock(filename, SHARED_LOCK)) {
		    struct FileLock *fl;
		    struct DosList  *dl;

		    fl = BADDR(lock);
		    dl = BADDR(fl->fl_Volume);

		    if (argv[2]) {
			struct MsgPort	*mp;

			mp = dl->dol_Task;

			dl = LockDosList(LDF_DEVICES|LDF_READ);
			while (dl = NextDosEntry(dl,LDF_DEVICES)) {
			    if (dl->dol_Task == mp) {
				DLName2CSTR(dl->dol_Name, buffer, sizeof(buffer));
				filename = buffer;
				break;
			    } /* if Found */
			} /* while Searching */
			UnLockDosList(LDF_DEVICES|LDF_READ);
		    } else {
			NameFromLock(lock, buffer, sizeof(buffer));
			/* DLName2CSTR(dl->dol_Name, buffer, sizeof(buffer)); */
			filename = buffer;
		    } /* if (!)Device */

		    UnLock (lock);
		} /* if Lock */
	    } /* if DoLocking */



	    /* ---- Extract the Volumepart */
	    if (filename) {
		UBYTE c, *p;
		for (p = filename; (c = *p) && c != ':'; ++p);
		if (c) {
		    ++p;
		    c = *p;
		    *p = 0;
		    if (PutStr (filename) == 0) retval = 0;
		    *p = c;
		} /* if */
	    } /* if */
	    PutStr ("\n");


	    if (process != NULL) {
		process->pr_WindowPtr = processwin;
	    } /* if */

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

#if defined(_DCC)

/* DICE has no '__inline' directive; we must set the support
   function behind the main function, cause the program is
   started w/ the 1st opcode */

static void DLName2CSTR(BPTR bstr, STRPTR buf, WORD bsize) {
    STRPTR x,y;
    int z;
    x = (STRPTR)BADDR(bstr);
    y = buf;
    z = *(x++);
    if (z >= bsize) z = bsize - 1;
    while (z--) *(y++) = *(x++);
    *(y++) = ':';
    *y	   = 0;
} /* DLName2CSTR */
#endif

/******************************************************************************
*****  END VolName.c
******************************************************************************/

