Re: Parsing a directory

Bernd Paysan <[email protected]>
Newsgroups gmane.comp.lang.forth.gforth
Message-ID <9867475.HFFik8OCNf@daiyu>
Am Dienstag, 12. Juni 2018, 23:58:49 CEST schrieb Dennis Ruffer:
> I’m working on a project that needs to recursively find every file in a
> directory structure and I found some code to get started with at:
> https://www.rosettacode.org/wiki/Walk_a_directory/Recursively#Forth
> 
> It is not doing what it is supposed to do and I hope to improve on it, but
> I’ve run into a stumbling block.
> 
> I can’t seem to be able to open directories to see if I’ve fallen into a
> symbolic loop.
> 
> For example, a symbolic link pointing to “../” will cause it to overflow
> real quickly.
> 
> The only solution I can think of is to look for duplicate names, but that
> has far too many false positives.
> Here’s my code, and I’m hoping someone can suggest an alternative:
> : $append ( from len to -- )   2DUP >R >R  COUNT + SWAP MOVE  R> R@ C@ + R>
> : C! ;
> : 
> : dots? ( name len -- ? )
> 
> \  2dup s" ." compare 0= if  2drop true exit  then
> \  s" .." compare 0= if  true exit  then
>   drop c@ [char] . = if  true exit  then
>   false ;
> 
> : (ls-r) ( dir len -- )
> 
>   pad c@ >r  dup >r  pad count >r >r  pad $append  s" /" pad $append
>   r> r> 2dup + 1- r> 2 + search nip nip if
>     ."  -> loop detected"  r> pad c!  exit  then  ( duplicate)
>   pad count open-dir if  drop  r> pad c!  exit  then  ( dirid)
>   begin
>     dup pad count + 256 rot read-dir throw
>   while
>     pad count + over dots? 0= if   \ ignore current and parent dirs
>       cr dup pad count rot + type
>       pad count + swap recurse
>     else drop then
>   repeat
>   drop  r> pad c!
>   close-dir throw
> ;
> 
> : ls-r ( dir len -- )  0 pad c!  (ls-r) ;
> 
> s" ." ls-r cr
> 
> DaR

Ah yes, there's a good reason why I added unix/filestat.fs, which has lstat, 
and allows you to analyze a file before you follow it.

Follow S_IFDIR files, don't follow symlinks.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o id: kQusJzA;7*?t=uy@X}1GWr!+0qqp_Cn176t4(dQ*
http://bernd-paysan.de/
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEERJ1NDqPQRwYnwBjr9y2Uk5MtoGcFAlsgVHsACgkQ9y2Uk5Mt
oGf6Cw//UclpuQLKTZWPaCXnA9wkya8CqcXbN9PJJjmbSAbZVDyHCpEdne+/5Gkj
mHF2nyWgPU9cVWyw+YUUlcsXoO0UBb4lLNeuG7qvTAAL9KZgTKCYum58DAp/IokJ
e0c65KmeYf9kTa0lqC6ymFoyx2cNHRKMcXrv8g7dGLtlQ4TqyL1NWqywVYLKkw8r
Jz+SAQqEHQOuTUVRaY+25+69i4EHvSLpsF/F21znKNlTFvEyAzxwNK9TXoKKeA0w
vt3FuAZWB3tyDXxGU0skPBW4ZwrnUQIursPvNui11dFiX0V9e4qrXi89Xv2WSW6o
1WS4zQnds3BNyqi7WsOf52Rf3jwZAImAURw4R/AUBak1w7I5gxMldTYxGxjAvubc
AiaHz7Pat3CcpoQeeaopCrDBReGeAB0gvELdlvf9GqFfv/7ogFo0H2jAjJR26Gag
k0qLVjBXKf4m+ZTdkJy4qUzBGAfdn6xjsRIaQ/3ZYmflsurrQjPuC0GKmkwJUjC5
96iOJGjW3yBL8cGU2O1PG/ni86l7nLRGE6f8fL6r+PzrHcxBKrJ2x5r5lTPWcn5T
LLT7BBS0CiQv+9VK3W+2EesdV7efl9tzhapa96yd/hY96f+yoBhltAWSa6JGEpgA
SMOKGtZe5BdZ4A4+YVgSocF3hKqc1Ps25HIv6WwNEjKJd/dGYEQ=
=7pn5
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.