/* pathname.c - expand a file's pathname */

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

	ExpandPath - general-purpose function to expand a pathname

	Usage:

	(void) ExpandPath (shortname, fullname);

	By: Tim Holloway
	Date Written: December 25, 1986
	Change History: none

	Copyright (C) 1986, by Tim Holloway.  May be freely distributed,
	as long as copyright notice is not removed.

	Description: Takes an AmigaDOS filename and expands it to a
	full file path description. I.e: "xxx" to "VOLxxx/yyy/zzz/.../xxx"
	Any legal AmigaDOS filename may be used.  If the name is null,
	returns the path name of the current directory (under 1.2 AmigaDOS,
	at least - this is an undocumented feature of the Lock function).
	If the named file does not exist or an error occurred, returns a
	null string as the expanded pathname.

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

#include "exec/types.h"
#include "exec/memory.h"
#include "libraries/dos.h"

struct FileInfoBlock fib;

typedef ULONG LOCK;

static int
parent_name(xlock, longname)
LOCK xlock;
char *longname;
{
	LOCK ylock;
	register int i, j;

	ylock = ParentDir (xlock);
	if (ylock == 0) return 0;

	i = parent_name(ylock, longname);
	if (!Examine(ylock, &fib))
	{
		return 0;
	}
	strcpy (longname+i, fib.fib_FileName);
	j = i+strlen(fib.fib_FileName);
	if (i == 0)
		longname[j++] = ':';  /* root directory is special */
	else
		longname[j++] = '/';
	UnLock (ylock);

/*  printf ("Concatenated to create %s\n", longname); */
	return j;
}

ExpandPath(shortname, longname)
char *shortname, *longname;
{
	register int i;
	LOCK pathlock;

	longname[0] = '\0';
	if ( (pathlock=Lock(shortname, ACCESS_READ)) == 0)
	{
		return;
	}

	i = parent_name(pathlock, longname);	
	if (!Examine(pathlock, &fib))
	{
		return;
	}
	strcpy (longname+i, fib.fib_FileName);
	UnLock (pathlock);
}

#ifdef TESTMODE
main(argc, argv)
int argc;
char *argv[];
{
	char full_name[128];

	printf ("expand pathname: %s\n", argv[1]);
	ExpandPath(argv[1], full_name);
	printf ("expanded name is '%s'\n", full_name);
}
#endif
