1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

ftw: Add `scandir'.

Suggested by Nala Ginrut <nalaginrut@gmail.com>.

* module/ice-9/ftw.scm (scandir): New procedure.
* test-suite/tests/ftw.test ("scandir"): New test prefix.
* doc/ref/misc-modules.texi (File Tree Walk): Document `scandir'.
This commit is contained in:
Ludovic Courtès 2011-12-18 21:14:33 +01:00
parent af98fafabf
commit 1629429d63
3 changed files with 51 additions and 1 deletions

View file

@ -1249,6 +1249,18 @@ to `du --apparent-size' with GNU Coreutils.)"
The alternative C-like functions are described below. The alternative C-like functions are described below.
@defun scandir name [select? [entry<?]]
Return the list of the names of files contained in directory @var{name}
that match predicate @var{select?} (by default, all files). The
returned list of file names is sorted according to @var{entry<?}, which
defaults to @code{string-locale<?} such that file names are sorted in
the locale's alphabetical order (@pxref{Text Collation}).
This procedure is modeled after the C library function of the same name
(@pxref{Scanning Directory Content,,, libc, GNU C Library Reference
Manual}).
@end defun
@defun ftw startname proc ['hash-size n] @defun ftw startname proc ['hash-size n]
Walk the file system tree descending from @var{startname}, calling Walk the file system tree descending from @var{startname}, calling
@var{proc} for each file and directory. @var{proc} for each file and directory.

View file

@ -193,9 +193,11 @@
#:use-module (ice-9 match) #:use-module (ice-9 match)
#:use-module (ice-9 vlist) #:use-module (ice-9 vlist)
#:use-module (srfi srfi-1) #:use-module (srfi srfi-1)
#:autoload (ice-9 i18n) (string-locale<?)
#:export (ftw nftw #:export (ftw nftw
file-system-fold file-system-fold
file-system-tree)) file-system-tree
scandir))
(define (directory-files dir) (define (directory-files dir)
(let ((dir-stream (opendir dir))) (let ((dir-stream (opendir dir)))
@ -505,4 +507,32 @@ children. The optional STAT parameter defaults to `lstat'."
(caar (file-system-fold enter?* leaf down up skip '(()) file-name stat))) (caar (file-system-fold enter?* leaf down up skip '(()) file-name stat)))
(define* (scandir name #:optional (select? (const #t))
(entry<? string-locale<?))
"Return the list of the names of files contained in directory NAME
that match predicate SELECT? (by default, all files.) The returned list
of file names is sorted according to ENTRY<?, which defaults to
`string-locale<?'."
(define (enter? name stat result)
(and stat (string=? name name)))
(define (leaf name stat result)
(if (select? name)
(cons (basename name) result)
result))
(define (down name stat result)
(cons "." result))
(define (up name stat result)
(cons ".." result))
(define (skip name stat result)
;; NAME itself is not readable.
#f)
(and=> (file-system-fold enter? leaf down up skip '() name stat)
(lambda (files)
(sort files entry<?))))
;;; ftw.scm ends here ;;; ftw.scm ends here

View file

@ -166,3 +166,11 @@
expected))) expected)))
(_ #f)) (_ #f))
children))))) children)))))
(with-test-prefix "scandir"
(pass-if "test-suite"
(let ((select? (cut string-suffix? ".test" <>)))
(match (scandir (string-append %test-dir "/tests") select?)
(("." ".." "00-initial-env.test" (? select?) ...)
#t)))))