/* <:t-3:>
      This will print the working directory.
      I.e., this does something similar to what CD w/o args does.
      Note that "GetCD()" will work if compiled alone, and does not
      need stdio.h
*/

#include <libraries/dos.h>
#include <libraries/dosextens.h>

/* The GetCD function returns a pointer to a static string which is
overwritten on each call.   STRCPY for best results. */

char * GetCD()
{
   struct Process * ptr;
   struct FileInfoBlock * FIB;
   struct Lock * cdlock, * prevlock;
   static char newname[300];
   char oldname[300];

   ptr = (struct Process *) FindTask(0);   /* Get address of current task */
      /* ptr now points to "AmigaDOS Process values",
              see pg 3-1 tech ref, dosextens.h */
   FIB = (struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock), 0);
   if (FIB == 0) return NULL;    /* error check */

   cdlock = (struct Lock *) DupLock((ptr->pr_CurrentDir) /* << 2 */ );
      /* NOTE: Book says it's BPTR, but GURU when shifted, not GURU
         when not shifted.  Explain, please? */
   newname[0] = oldname[0] = '\0';

   do {
      cdlock = (struct Lock *) ParentDir(prevlock = cdlock);
      Examine(prevlock, FIB);
      strcpy(newname, FIB->fib_FileName);
      UnLock(prevlock);
      if (cdlock == 0) {
         /* prevlock was the root of a disk, and thus the name */
         strcat(newname, ":");
         strcat(newname, oldname);
         }
      else {
         strcpy(newname, FIB->fib_FileName);
         if (oldname[0]) {
            strcat(newname, "/");
            strcat(newname, oldname);
            }
         strcpy(oldname, newname);
         }
      } while (cdlock != 0);
   FreeMem(FIB, sizeof(struct FileInfoBlock));
   return newname;
   }


void main()
{
   printf("CD: \"%s\"\n", GetCD());
   exit(0);
   }



