From: Peter Seibel
Subject: Portable directory listing function
Date: 
Message-ID: <m3hdxb7ywv.fsf@javamonkey.com>
So I'm trying to write a function that returns a list of pathnames
representing files contained in a given subdirectory including
immediate subdirectories. Further, I want the subdirectories to be
represented by pathnames in "directory form", i.e. with all the name
elements in the directory component of the pathname as opposed to
"file form" where the last name element is split up into the name and
type components. Here's what I've got so far which I've tested on
current versions of SBCL, CMUCL, Lispworks, OpenMCL, Allegro, and
CLISP. If you have easy access to other implementations and care to
test this out, let me know what readtime conditionalization I need to
add.

-Peter

P.S. Edi, this code also includes a portable directory walker that
applies a function to all the pathnames under a given root, something
I think you were asking about a few weeks ago.

  (defpackage :pathnames 
    (:use :cl)
    (:export
     :list-directory
     :walk-directory
     :pathname-as-directory
     :directory-pathname-p))

  (in-package :pathnames)

  (defun list-directory (dirname)
    "Return a list of the contents of the directory named by dirname.
  Names of subdirectories will be returned in `directory normal form'.
  Unlike CL:DIRECTORY, LIST-DIRECTORY does not accept wildcard
  pathnames; `dirname' should simply be a pathname that names a
  directory. It can be in either file or directory form."
    (let ((wildcard (make-pathname :name :wild 
                                   :type :wild
                                   :defaults (pathname-as-directory dirname))))

      #+(or sbcl cmu lispworks)
      ;; SBCL, CMUCL, and Lispworks return subdirectories in directory
      ;; form just the way we want.
      (directory wildcard)

      #+openmcl
      ;; OpenMCl by default doesn't return subdirectories at all. But
      ;; when prodded to do so with the special argument :directories,
      ;; it returns them in directory form.
      (directory wildcard :directories t)

      #+allegro
      ;; Allegro normally return directories in file form but we can
      ;; change that with the :directories-are-files argument.
      (directory wildcard :directories-are-files nil)

      #+clisp
      ;; CLISP has a particularly idiosyncratic (though arguably
      ;; logically consistent) view of things. And (as of 2.32) it has a
      ;; slight bug to work around. But we can bludgeon even it into
      ;; doing what we want.
      (nconc 
       ;; CLISP 2.32 won't list files without an extension when :type is
       ;; wild so we make a special wildcard for it.
       (directory (make-pathname :type nil :defaults wildcard))
       ;; And CLISP doesn't consider subdirectories to match unless
       ;; there is a :wild in the directory component.
       (directory (make-pathname 
                   :directory (append (pathname-directory wildcard) (list :wild))
                   :name nil
                   :type nil
                   :defaults wildcard)))))

  (defun pathname-as-directory (pathname)
    "Return a pathname reperesenting the given pathname in `directory
  form', i.e. with all the name elements in the directory component and
  NIL in the name and type components. Can not be used on wild
  pathnames. Returns its argument if name and type are both nil or
  :unspecific."
    (setf pathname (pathname pathname))
    (when (wild-pathname-p pathname)
      (error "Can't reliably convert wild pathnames to directory names."))
    (cond 
     ((or (component-present-p (pathname-name pathname))
          (component-present-p (pathname-type pathname)))
      (make-pathname 
       :directory (append (pathname-directory pathname) (list (file-namestring pathname)))
       :name      nil
       :type      nil
       :defaults pathname))
      (t pathname)))

  (defun walk-directory (dirname fn &key (depth-first t) (directories t))
    "Walk a directory invoking `fn' on each pathname found. If
  `depth-first' is t (the default) walks depth-first. If `directories'
  is t (the default) invokes `fn' on directory pathnames as well."
    (do ((files (list (pathname-as-directory dirname))))
        ((null files))
      (let ((f (pop files)))
        (cond
         ((directory-pathname-p f)
          (when directories (funcall fn f))
          (let ((children (list-directory f)))
            (setf files 
                  (if depth-first
                    (nconc children files)
                    (nconc files children)))))
         (t (funcall fn f))))))

  (defun directory-pathname-p  (p)
    "Is the given pathname the name of a directory? This function can
  usefully be used to test whether a name returned by LIST-DIRECTORIES
  or passed to the function in WALK-DIRECTORY is the name of a directory
  in the file system since they always return names in `directory normal
  form'."
    (and 
     (not (component-present-p (pathname-name p)))
     (not (component-present-p (pathname-type p)))))

  (defun component-present-p (value)
    (and value (not (eql value :unspecific))))

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp

From: Avi Blackmore
Subject: Re: Portable directory listing function
Date: 
Message-ID: <7c1401ca.0402291946.1350bc8b@posting.google.com>
Peter Seibel <·····@javamonkey.com> wrote in message news:<··············@javamonkey.com>...
> So I'm trying to write a function that returns a list of pathnames
> representing files contained in a given subdirectory including
> immediate subdirectories. Further, I want the subdirectories to be
> represented by pathnames in "directory form", i.e. with all the name
> elements in the directory component of the pathname as opposed to
> "file form" where the last name element is split up into the name and
> type components. Here's what I've got so far which I've tested on
> current versions of SBCL, CMUCL, Lispworks, OpenMCL, Allegro, and
> CLISP. If you have easy access to other implementations and care to
> test this out, let me know what readtime conditionalization I need to
> add.

[code snipped]

I like this!  I've wanted this sort of functionality for a project of
mine.  What
license is it under, if I may ask?  I would want your permission
before redistributing
it, of course.

Avi Blackmore
From: Peter Seibel
Subject: Re: Portable directory listing function
Date: 
Message-ID: <m3hdx9ji4g.fsf@javamonkey.com>
········@cableone.net (Avi Blackmore) writes:

> Peter Seibel <·····@javamonkey.com> wrote in message news:<··············@javamonkey.com>...
> > So I'm trying to write a function that returns a list of pathnames
> > representing files contained in a given subdirectory including
> > immediate subdirectories. Further, I want the subdirectories to be
> > represented by pathnames in "directory form", i.e. with all the name
> > elements in the directory component of the pathname as opposed to
> > "file form" where the last name element is split up into the name and
> > type components. Here's what I've got so far which I've tested on
> > current versions of SBCL, CMUCL, Lispworks, OpenMCL, Allegro, and
> > CLISP. If you have easy access to other implementations and care to
> > test this out, let me know what readtime conditionalization I need to
> > add.
> 
> [code snipped]
> 
> I like this! I've wanted this sort of functionality for a project of
> mine. What license is it under, if I may ask? I would want your
> permission before redistributing it, of course.

I hereby place it in the public domain. Go nuts. Better yet, convince
your favorite vendor (assuming it's not SBCL, CMUCL, or Lispworks who
already do) to make their native CL:DIRECTORY function to return
subdirectories when given a wild pathname like:

  (make-pathname :name :wild :type :wild :directory whatever)

and to always return the names of directories in "directory form",
thus the LIST-DIRECTORIES function that much less necessary. (If
anyone cares, I have a theory about how such an implementation of
DIRECTORY can also be made compatible with PATHNAME-MATCH-P.)

Soon I should have a FILE-EXISTS-P function that papers over the wide
range of behaviors exhibited by different implementations when given
the name of a directory and which can also be used to tell whether a
file is a directory or a file.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp