Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ptsfa!well!ans
From: ans@well.UUCP (Anne Schweizer)
Newsgroups: comp.sys.amiga
Subject: Re: How to cd
Summary: Don't forget to Examine it.
Keywords: cd,Examine,Lock
Message-ID: <4709@well.UUCP>
Date: 12 Dec 87 01:03:44 GMT
References: <796@rocky.STANFORD.EDU> <1237@sugar.UUCP> <810@rocky.STANFORD.EDU>
Reply-To: ans@well.UUCP (Anne Schweizer)
Organization: Whole Earth 'Lectronic Link, Sausalito, CA
Lines: 42

In article <796@rocky.STANFORD.EDU>, I wrote:
> What's the correct way to change directories in a program without going
> back to the original directory at the end? 

newdir = Lock(destdir,SHARED_LOCK;
if(newdir) Unlock(CurrentDir(newdir));

This seems to be enough to prevent gurus.
But especially in cases, where destdir isn't a static defined string,
you should check if destir really is a dir !! It could also be a file !!

mycd(destdir)
char *destdir;
{
   struct FileInfoBlock *finfo;
   struct FileLock *newdir;

   if(!Lock(destdir,HARED_LOCK)) return(FALSE):
   if(!AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC)))
   {
      UnLock(newdir);
      return(FALSE);    /* Should return out of memory */
   }
   if(!Examine(newdir,finfo))
   {
      UnLock(newdir);
      FreeMem(finfo,sizeof(struct FileInfoBlock));
      return(FALSE):    /* Should return can't examine lock */
   }
   if(finfo->fib_DirEntryType < 0 )
   {
      UnLock(newdir);
      FreeMem(finfo,sizeof(struct FileInfoBLock));
      return(FALSE):    /* LOCK IS NOT A DIRECTORY !!!!!!!!!! */
   }
   UnLock(CurrentDir(newdir));
   FreeMem(finfo,sizeof(struct FileInfoBlock));
   return(TRUE);        /* Allright now ...... */
}

--
	Anne