Walk a directory/Recursively: Difference between revisions

Content deleted Content added
Kazinator (talk | contribs)
Kazinator (talk | contribs)
→‎{{header|TXR}}: Fix typo with better wording; move side note to end.
Line 2,061: Line 2,061:
TXR wraps and exposes the POSIX <code>nftw</code> function, which is demonstrated here. This function encapsulates a tree walk, and uses callbacks to inform the program of visited filesystem tree nodes, and of error situations. We can use a <code>lambda</code> for the code walk, or wrap the invocation of <code>ftw</code> with a macro which hides the <code>lambda</code> syntax.
TXR wraps and exposes the POSIX <code>nftw</code> function, which is demonstrated here. This function encapsulates a tree walk, and uses callbacks to inform the program of visited filesystem tree nodes, and of error situations. We can use a <code>lambda</code> for the code walk, or wrap the invocation of <code>ftw</code> with a macro which hides the <code>lambda</code> syntax.


A nice approach would be to capture a continuation in the callback, and then obtain the walk elements lazily; alas, capturing a continuation from a C library function's callback is not permitted, because the capture would envelop foreign stack frames.


Here we use the <code>build</code> macro for procedural list building to gather all of the finding sinto a list, which is implicitly returned. The callback is an explicit <code>lambda</code>:
Here we use the <code>build</code> macro for procedural list building to gather all of the found paths into a list, which is implicitly returned. The callback is an explicit <code>lambda</code>:


<lang txrlisp>(build (ftw "." (lambda (path type stat level base)
<lang txrlisp>(build (ftw "." (lambda (path type stat level base)
Line 2,100: Line 2,099:


The <code>type</code>, <code>stat</code>, <code>level</code> and <code>base</code> callback arguments we are ignoring closely follow those of the POSIX C <code>nftw</code> function. <code>type</code> is a type code which indicates the kind of item visited: file, directory; <code>stat</code> is a Lisp version of <code>struct stat</code>, providing various information about the filesystem object: permissions, timestamps, inode number, etc.
The <code>type</code>, <code>stat</code>, <code>level</code> and <code>base</code> callback arguments we are ignoring closely follow those of the POSIX C <code>nftw</code> function. <code>type</code> is a type code which indicates the kind of item visited: file, directory; <code>stat</code> is a Lisp version of <code>struct stat</code>, providing various information about the filesystem object: permissions, timestamps, inode number, etc.

A nice approach would be to capture a continuation in the callback, and then obtain the walk elements lazily; alas, capturing a continuation from a C library function's callback is not permitted, because the capture would span foreign stack frames.


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==