Walk a directory/Recursively: Difference between revisions

Content added Content deleted
(Added Kotlin)
Line 826: Line 826:


=={{header|Forth}}==
=={{header|Forth}}==
{{works with|gforth|0.6.2}}
{{works with|gforth|0.7.9}}
<lang forth>
''Todo: track the full path and print it on matching files.''
require unix/filestat.fs
<lang forth>defer ls-filter
require unix/libc.fs


: $append ( from len to -- ) 2DUP >R >R COUNT + SWAP MOVE R> R@ C@ + R> C! ;
: dots? ( name len -- ? )
dup 1 = if drop c@ [char] . =
else 2 = if dup c@ [char] . = swap 1+ c@ [char] . = and
else drop false then then ;


defer ls-filter
: ls-r ( dir len -- )

open-dir if drop exit then ( dirid)
: dots? ( name len -- ? ) drop c@ [char] . = ;

file-stat buffer: statbuf

: isdir ( addr u -- flag )
statbuf lstat ?ior statbuf st_mode w@ S_IFMT and S_IFDIR = ;

: (ls-r) ( dir len -- )
pad c@ >r pad $append s" /" pad $append
pad count open-dir if drop r> pad c! exit then ( dirid)
begin
begin
dup pad 256 rot read-dir throw
dup pad count + 256 rot read-dir throw
while
while
pad over dots? 0= if \ ignore current and parent dirs
pad count + over dots? 0= if \ ignore all hidden names
pad over recurse
dup pad count rot + 2dup ls-filter if
pad over ls-filter if
cr 2dup type
cr pad swap type
then
isdir if
pad count + swap recurse
else drop then
else drop then
else drop then
else drop then
repeat
repeat
drop close-dir throw ;
drop r> pad c!
close-dir throw
;


: c-file? ( str len -- ? )
: ls-r ( dir len -- ) 0 pad c! (ls-r) ;

: c-files ( str len -- ? )
dup 3 < if 2drop false exit then
dup 3 < if 2drop false exit then
+ 1- dup c@ 32 or
+ 1- dup c@ 32 or
Line 855: Line 869:
1- dup c@ [char] . <> if drop false exit then
1- dup c@ [char] . <> if drop false exit then
drop true ;
drop true ;
' c-file? is ls-filter
' c-files is ls-filter

: all-files ( str len -- ? ) 2drop true ;
' all-files is ls-filter


s" ." ls-r</lang>
s" ." ls-r cr
</lang>


=={{header|Gambas}}==
=={{header|Gambas}}==