beshers@tune.cs.columbia.edu (Clifford Beshers) wrote:
> I find no example scripts with the scheme shell distribution. Can
> anyone point me at some sample code?
Here's an example that implements something similar to Unix's `du'.
It uses a `find'-like program that Olin posted earlier. If you don't
have that (those, really -- there were several), let me know.
'shriram
; Shriram Krishnamurthi (shriram@cs.rice.edu)
; Sun Feb 11 02:07:05 CST 1996
; (disk-usage print-files?
; print-dirs?
; print-summary? . dirs) --> sizes procedure
;
; disk-usage mimics the Unix command du(1). The arguments are:
;
; print-files?
; Whether to print names and sizes of individual files visited.
; print-dirs?
; Whether to print for each directory visited.
; print-summary?
; Whether to print a summary for each top-level file/directory. This
; flag is ignored if the top-level item is a file and print-files? is
; requested, or if it is a directory and print-dirs? is on.
; dirs
; Any number of files/directories. If none are provided, this
; argument defaults to the current directory.
; (This rationale is questionable. For interactive use, one rarely
; queries more than a single directory, so it makes no difference.
; For program use, where multiple "dirs" is more likely, an apply
; has to be needlessly used over a list that has already been consed
; up in memory.)
;
; The value returned is the list of sizes, one for each directory in
; dirs (in the order provided).
;
; Shortcomings:
;
; - Doesn't yet do anything intelligent about symbolic links.
; - Could improve the formatting of printed output.
; - Should do better on unreachable/non-existent nodes.
; - Could convert file sizes to blocks, to be more Unix-like.
(define disk-usage
(let ((print-item (lambda (name size)
(display size) (display " ")
(display name) (newline))))
(lambda (print-files? print-dirs? print-summary? . dirs)
(map
(lambda (dir)
(let ((size (car
(reduce-file-tree dir
(lambda (name info accum)
(cons 0 accum))
(lambda (name info accum)
(let ((size (file-info:size info)))
(if print-files? (print-item name size))
(cons (+ (car accum) size) (cdr accum))))
(lambda (name info accum)
(let ((current-dir-size (car accum)))
(if print-dirs?
(print-item name current-dir-size))
(cons (+ current-dir-size (cadr accum))
(cddr accum))))
'(0)))))
(if (and print-summary?
(or (and (file-directory? dir) (not print-dirs?))
(not (or (file-directory? dir) print-files?))))
(print-item dir size))
size))
(if (null? dirs) (cons "." dirs) dirs)))))
|