OneSpace Modeling: A platform-independent ls -l replacement

Oftentimes, users overlook the wealth of file handling functionality which is built-in into Common Lisp and also into OneSpace Modeling's dialect of the language. Way too often, it is assumed that any kind of OS-level work should best be done by calling external components, for example by running DOS or UNIX utilities via a command shell. If you need access to external utilities, oli:sd-sys-exec is your friend, but as a rule of thumb, if you're using oli:sd-sys-exec, chances are that there are much better and less platform-dependent ways to achieve the same.

As an example, here is some code which does the rough equivalent of

  ls -l /some/directory/*somepattern* >somelogfile

Sure enough, the command-line above looks simple and takes only a few seconds to type, but it simply doesn't port to non-UNIX systems (except if you install a UNIX-like shell environment on them). Here's a platform-independent approximation:

;; -*-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Description:  Platform-independent replacement for ls -l
;; Author:       Claus Brod  
;; Language:     Lisp
;;
;; (C) Copyright 2005 Claus Brod, all rights reserved
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
(in-package :clausbrod.de)
(export 'ls-l)

(defun file-mod-time(f)
  (multiple-value-bind (sec minutes hour date month year)
      (decode-universal-time (file-write-date f))
    (format nil "~D/~2,'0D/~2,'0D ~2,'0D:~2,'0D:~2,'0D" year month date hour minutes sec)))

(defun print-file-info(s f)
  (format s "~A ~A ~A~%" (file-namestring f) (file-mod-time f) 
     (oli:sd-inq-file-size (file-namestring f))))

(defun ls-l(dir pattern logfile)
  (oli:sd-with-current-working-directory dir
   (with-open-file (s logfile :direction :output)
     (dolist (f (directory pattern))
       (print-file-info s f)))))

Usage example:

  (clausbrod.de:ls-l "c:/temp" "*.pkg" "pkg.log")

It's too bad that Common Lisp doesn't have a direct equivalent of strftime - this would have saved me from format's funky parameter syntax. But still, now we've got code which...

  • ... is platform-independent,
  • ... does not need to fork costly external processes to do its job,
  • ... is adaptable to particular formatting requirements, unlike external utilities such as dir or ls, where you have to live with the way they format their output and somehow cope with it.

-- ClausBrod - 11 Oct 2005


When asked for a TWiki account, use your own or the default TWikiGuest account.

to top

You are here: CoCreateModeling > OsdmMacros > MacroListDirectory

r1.8 - 01 May 2007 - 15:22 - ClausBrod to top

CoCreateModeling
CoCreate ModelingRSS
FAQ
  Introduction
  Hardware
  Operating system
  Memory management
  File handling
  Installation
  Licensing
  Graphics
  App. knowhow
  Lisp
    Learning
    Programming
    Debugging
    DDE
    Compiler
    Customization
  Troubleshooting
  Links
Code examples
Viewbench
News
Changes
Index
Search
Impressum
Home

  • My links
  • Show me topics of interest

TWiki

Welcome


TWiki Web TWiki Web Home Changes Topics Index Search


TWiki Webs Atari Blog Main OneSpaceModeling? Sandbox TWiki TWiki Webs Atari Blog Main OneSpaceModeling? Sandbox TWiki

Jump:

Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback