Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!lll-winken!sun-barr!newstop!sun!stpeter!cmcmanis From: cmcmanis@stpeter.Sun.COM (Chuck McManis) Newsgroups: comp.sys.amiga.tech Subject: Re: CurrentDir Message-ID: <133060@sun.Eng.Sun.COM> Date: 18 Mar 90 21:03:19 GMT References: <00173.AA00173@starsoft.UUCP> Sender: news@sun.Eng.Sun.COM Reply-To: cmcmanis@sun.UUCP (Chuck McManis) Organization: Sun Microsystems, Mountain View Lines: 82 [Lock question, included below] The answer is, you are responsible for freeing any Locks that you have aquired. So if you use a "lock creating" call such as Lock() or ParentDir() then you will have to free the lock that was returned. You are forbidden from freeing locks that you didn't create. And in the case of CurrentDir it returns you a stashed lock and puts yours in the stash. Bad news if you free the one you got back from CurrentDir because you didn't create it, and even worse news if you free the one that is currently in the stash. To build a chdir() system call your startup code should really have oldlock = CurrentDir(0); mylock = DupLock(oldlock); (void) CurrentDir(mylock); ... /* Now you own all of the locks in your process */ /* Then in _exit() */ ... loch = CurrentDir(oldlock); /* put the old one back */ if (loch) UnLock(loch); ... What this does is save the current directory lock away that you didn't create in a safe place, and put in it's place a lock that you did create so your application can free if it chooses and the chdir call can be coded as : chdir(dir) char *dir; { ULONG lck; ULONG olck; lck = Lock(dir, ACCESS_READ); if (lck) { olck = CurrentDir(lck); if (olck) UnLock(olck); } else { errno = NOSUCHDIR; return -1; } return 0; } And you will be assured that you won't ever unlock a lock you don't own. --Chuck McManis uucp: {anywhere}!sun!cmcmanis BIX: cmcmanis ARPAnet: cmcmanis@sun.com These opinions are my own and no one elses, but you knew that didn't you. -------------Included Question------------- In article <00173.AA00173@starsoft.UUCP> (Dave Lowrey) writes: >Reading in Rob Peck's "Programmer's Guide to the Amiga" about >the CurrentDir function, he says the proper sequence to use is: > > mylock = Lock("df0:c", ACCESS_READ); > oldlock = CurrentDir(mylock); > >Now, he also says that I shouldn't free any locks that are returned >by CurrentDir, only ones I obtained by Lock. > >I understand this so far. However, is it legal to add the following >line immediately AFTER the CurrentDir call: > > Unlock(mylock); > >Would this pull the rug out from under DOS, or does it get it's own >lock on the current directory? > >The reason i'm asking this is that I am trying to simulate a UNIX >"chdir()" system call. I don't want to have to figure out a way to >free up all of the locks at the end of my program, if I don't have >to. --Chuck McManis uucp: {anywhere}!sun!cmcmanis BIX: cmcmanis Internet: cmcmanis@Eng.Sun.COM These opinions are my own and no one elses, but you knew that didn't you. "If it didn't have bones in it, it wouldn't be crunchy now would it?!"