spinix-hive/pfth/cd.fth

62 lines
939 B
Forth
Raw Permalink Normal View History

: slash-count ( addr count -- addr num )
over swap ( addr0 addr count )
begin
over c@ [char] / <>
over 0 > and
while
1 /string
repeat
drop over -
;
\ Change directory
: cd-path
\ Check if count <= 0
dup 0 <=
if
spi_rootdir @
spi_currdir !
2drop exit
then
\ Save current directory on return stack
spi_currdir @ >r
\ Check if absolute path or relative path
over c@ [char] / =
if
spi_rootdir @
spi_currdir !
1 /string
then
\ Loop on directory list
begin
dup 0 >
while
2dup slash-count
dup 0=
if
." Unexpected /" cr
r> spi_currdir !
2drop 2drop exit
then
dup 1+ >r
0 open-file
if
drop
." Could not open file" cr
r> drop
r> spi_currdir !
2drop exit
then
file_sector0 @
spi_currdir !
close-file drop
r> /string
repeat
r> drop 2drop
;
: cd bl word count cd-path ;