#!/usr/bin/gosh (use file.util) (define (rounded-number->string x . digits-of-precision) (if (null? digits-of-precision) (number->string (inexact->exact (round x))) (let* ((digits (car digits-of-precision)) (factor (expt 10.0 digits)) (n (abs (inexact->exact (round (* x factor))))) (s (number->string n)) (l (string-length s)) (rs (if (< n factor) (string-append "0." (make-string (- digits l) #\0) s) (string-append (substring s 0 (- l digits)) "." (substring s (- l digits) l))))) (if (< x 0) (string-append "-" rs) rs)))) (define (format-size size) ;; TODO print decimals in k & M notations ;; - with format: does ~d support this? doesn't look like it ;; - with quotient&remainder: repeated simplification ;; (q&r 1234567 (expt 2 20)) => (1 185991) ;; (q&r 185991 (expt 2 10)) => (181 647) ;; so: 1M, 181k and 647 bytes ;; or determine single decimal after first quotient: ;; (q&r 1234567 (expt 2 20)) => (1 185991) ;; and round off (/ 185991 (expt 2 20)) using integer arithmetic ;; so: 1.2M (cond ;;((< size (expt 2 10)) (format "~4d " size)) ((< size (expt 2 20)) ;;(format "~4dk" (round (/ size (expt 2 10))))) (format "~6@ak" (rounded-number->string (/ size (expt 2 10)) 1))) ((< size (expt 2 30)) (format "~6@aM" (rounded-number->string (/ size (expt 2 20)) 1))) (else "moby"))) ;; TODO list the following fields: ;; mode size mtime name ;; -rw-r--r-- 1.4k Nov 21 15:34 snarf.scm (define (print-entry/1 name) (let ((st (sys-stat name))) (format #t "~o ~s ~a\n" (slot-ref st 'perm) (slot-ref st 'mtime) name))) (define (print-entry name) (let1 st (sys-stat name) (format #t "~o ~6@a ~a ~a\n" (slot-ref st 'perm) (format-size (slot-ref st 'size)) (sys-strftime "%Y%m%d %H:%M" (sys-localtime (slot-ref st 'mtime))) (if (file-is-directory? name) (string-append name "/") name)))) (define (main args) (let ((args (cdr args))) (cond ((null? args) (map print-entry (directory-list "."))) ;; XXX invoke directory-list when an arg is-a directory. ((file-is-directory? (car args)) (map print-entry (directory-list (car args)))) (else (map print-entry args))))) ;; Local Variables: ;; mode: gauche ;; end: