Procedure GetCurrentDir()

AmigaOS specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Roxxler.

Hi,

here a procedure which returns the acutal current path. This is useful
after a WBStartup() followed by a InitTooltype(). The function InitToolType()
then sets the program dir as current directory. To retrieve the complete
path of the current dir you can use this procedure. You can also use this
procedure in a CLI program, then it returns also the actual given path of
the current dir, but this returned path must NOT be the program dir path (if you
set the dir then yes). Normally it is the path that was set in the CLI (with
'CD').

The procedure uses some dos functions (Examine() and ParentDir()), so the
dos.library must be open. Also we need at program start a InitMemoryBank(0),
because the procedure uses Bank(0) for some mem.


Procedure.s GetCurrentPath() ; no parameter, return is a string
infoblock.l=AllocateMemoryBank(0,260,#MEMF_FAST|#MEMF_CLEAR) ; some mem for the info structure for Examine()
filelock.l=PeekL(PeekL(ExecBase()+276)+152) ; at offset 276 of Exec we found a pointer to the actual task, which
; means our task. We could do that also with exec/FindTask(0), but this
; is a little bit faster. At offset 152 of the process structure we found
; a pointer to the lock of the actual set current dir.
Examine_(filelock,infoblock) ; we 'Examine' the lock
path$=PeekS(infoblock+8) ; at offset 8 we found the name of the cuurent dir
Repeat ; here we start to create the complete path of the current dir.
filelock=ParentDir_(filelock) ; it looks strange, but i think you can read it :)
If filelock>0 ; Maybe you know a easier,better and faster way for creating
flag=-1 ; the path, let me know.
nextlock.l=ParentDir_(filelock)
If nextlock=0
path$=":"+path$
Else
path$="/"+path$
EndIf
Examine_(filelock,infoblock)
path$=PeekS(infoblock+8)+path$
Else
If flag=0
path$+":"
Else
path$+"/"
EndIf
EndIf
Until filelock=0
FreeMemoryBank(0)
ProcedureReturn path$
EndProcedure

DosBase.l=OpenDosLibrary_(36)
InitMemoryBank(0)
currentdir$=GetCurrentPath()
PrintN(currentdir$)
End


Greetings ..
Roxxler