[comp.sys.amiga.tech] UnLocking locks returned by CurrentDir

a665@mindlink.UUCP (Anthon Pang) (12/05/90)

First of all, IMO, CurrentDir takes the old lock, frees it, and gives you a new
one.  Secondly, NULL isn't a valid lock value (returned by Lock, CurrentDir,
DupLock...)...it is used as a function parameter to specify the root directory
or system volume.

--
Disclaimer: just pulled an all nighter...so all of this is just MHO and
swiss-cheese memory.

laughlin@fornax.UUCP (Bob Laughlin) (12/05/90)

    I've been using the following code to move around directories 
from within a program and restore the original current directory
on termination.  It seems to work and I originally got the code
from an example posted here a few years ago.

 // at program startup store a lock on the current directory
 if ((startdir = Lock ("", ACCESS_READ)) == NULL)
    error ...
 ........
 // during the program move around the directories with ...
 if ((newlock = Lock (newdir, ACCESS_READ) != NULL)  {
    oldlock = CurrentDir (newlock) ;   // oldlock == NULL is a valid lock!
    UnLock (oldlock) ;
 }
 ........
 // at program termination restore the original directory
 oldlock = CurrentDir (startdir) ; 
 UnLock (oldlock) ;

   However, reading Rob Peck's Programmer's Guide to the Amiga
lately I notice that on page 30 it states that you should NOT
call UnLock() on locks obtained from a call to CurrentDir().
Also, the 1.3 AutoDocs only mention using UnLock() on a lock obtained
from Lock(), DupLock(), or CreateDir(). If this is correct then how do
you move around directories without leaving any locks around when the
program exits? 

 My guess is that you something like the following:  

 // at program startup store a lock on the current directory
 startdir = Lock ("", ACCESS_READ) ;
 ........
 // during the program move around the directories with ...
 if ((newlock = Lock (newdir, ACCESS_READ) != NULL)
    CurrentDir (newlock) ;

 ...... use newlock
 UnLock (newlock) ;
 }
 ........
 // at program termination restore the original directory
 CurrentDir (startdir) ; 
 UnLock (startdir) ;

 Is this correct?
-- 
 Bob Laughlin  laughlin@cs.sfu.ca 

ken@cbmvax.commodore.com (Ken Farinsky - CATS) (12/06/90)

In article <4046@mindlink.UUCP> a665@mindlink.UUCP (Anthon Pang) writes:
>First of all, IMO, CurrentDir takes the old lock, frees it, and gives you a new
>one.  Secondly, NULL isn't a valid lock value (returned by Lock, CurrentDir,
>DupLock...)...it is used as a function parameter to specify the root directory
>or system volume.

CurrentDir does not free anything.  NULL is a valid result.
When in doubt, consult the documentation:

dos.library/CurrentDir                                   dos.library/CurrentDir

    NAME
        CurrentDir -- Make a directory associated with a lock the working directory

    SYNOPSIS
        oldLock = CurrentDir( lock )
        D0                    D1

        struct FileLock *oldlock, *lock;

    FUNCTION
        CurrentDir() causes a directory associated with a lock to be made
        the current directory.  The old current directory lock is returned.

        A value of zero is a valid result here, this 0 lock represents the
        root of fiat you booted from (which is, in effect, the
        parent of all other file system roots.)

    INPUTS
        lock - BCPL pointer to a lock

    OUTPUTS
        oldLock - BCPL pointer to a lock

    SEE ALSO
        Lock

-- 
--
Ken Farinsky - CATS - (215) 431-9421 - Commodore Business Machines
uucp: ken@cbmvax.commodore.com   or  ...{uunet,rutgers}!cbmvax!ken
bix:  kfarinsky