/** pathname.c
*
*                     Copyright 1988, W.G.J. Langeveld
*                           All Rights Reserved
*                           Freely Distributable
*
*	Expand path name given a lock on a file name. Emulates ARP function
*   of the same name, which is slightly unreliable.
*
**/
#include <exec/exec.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <functions.h>
#include <stdio.h>

char *index();

#define FIBSIZ ((long) sizeof(struct FileInfoBlock))

int PathName(plock, buffer, n)
struct FileLock *plock;
char *buffer;
int n;
{
   struct FileLock *lock, *curdir;
   char *temp, *pos;
   struct FileInfoBlock *filinf;

   lock = NULL;
   filinf = NULL;
   temp = NULL;
   buffer[0] = '\0';

   filinf = (struct FileInfoBlock *) AllocMem(FIBSIZ, MEMF_PUBLIC|MEMF_CLEAR);
   if (filinf == NULL) goto cleanuplk;

   temp = (char *) AllocMem(255L, MEMF_PUBLIC|MEMF_CLEAR);
   if (temp == NULL) goto cleanuplk;

   if (Examine(plock, filinf) == NULL) goto cleanuplk;
   strcpy(buffer, filinf->fib_FileName);
   lock = ParentDir(plock);
/*
*  Now loop back through the parent directories until root is found.
*  Meanwhile, keep track of the names.
*/
   while (lock != NULL) {
      if (Examine(lock, filinf) == NULL) goto cleanuplk;

      strcpy(temp, buffer);
      strcpy(buffer, filinf->fib_FileName);
      strcat(buffer, "/");
      strcat(buffer, temp);

      curdir = ParentDir(lock);
      UnLock(lock);
      lock = curdir;
   }
/*
*   Now fix up the name
*/
   if (pos = index(buffer, '/')) {
      *pos = ':';
      if (pos == buffer) {
         strcpy(temp, "ram");
         strcat(temp, buffer);
         strcpy(buffer, temp);
      }
   }

cleanuplk:
   
   if (filinf) FreeMem(filinf, FIBSIZ);
   if (temp)   FreeMem(temp, 255L);
   if (lock)   UnLock(lock);

   return(strlen(buffer));
}

