Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!gamma!mibte!fmsrl7!eecae!nancy!mailrus!ames!pasteur!ucbvax!USU.BITNET!SLMYQ From: SLMYQ@USU.BITNET Newsgroups: comp.sys.amiga.tech Subject: Changing Directories Message-ID: <8804060734.AA13918@jade.berkeley.edu> Date: 6 Apr 88 07:22:00 GMT Sender: usenet@ucbvax.BERKELEY.EDU Lines: 58 Here are some hints for changing the current directory in a program. First of all, just using CurrentDir(newdir) IS ABSOLUTELY NO GOOD. It will lose 24 bytes of memory each time, because you didn't free the lock on the old directory. Although 24 bytes isn't much, think of how many times you might be changing the current directory. You need to UnLock the lock returned by CurrentDir(). However, remember that CurrentDir() *might* not return a lock - just a NULL, if you're changing the directory from the initial startup directory to something else. My suggestion is to create a routine like this and always use it to change directories. VOID ChangeDir(NewDir) BPTR NewDir; { BPTR OldDir; if ((OldDir = (BPTR)CurrentDir(NewDir)) != NULL) UnLock(OldDir); } Next suggestion. Unless you are writing a CD program, you really should save and restore the old current directory, so if you start a program from the CLI in one directory, you don't wind up in another directory afterwards. However, if you use the ChangeDir() routine above, beware that it is not enough that you simply save the old lock pointer. If you did this, then the first time you did a ChangeDir(), that lock would be freed, and your pointer would become invalid. You need to DupLock() that lock, and save it. Then after the program is finished, just ChangeDir() to the duplicate lock. Final suggestion. This is merely to avoid confusion. Put a few lines at the beginning of your program like this: struct Process *MyProcess = FindTask(NULL); if (MyProcess->pr_CurrentDir == NULL) { ChangeDir(Lock("SYS:",ACCESS_READ)); } Of course, you really should check to make sure the lock returned by the Lock() call actually succeeded. Now if you're wondering why the current directory could possibly be zero, it's because of one of those weird things in AmigaDOS. When a process is first created, its pr_CurrentDir is set to zero, rather than some specific lock. With the BCPL commands, this zero lock "assumes" the SYS: directory. For your programs, it would probably be easier just to get a *real* lock on the SYS: directory and set it to that, and then you don't have to worry about yourself being nowhere at all. :) Hope this helps, and I hope I stop seeing programs losing memory every time they run :) Bryan Bryan Ford //// A computer does what \\\\ Snail: 1790 East 1400 North //// you tell it to do, not \\\\ Logan, UT 84321 \\\X/// what you want it to do. \\\X/// Email: SLMYQ@USU.BITNET \XXX/ Murphy's Law Calendar 1986 \XXX/