#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
#include <string.h>
#include <ctype.h>

void parspath(char * path)
{								/* this function converts a pathname */
								/* into a full pathname */
								/* it uses the string you pass it */
	char drive[MAXDRIVE];		/* used for splitting path */
	char dir[MAXDIR];
	char fname[MAXFILE];
	char ext[MAXEXT];
	char curpath[MAXPATH];		/* used to re-merge path */
	char pathtemp[80]="\0",dirtemp[80]="\0"; /* used as temporary strings */
	char tempstr[80];
	register int a;

	fnsplit(path,drive,dir,fname,ext);	/* split path */
	if ( (ext[0] == '\0') && (fname[0]!='\0') &&
			( (dir[(strlen(dir)-1)]=='\\') || (strlen(dir) == 0) ) )
	{							/* no extension & either */
								/* no directory or dir ends with \ */
								/* filename is PROBABLY a directory name */
		strcat(dir,fname);
		fname[0] = '\0';
	}
	a=getdisk();
	if (drive[0] == '\0')
	{						/* if no drive specified, use current drive */
		drive[0]=a+'A';
		drive[1]=':';
		drive[2] = '\0';
	}
	drive[0]=toupper(drive[0]);
	if (dir[0] == '\0')
	{			/* if no directory, use current directory on that drive */
		getcurdir((drive[0]-'A'+1),curpath);
		strcpy(dirtemp,curpath);
	}
	else
	{							/* if directory, parse dir. */
		register char * x,* y;

		getcurdir((drive[0]-'A'+1),curpath);
		y = dir;
		x = y;
		if (y[0]!='\\') strcpy(dirtemp,curpath);/* not root; use current */
		while (*y)
		{
			x=strchr(y,'\\');					/* use part before 1st \ */
			if (x==NULL) x=y+strlen(y);			/* no \.  use remainder */
			strncpy(tempstr,y,(size_t)(x-y));	/* copy to temp string */
			tempstr[(x-y)]='\0';				/* teminate with null */

			if (dirtemp[0] == '\0')
			{							/* no working directory */
				if (strcmp(tempstr,".")==0)
				{						/* current directory */
					strcpy(dirtemp,curpath);
				}
				else if (strcmp(tempstr,"..")==0)
				{						/* parent of current directory */
					register char * z;
					strcpy(dirtemp,curpath);			/* get current dir */
					z=strrchr(dirtemp,'\\');		/* remove last \ */
					if (z != NULL )
						*z='\0';					/* done */
				}
				else
				{						/* use root directory specified */
					strcpy(dirtemp,tempstr);
				}
			}
			else
			{							/* modify working directory */
				if (strcmp(tempstr,".")==0)
				{
					;	/* do nothing - already in current directory */
				}
				else if (strcmp(tempstr,"..")==0)
				{			/* remove last directory in working dir */
					register char * z;
					z=strrchr(dirtemp,'\\');	/* find last directory */
					if ( z!= NULL)
						*z='\0';				/* remove it */
				}
				else
				{						/* add directory to working */
					strcat(dirtemp,"\\");
					strcat(dirtemp,tempstr);
				}
			}
			y=(x+1);				/* move past \ */
			if ((*x)=='\0') y=x;	/* end if at end of string */
		}
	}
	if (fname[0] == '\0')
	{								/* no filename - use * */
		strcpy(fname,"*");
	}
	if (ext[0] == '\0')
	{								/* no extension - use .* */
		strcpy(ext,".*");
	}
	strcpy(dir,"\\");				/* place \ at beginning of dir */
	strcat(dir,dirtemp);			/* copy working dir to dir */
	fnmerge(pathtemp,drive,dir,fname,ext);	/* re-merge pathname */
	strcpy(path,pathtemp);			/* move it to string passed in */
}