changes made to

* avoid too many STAT-calls
* show executable-flag (*) for regular files
This commit is contained in:
interp 2001-09-07 15:08:37 +00:00
parent 669e5ab4a8
commit a9a269043f
1 changed files with 58 additions and 55 deletions

51
ls.scm
View File

@ -55,15 +55,16 @@
(define (ls-path path all? recursive? long? directory? flag? columns? port)
(cond
((and (not directory?)
(or (and (file-name-directory? path)
(file-directory? path #t))
(file-directory? path #f)))
((and (not directory?) ;; go into directories
(or (and (file-name-directory? path) ;; path specifies directory
(file-directory? path #t)) ;; either as a symlink (if the names end with a slash)
(file-directory? path #f))) ;; or not
(ls-directory path all? recursive? long? directory? flag? columns? port))
(else
(ls-file path long? flag? port))))
(define (ls-directory directory all? recursive? long? directory? flag? columns? port)
; (display (list "starting ls-directory:" directory)) (newline)
(let* ((directory (file-name-as-directory directory))
(substantial-directory (string-append directory "."))
(files (directory-files substantial-directory all?)))
@ -146,16 +147,16 @@
files))
(define (ls-file file-name long? flag? port)
(let ((info (file-info file-name #f)))
(if long?
(ls-file-long file-name flag? port)
(ls-file-short file-name flag? port)))
(ls-file-long file-name info flag? port)
(ls-file-short file-name info flag? port))))
(define (ls-file-short file-name flag? port)
(display-file file-name flag? port)
(define (ls-file-short file-name info flag? port)
(display-file file-name info flag? port)
(newline port))
(define (ls-file-long file-name flag? port)
(let ((info (file-info file-name #f)))
(define (ls-file-long file-name info flag? port)
(display-permissions info port)
(display-decimal-justified (file-info:nlinks info) 4 port)
(write-char #\space port)
@ -183,12 +184,12 @@
(write-char #\space port)
(display-time (file-info:mtime info) port)
(write-char #\space port)
(display-file file-name flag? port)
(if (file-symlink? file-name)
(display-file file-name info flag? port)
(if (eq? (file-info:type info) 'symlink)
(begin
(display " -> " port)
(display (read-symlink file-name) port)))
(newline port)))
(newline port))
(define *year-seconds* (* 365 24 60 60))
@ -199,27 +200,29 @@
(display (format-date "~b ~d ~H:~M" date) port)
(display (format-date "~b ~d ~Y " date) port))))
(define (display-file file-name flag? port)
(define (display-file file-name info flag? port)
(display file-name port)
(if (maybe-display-flag file-name flag? port)
(if (maybe-display-flag info flag? port)
(+ 1 (string-length file-name))
(string-length file-name)))
(define (maybe-display-flag file-name flag? port)
(if (not (and flag?
(not (file-regular? file-name))))
#f
(define (maybe-display-flag info flag? port)
(and flag?
(begin
(cond
((file-directory? file-name)
((eq? (file-info:type info) 'directory)
(write-char #\/ port))
((file-symlink? file-name)
((eq? (file-info:type info) 'symlink)
(write-char #\@ port))
((file-executable? file-name)
; 'executable: bits 0, 3 or 6 are set:
; that means, 'AND' with 1+8+64=73 results in a nonzero-value
; note: there is no distinction between user's, group's and other's permissions
; (as the real GNU-ls does not)
((not (zero? (bitwise-and (file-info:mode info) 73)))
(write-char #\* port))
((file-socket? file-name)
((eq? (file-info:type info) 'socket)
(write-char #\= port))
((file-fifo? file-name)
((eq? (file-info:type info) 'fifo)
(write-char #\| port)))
#t)))