- "Returns a list of pathnames, one for each file that matches the given
- pathname. Supplying :ALL as NIL causes this to ignore Unix dot files. This
- never includes Unix dot and dot-dot in the result. If :FOLLOW-LINKS is NIL,
- then symbolic links in the result are not expanded. This is not the
- default because TRUENAME does follow links, and the result pathnames are
- defined to be the TRUENAME of the pathname (the truename of a link may well
- be in another directory.)"
- (let ((results nil))
- (enumerate-search-list
- (pathname (merge-pathnames pathname
- (make-pathname :name :wild
- :type :wild
- :version :wild)))
- (enumerate-matches (name pathname)
- (when (or all
- (let ((slash (position #\/ name :from-end t)))
- (or (null slash)
- (= (1+ slash) (length name))
- (char/= (schar name (1+ slash)) #\.))))
- (push name results))))
- (let ((*ignore-wildcards* t))
- (mapcar (lambda (name)
- (let ((name (if (and check-for-subdirs
- (eq (sb!unix:unix-file-kind name)
- :directory))
- (concatenate 'string name "/")
- name)))
- (if follow-links (truename name) (pathname name))))
- (sort (delete-duplicates results :test #'string=) #'string<)))))
+ "Return a list of PATHNAMEs, each the TRUENAME of a file that matched the
+ given pathname. Note that the interaction between this ANSI-specified
+ TRUENAMEing and the semantics of the Unix filesystem (symbolic links..)
+ means this function can sometimes return files which don't have the same
+ directory as PATHNAME."
+ (let (;; We create one entry in this hash table for each truename,
+ ;; as an asymptotically fast way of removing duplicates (which
+ ;; can arise when e.g. multiple symlinks map to the same
+ ;; truename).
+ (truenames (make-hash-table :test #'equal))
+ (merged-pathname (merge-pathnames pathname
+ (make-pathname :name :wild
+ :type :wild
+ :version :wild))))
+ (!enumerate-matches (match merged-pathname)
+ (let ((*ignore-wildcards* t)
+ (truename (truename (if (eq (sb!unix:unix-file-kind match)
+ :directory)
+ (concatenate 'string match "/")
+ match))))
+ (setf (gethash (namestring truename) truenames)
+ truename)))
+ (mapcar #'cdr
+ ;; Sorting isn't required by the ANSI spec, but sorting
+ ;; into some canonical order seems good just on the
+ ;; grounds that the implementation should have repeatable
+ ;; behavior when possible.
+ (sort (loop for name being each hash-key in truenames
+ using (hash-value truename)
+ collect (cons name truename))
+ #'string<
+ :key #'car))))