#include <stdio.h>
#include <string.h>
#include <dos.h>
#ifndef AZTEC_C
#include <proto/dos.h>
#endif
#include <libraries/dos.h>
#include "global.h"
#include "dirutil.h"

/* Given a working directory and an arbitrary pathname, resolve them into
 * an absolute pathname. Memory is allocated for the result, which
 * the caller must free
 */
char *
pathname(cd,path)
char *cd;	/* Current working directory */
char *path;	/* Pathname argument */
{
	BPTR	cdlock, pathlock, oldcdlock;
	static char filenamebuf[FMSIZE];
	register char *cp;

	if(cd == NULLCHAR || path == NULLCHAR)
		return NULLCHAR;

	/* Strip any leading white space on args */
	while(*cd == ' ' || *cd == '\t')
		cd++;
	while(*path == ' ' || *path == '\t')
		path++;


	/*
	 * Here's the plan:  change the actual working directory to the 
	 * directory specified by `cd'.  Then try to lock `path' relative
	 * to that directory.  Finally, compute the absolute path based on
	 * that lock.
	 */

	if (!(cdlock = Lock(cd, SHARED_LOCK)))
		return NULLCHAR;

	if (!(oldcdlock = CurrentDir(cdlock))) {
		/* this might fail:  `cd' might not have been a directory */
		UnLock(cdlock);
		return NULLCHAR;
	}

	if (!(pathlock = Lock(path, SHARED_LOCK))) {
		/*
		 *  Special kludge:
		 *
		 *  If we're trying to figure the name of a file which doesn't
		 *  exist, then we have to fake it.  Take the easy way out: 
		 *  don't allow `path' to have any path seperator characters in
		 *  it and just concatenate it to the real name of `cd'.  This
		 *  way we don't have to worry about someone trying to change
		 *  directories up using /// sequences.
		 */
		if (strpbrk(path, "/:")) {
			/* Sorry, too clever, you lose */
			UnLock(CurrentDir(oldcdlock));
			return NULLCHAR;
		}

		/* Get the real name of `cd' and concatenate `path' to it */
/*		if (getpath(cdlock, filenamebuf)) {*/
			UnLock(CurrentDir(oldcdlock));
			return NULLCHAR;
/*		}*/
		
		/* done with lock */
		UnLock(CurrentDir(oldcdlock));
		/*
		 *  Horrible kludge.  getpath() screws up and doesn't put a colon
		 *  on the volume name if it is the only component on the path.
		 */
		for (cp = filenamebuf; *cp && *cp != '/' && *cp != ':'; cp++)
			/* void */ ;

		if (*cp) {	/* did we find a pathname separator? */
			/* yes */
			cp = malloc(strlen(filenamebuf)+strlen(path) + 2);
			strcpy(cp, filenamebuf);
			strcat(cp, "/");
			strcat(cp, path);
			return cp;
		} else {
			cp = malloc(strlen(filenamebuf)+strlen(path) + 2);
			strcpy(cp, filenamebuf);
			strcat(cp, ":");
			strcat(cp, path);
			return cp;
		}
		/*NOTREACHED*/
	}

	/*
	 * we're done with the the cdlock Lock;  restore the current 
	 * working directory to what it was when we started.
	 */
	  
	UnLock(CurrentDir(oldcdlock));
	/*
	 *  Ok, now we've got a lock on the file specified relative to the
	 *  directory specified.  Get its absolute pathname.
	 */

/*	if (getpath(pathlock, filenamebuf)) {*/
		UnLock(pathlock);
		printf("pathname(%s,%s): getpath() fails\n", cd, path);
		return NULLCHAR;
/*	}*/

	UnLock(pathlock);

	/*
	 *  Horrible kludge.  getpath() screws up and doesn't put a colon
	 *  on the volume name if it is the only component on the path.
	 */
	for (cp = filenamebuf; *cp && *cp != '/' && *cp != ':'; cp++)
		/* void */ ;

	if (*cp)	/* did we find a pathname separator? */
		return strdup(filenamebuf);

	cp = malloc(strlen(filenamebuf)+2);
	strcpy(cp, filenamebuf);
	strcat(cp, ":");
	return cp;
}
