/** 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.
*
*   Mods to device name generation by Steve (Raz) Berry
*   12/31/88 (New Years Eve!)
*
**/

#include <stdio.h>

char *index();
extern void auto_req();

#define FIBSIZ ((long) sizeof(struct FileInfoBlock))

int PathName(plock, buffer)
struct FileLock *plock;
char *buffer;
{
   struct FileLock *llock, *curdir;
   char *temp, *pos, buf[100];
   struct FileInfoBlock *filinf;
    long rc;

   llock = 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;

#ifdef DEBUG
   if(plock == NULL)
    auto_req("lock is null");
#endif

   rc = Examine(plock, filinf);

   if (rc == NULL) goto cleanuplk;

   strcpy(buffer, filinf->fib_FileName);
   llock = ParentDir(plock);
/*
*  Now loop back through the parent directories until root is found.
*  Meanwhile, keep track of the names.
*/

   while (llock != NULL) {
      if (Examine(llock, filinf) == NULL) goto cleanuplk;

      strcpy(temp, buffer);
      strcpy(buffer, filinf->fib_FileName);
      strcat(buffer, "/");
      strcat(buffer, temp);

      curdir = ParentDir(llock);
      UnLock(llock);
      llock = curdir;
   }
/*
*   Now fix up the name
*/

   if (pos = index(buffer, '/')) {
      *pos = ':';
      if (pos == buffer) {
         strcpy(temp, "ram");
         strcat(temp, buffer);
         strcpy(buffer, temp);
      } 
   } else 
        strcat(buffer, ":");    /* Append : to root names - swb */

cleanuplk:

   if (filinf != NULL) FreeMem(filinf, FIBSIZ);
   if (temp != NULL)   FreeMem(temp, 255L);
   if (llock != NULL)   UnLock(llock);

   return(strlen(buffer));
}

