[comp.sys.amiga] Proper use of CurrentDir

dillon@CORY.BERKELEY.EDU.UUCP (03/02/87)

	The mistake many people make is to UnLock() the directory lock they
had while it is still installed as the Current Directory.  When you exit a 
program, you always leave a *valid* directory lock on CurrentDir().  E.G:

	LOCK *lock;

	lock = Lock(...)	lock some file
	UnLock(CurrentDir(lock));
	UnLock(lock);			/* WRONG */
	exit()


	In Fact, on return, the CLI doesn't care if the lock in CurrentDir()
is the same as the one originally in CurrentDir().  As far as I know, you
could even leave a NULL in the current directory.  But one thing you absolutely
cannot do is leave a pointer that has already been deallocated.

For instance, a program like CD does something like this:

	lock = Lock(...)	Get new direcory lock
	if (...)		make sure it's a valid lock and a directory
	oldlock = CurrentDir(lock)
	if (oldlock)
		UnLock(oldlock);
	exit(0);

	As you can see, CD replaces the current lock with the new directory
	lock.  Note, however, that there is NO GUARENTEE that the ORIGINAL
	lock (oldlock) that was in CurrentDir() to start with existed... it
	could be NULL. 

			-Matt