gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gforth] Parsing a directory


From: Dennis Ruffer
Subject: Re: [gforth] Parsing a directory
Date: Tue, 12 Jun 2018 21:32:05 -0700

I probably still have some issues, but I think this is close:

 

require unix/filestat.fs

require unix/libc.fs

 

: $append ( from len to -- )   2DUP >R >R  COUNT + SWAP MOVE  R> R@ C@ + R> C! ;

 

: 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

    dup pad count + 256 rot read-dir throw

  while

    pad count + over dots? 0= if   \ ignore all hidden names

      cr dup pad count rot + type

      dup pad count rot + isdir if

        pad count + swap recurse

      else drop then

    else drop then

  repeat

  drop  r> pad c!

  close-dir throw

;

 

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

s" ." ls-r cr

 

Thanks

 

DaR

 

From: Dennis Ruffer
Sent: Tuesday, June 12, 2018 7:07 PM
To: Bernd Paysan; address@hidden
Subject: RE: [gforth] Parsing a directory

 

I’ll check it out shortly Bernd.

 

Thanks

 

DaR

 

From: Bernd Paysan
Sent: Tuesday, June 12, 2018 4:17 PM
To: address@hidden
Cc: Dennis Ruffer
Subject: Re: [gforth] Parsing a directory

 

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;address@hidden(dQ*

http://bernd-paysan.de/

 

 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]