source: trunk/abcl/src/org/armedbear/lisp/directory.lisp @ 12502

Last change on this file since 12502 was 12502, checked in by Mark Evenson, 13 years ago

ABCL system code should be platform agnostic at runtime.

Use (featurep :windows) rather than #+windows so that the compiled
code will work the same no matter where it is compiled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
1;;; directory.lisp
3;;; Copyright (C) 2004-2007 Peter Graves
4;;; Copyright (C) 2008 Ville Voutilainen
5;;; $Id: directory.lisp 12502 2010-02-22 14:45:59Z mevenson $
7;;; This program is free software; you can redistribute it and/or
8;;; modify it under the terms of the GNU General Public License
9;;; as published by the Free Software Foundation; either version 2
10;;; of the License, or (at your option) any later version.
12;;; This program is distributed in the hope that it will be useful,
13;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; GNU General Public License for more details.
17;;; You should have received a copy of the GNU General Public License
18;;; along with this program; if not, write to the Free Software
19;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21;;; As a special exception, the copyright holders of this library give you
22;;; permission to link this library with independent modules to produce an
23;;; executable, regardless of the license terms of these independent
24;;; modules, and to copy and distribute the resulting executable under
25;;; terms of your choice, provided that you also meet, for each linked
26;;; independent module, the terms and conditions of the license of that
27;;; module.  An independent module is a module which is not derived from
28;;; or based on this library.  If you modify this library, you may extend
29;;; this exception to your version of the library, but you are not
30;;; obligated to do so.  If you do not wish to do so, delete this
31;;; exception statement from your version.
33(in-package "SYSTEM")
35(defun pathname-as-file (pathname)
36  (let ((directory (pathname-directory pathname)))
37    (make-pathname :host nil
38                   :device (pathname-device pathname)
39                   :directory (butlast directory)
40                   :name (car (last directory))
41                   :type nil
42                   :version nil)))
44(defun list-directories-with-wildcards (pathname)
45  (let* ((directory (pathname-directory pathname))
46   (first-wild (position-if #'wild-p directory))
47   (wild (when first-wild (nthcdr first-wild directory)))
48   (non-wild (if first-wild
49           (nbutlast directory
50         (- (length directory) first-wild))
51           directory))
52   (newpath (make-pathname :directory non-wild
53         :name nil :type nil :defaults pathname))
54   (entries (list-directory newpath)))
55    (if (not wild)
56  entries (mapcan (lambda (entry)
57                          (let* ((pathname (pathname entry))
58                                 (directory (pathname-directory pathname))
59                                 (rest-wild (cdr wild)))
60                            (unless (pathname-name pathname)
61            (when (pathname-match-p (first (last directory)) (if (eql (car wild) :wild) "*" (car wild)))
62        (when rest-wild
63          (setf directory (nconc directory rest-wild)))
64          (list-directories-with-wildcards
65         (make-pathname :directory directory
66            :defaults newpath))))))
67                        entries))))
70(defun directory (pathspec &key)
71  (let ((pathname (merge-pathnames pathspec)))
72    (when (logical-pathname-p pathname)
73      (setq pathname (translate-logical-pathname pathname)))
74    (if (or (position #\* (namestring pathname))
75      (wild-pathname-p pathname))
76        (let ((namestring (directory-namestring pathname)))
77          (when (and namestring (> (length namestring) 0))
78            (when (featurep :windows)
79              (let ((device (pathname-device pathname)))
80                (when device
81                  (setq namestring (concatenate 'string device ":" namestring)))))
82            (let ((entries (list-directories-with-wildcards namestring))
83                  (matching-entries ()))
84              (dolist (entry entries)
85                (cond ((file-directory-p entry)
86                       (when (pathname-match-p (file-namestring (pathname-as-file entry)) (file-namestring pathname))
87                         (push entry matching-entries)))
88                      ((pathname-match-p (file-namestring entry) (file-namestring pathname))
89                       (push entry matching-entries))))
90              matching-entries)))
91        ;; Not wild.
92        (let ((truename (probe-file pathname)))
93          (if truename
94              (list (pathname truename))
95              nil)))))
Note: See TracBrowser for help on using the repository browser.