/* pathinfo.c */
/**********************************************************************/
/* P A T H I N F O - a demonstration of jumping around the file       */
/*                   from within a C program.                         */
/*                                           by Tom Krotchko          */
/* use: type 'pathinfo' at CLI prompt.                                */
/* output: current directory path. Similar to 'cd' output.            */
/**********************************************************************/
#include "libraries/dos.h"
#include "libraries/dosextens.h"
#include "exec/memory.h"
#include "stdio.h"

extern struct FileLock *Lock(),*DupLock(),*ParentDir();
main()
{
   struct FileLock *pathlock;

   /* back all the way out to the root directory */
   pathlock = Lock("",ACCESS_READ);

   if(pathlock != 0)
   {
     printf("\nCurrent directory\n");

     /* don't print slash if at bottommost level */
     getpath(pathlock);
   }
   else
   {
     printf("\nERROR: Couldn't lock the current directory.");
   }
   printf("\n");
  /* for use from WORKBENCH to avoid window closing immediately */
   printf("Press RETURN ");
   getchar(); 
} /* end of main procedure */

int getpath(lock)
struct FileLock *lock;
{
   struct FileInfoBlock *fib;
   struct FileLock *newlock;
   char outstring[80];
   int success,error;

   /* after we're all done, don't do anything. */
   if(!lock) return(0);

   /* Memory allocation for the file information block. Necessary because */
   /* we don't know how much room we need until we actually need it!      */
   fib = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),
             MEMF_CLEAR);

   /* Always make sure you check whether or not the memory allocation     */
   /* its probably safe in this program, but failure to check will bring  */
   /* the GURU sooner or later.                                           */
   if(fib == 0)
   {
     printf("Couldn't allocate fib. System is in deep trouble.\n");
     return(0);
   }
   /* use ParentDir to see if there is a parent directory.                */
   newlock = ParentDir(lock);

   /* check if everything was okay.                                       */  
   error = IoErr();
   if(newlock == 0 && error != 0)
   {
      printf("\nFatal disk error. Return code is %ld",error);
      printf("\n");
   }
   
   /* Recursively call this function to climb directory path. Be careful  */
   /* when using recursive functions since a loop will send your Amiga    */
   /* into infinity.                                                      */
   getpath(newlock);
 
   /* use the Examine function to get information about the directory.    */
   /* Examine may be used on individual files as well.                    */
   success = Examine(lock,fib);
   if(success)
     {
     strcat(outstring,&fib->fib_FileName[0]);
     if(newlock == 0) /* This means we're at the root directory   */
     {
       strcat(outstring,":");
     }
     else
     {
       strcat(outstring,"/");
     }
     printf(outstring);
     UnLock(lock);
   }

   if(fib)
     FreeMem(fib, sizeof(struct FileInfoBlock));
   return(1);
}
