even less STAT-calls
(removed STAT-calls when called with RECURSIVE)
This commit is contained in:
parent
5973322619
commit
3c200d4db9
59
ls.scm
59
ls.scm
|
@ -61,16 +61,26 @@
|
||||||
(file-directory? path #f))) ;; or not
|
(file-directory? path #f))) ;; or not
|
||||||
(ls-directory path all? recursive? long? directory? flag? columns? port))
|
(ls-directory path all? recursive? long? directory? flag? columns? port))
|
||||||
(else
|
(else
|
||||||
(ls-file path long? flag? port))))
|
(ls-file (cons path (file-info path #f)) long? flag? port))))
|
||||||
|
|
||||||
(define (ls-directory directory all? recursive? long? directory? flag? columns? port)
|
(define (ls-directory directory all? recursive? long? directory? flag? columns? port)
|
||||||
; (display (list "starting ls-directory:" directory)) (newline)
|
; terminology: a FILE-NAME is the name of a file
|
||||||
|
; a FILE is a pair whose car is a file-name and whose cdr is
|
||||||
|
; either its file-info-object or #f (if not needed)
|
||||||
|
; a INFO is a file-info-object
|
||||||
(let* ((directory (file-name-as-directory directory))
|
(let* ((directory (file-name-as-directory directory))
|
||||||
(substantial-directory (string-append directory "."))
|
(substantial-directory (string-append directory "."))
|
||||||
(files (directory-files substantial-directory all?)))
|
(file-names (directory-files substantial-directory all?)))
|
||||||
(with-cwd*
|
(with-cwd*
|
||||||
substantial-directory
|
substantial-directory
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
(let ((files (if (or recursive? long? flag?) ; these are the flags for which we need the file-info
|
||||||
|
(map (lambda (file-name)
|
||||||
|
(cons file-name (file-info file-name #f)))
|
||||||
|
file-names)
|
||||||
|
(map (lambda (file-name) (cons file-name #f))
|
||||||
|
file-names))))
|
||||||
|
|
||||||
(if (and (not long?)
|
(if (and (not long?)
|
||||||
columns?)
|
columns?)
|
||||||
(ls-files-columns files flag? port)
|
(ls-files-columns files flag? port)
|
||||||
|
@ -78,14 +88,16 @@
|
||||||
|
|
||||||
(if recursive?
|
(if recursive?
|
||||||
(let ((directories
|
(let ((directories
|
||||||
(filter (lambda (file) (file-directory? file #f))
|
(map (lambda (file) (car file))
|
||||||
files)))
|
(filter (lambda (file)
|
||||||
|
(eq? (file-info:type (cdr file)) 'directory))
|
||||||
|
files))))
|
||||||
(if (not (null? directories))
|
(if (not (null? directories))
|
||||||
(begin
|
(begin
|
||||||
(newline port)
|
(newline port)
|
||||||
(real-ls directories directory
|
(real-ls directories directory
|
||||||
all? recursive? long? directory? flag? columns?
|
all? recursive? long? directory? flag? columns?
|
||||||
port)))))))))
|
port))))))))))
|
||||||
|
|
||||||
(define *width* 79)
|
(define *width* 79)
|
||||||
|
|
||||||
|
@ -93,7 +105,7 @@
|
||||||
(let* ((max-file-name-width
|
(let* ((max-file-name-width
|
||||||
(if (null? files)
|
(if (null? files)
|
||||||
0
|
0
|
||||||
(apply max (map string-length files))))
|
(apply max (map (lambda (file) (string-length (car file))) files))))
|
||||||
(max-file-name-width
|
(max-file-name-width
|
||||||
(if flag?
|
(if flag?
|
||||||
(+ 1 max-file-name-width)
|
(+ 1 max-file-name-width)
|
||||||
|
@ -125,8 +137,8 @@
|
||||||
((= column columns))
|
((= column columns))
|
||||||
(let ((tail (vector-ref tails column)))
|
(let ((tail (vector-ref tails column)))
|
||||||
(if (not (null? tail))
|
(if (not (null? tail))
|
||||||
(let* ((file-name (car tail))
|
(let* ((file (car tail))
|
||||||
(width (display-file file-name (file-info file-name) flag? port)))
|
(width (display-file file flag? port)))
|
||||||
(display-spaces (- column-width width) port)
|
(display-spaces (- column-width width) port)
|
||||||
(vector-set! tails column (cdr tail))))))
|
(vector-set! tails column (cdr tail))))))
|
||||||
(newline port))))
|
(newline port))))
|
||||||
|
@ -144,17 +156,17 @@
|
||||||
(ls-file file long? flag? port))
|
(ls-file file long? flag? port))
|
||||||
files))
|
files))
|
||||||
|
|
||||||
(define (ls-file file-name long? flag? port)
|
(define (ls-file file long? flag? port)
|
||||||
(let ((info (file-info file-name #f)))
|
|
||||||
(if long?
|
(if long?
|
||||||
(ls-file-long file-name info flag? port)
|
(ls-file-long file flag? port)
|
||||||
(ls-file-short file-name info flag? port))))
|
(ls-file-short file flag? port)))
|
||||||
|
|
||||||
(define (ls-file-short file-name info flag? port)
|
(define (ls-file-short file flag? port)
|
||||||
(display-file file-name info flag? port)
|
(display-file file flag? port)
|
||||||
(newline port))
|
(newline port))
|
||||||
|
|
||||||
(define (ls-file-long file-name info flag? port)
|
(define (ls-file-long file flag? port)
|
||||||
|
(let ((info (cdr file)))
|
||||||
(display-permissions info port)
|
(display-permissions info port)
|
||||||
(display-decimal-justified (file-info:nlinks info) 4 port)
|
(display-decimal-justified (file-info:nlinks info) 4 port)
|
||||||
(write-char #\space port)
|
(write-char #\space port)
|
||||||
|
@ -182,12 +194,12 @@
|
||||||
(write-char #\space port)
|
(write-char #\space port)
|
||||||
(display-time (file-info:mtime info) port)
|
(display-time (file-info:mtime info) port)
|
||||||
(write-char #\space port)
|
(write-char #\space port)
|
||||||
(display-file file-name info flag? port)
|
(display-file file flag? port)
|
||||||
(if (eq? (file-info:type info) 'symlink)
|
(if (eq? (file-info:type info) 'symlink)
|
||||||
(begin
|
(begin
|
||||||
(display " -> " port)
|
(display " -> " port)
|
||||||
(display (read-symlink file-name) port)))
|
(display (read-symlink (car file) port))))
|
||||||
(newline port))
|
(newline port)))
|
||||||
|
|
||||||
(define *year-seconds* (* 365 24 60 60))
|
(define *year-seconds* (* 365 24 60 60))
|
||||||
|
|
||||||
|
@ -198,11 +210,12 @@
|
||||||
(display (format-date "~b ~d ~H:~M" date) port)
|
(display (format-date "~b ~d ~H:~M" date) port)
|
||||||
(display (format-date "~b ~d ~Y " date) port))))
|
(display (format-date "~b ~d ~Y " date) port))))
|
||||||
|
|
||||||
(define (display-file file-name info flag? port)
|
(define (display-file file flag? port)
|
||||||
|
(let ((file-name (car file)))
|
||||||
(display file-name port)
|
(display file-name port)
|
||||||
(if (maybe-display-flag info flag? port)
|
(if (maybe-display-flag (cdr file) flag? port)
|
||||||
(+ 1 (string-length file-name))
|
(+ 1 (string-length file-name))
|
||||||
(string-length file-name)))
|
(string-length file-name))))
|
||||||
|
|
||||||
(define (maybe-display-flag info flag? port)
|
(define (maybe-display-flag info flag? port)
|
||||||
(and flag?
|
(and flag?
|
||||||
|
@ -230,6 +243,8 @@
|
||||||
(write-char #\d port))
|
(write-char #\d port))
|
||||||
((symlink)
|
((symlink)
|
||||||
(write-char #\l port))
|
(write-char #\l port))
|
||||||
|
((fifo)
|
||||||
|
(write-char #\p port))
|
||||||
(else
|
(else
|
||||||
(write-char #\- port)))
|
(write-char #\- port)))
|
||||||
(let ((mode (file-info:mode info))
|
(let ((mode (file-info:mode info))
|
||||||
|
|
Loading…
Reference in New Issue