🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.amiga.tech
Article #8435

Re: Changing Directories

#8435
From: SLMYQ
Date: Sat, 09 Apr 1988 02:36
74 lines
3144 bytes
>>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
>Very true. Here's what I do:
>At the beginning of my program, I do:
>
>curdir = DupLock(FindTask(NULL)->pr_CurrentDir);
>firstdir = CurrentDir(curdir);

Yeah, that's another way to do it, although it seems you could just have:

firstdir = DupLock(FindTask(NULL)->pr_CurrentDir);

Then the new lock would be the one you would save until the end of the
program, and the original one would be the "working" lock.  The only
difference is that it saves the CurrentDir().

>So now I have firstdir available to CurrentDir to at the end of my program.
>Now, since _I_ allocated curdir, I can use:
>
>curdir = newdir;
>UnLock(CurrentDir(curdir));

Or how about just UnLock(CurrentDir(newdir))?
Or better yet, like I did, define a function that only UnLock()s it if
it's non-zero - I called it ChangeDir().

>to move to a new directory. This is, of course, after checking that newdir
>is valid and is a directory. At the end of my program I just:
>
>UnLock(CurrentDir(firstdir));
>
>and I'm back!
>
>One note:
>curdir = Lock("", ACCESS_READ); can be used in place of the above DupLock().

Interesting.  I wonder which has more overhead?

>>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.
>Are you sure about this? When you 'run <programname>', the newly created
>process has whatever current directory the shell it was invoked from has.
>But I do believe that if you never switched directories in your original
>shell, what you said above is true.

Oh, yeah, that's right.  However, if you boot up the Amiga, press CTRL-D
before anything can get going, and run a program, that program's
pr_CurrentDir *will* be zero.  I've had a lot of trouble with this -
my programs aborted with out-of-memory errors because they couldn't
DupLock() a zero lock!

Try this:

#include <exec/types.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

main()
{
        printf("pr_CurrentDir: %ld",((struct Process*)FindTask(NULL))->
                pr_CurrentDir);
}

This is just off the top of my head, so I don't guarantee that it's bug-free.
If it works as expected, when you run it before CDing anywhere, it'll show
0.  After you CD to somewhere, it'll show you some value.

>--
>Aaron Avery (avery@puff.cs.wisc.edu)
>            ({seismo,caip,allegra,harvard,rutgers,ihnp4}!uwvax!puff!avery)

                                Bryan Ford (SLMYQ@USU.BITNET)
Technical Details
Message-ID: <8804090302.AA01309@jade.berkeley.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!ucbvax!USU.BITNET!SLMYQ

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!ucbvax!USU.BITNET!SLMYQ
From: SLMYQ@USU.BITNET
Newsgroups: comp.sys.amiga.tech
Subject: Re: Changing Directories
Message-ID: <8804090302.AA01309@jade.berkeley.edu>
Date: 9 Apr 88 02:36:00 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Lines: 74