Newer
Older
(defun complete-file (pathname &key (defaults *default-pathname-defaults*)
ignore-types)
(let ((files (directory (complete-file-directory-arg pathname defaults)
:check-for-subdirs nil
:follow-links nil)))
(cond ((null files)
(values nil nil))
((null (cdr files))
(values (merge-pathnames (file-namestring (car files))
pathname)
t))
(t
(let ((good-files
(delete-if #'(lambda (pathname)
(and (simple-string-p
(pathname-type pathname))
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
(member (pathname-type pathname)
ignore-types
:test #'string=)))
files)))
(cond ((null good-files))
((null (cdr good-files))
(return-from complete-file
(values (merge-pathnames (file-namestring
(car good-files))
pathname)
t)))
(t
(setf files good-files)))
(let ((common (file-namestring (car files))))
(dolist (file (cdr files))
(let ((name (file-namestring file)))
(dotimes (i (min (length common) (length name))
(when (< (length name) (length common))
(setf common name)))
(unless (char= (schar common i) (schar name i))
(setf common (subseq common 0 i))
(return)))))
(values (merge-pathnames common pathname)
nil)))))))
;;; COMPLETE-FILE-DIRECTORY-ARG -- Internal.
;;;
(defun complete-file-directory-arg (pathname defaults)
(let* ((pathname (merge-pathnames pathname (directory-namestring defaults)))
(type (pathname-type pathname)))
(flet ((append-multi-char-wild (thing)
(etypecase thing
(null :wild)
(pattern
(make-pattern (append (pattern-pieces thing)
(list :multi-char-wild))))
(simple-string
(make-pattern (list thing :multi-char-wild))))))
(if (or (null type) (eq type :unspecific))
;; There is no type.
(make-pathname :defaults pathname
:name (append-multi-char-wild (pathname-name pathname))
:type :wild)
;; There already is a type, so just extend it.
(make-pathname :defaults pathname
:name (pathname-name pathname)
:type (append-multi-char-wild (pathname-type pathname)))))))
;;; Ambiguous-Files -- Public
(defun ambiguous-files (pathname
&optional (defaults *default-pathname-defaults*))
"Return a list of all files which are possible completions of Pathname.
We look in the directory specified by Defaults as well as looking down
the search list."
(directory (complete-file-directory-arg pathname defaults)
:follow-links nil
:check-for-subdirs nil))
;;; File-writable -- exported from extensions.
;;;
;;; Determines whether the single argument (which should be a pathname)
;;; can be written by the the current task.
(defun file-writable (name)
"File-writable accepts a pathname and returns T if the current
process can write it, and NIL otherwise."
(let ((name (unix-namestring name nil)))
(cond ((null name)
nil)
((unix:unix-file-kind name)
(values (unix:unix-access name unix:w_ok)))
(t
(values
0
(or (position #\/ name :from-end t)
0))
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
;;; Pathname-Order -- Internal
;;;
;;; Predicate to order pathnames by. Goes by name.
;;;
(defun pathname-order (x y)
(let ((xn (%pathname-name x))
(yn (%pathname-name y)))
(if (and xn yn)
(let ((res (string-lessp xn yn)))
(cond ((not res) nil)
((= res (length (the simple-string xn))) t)
((= res (length (the simple-string yn))) nil)
(t t)))
xn)))
;;; Default-Directory -- Public
;;;
(defun default-directory ()
"Returns the pathname for the default directory. This is the place where
a file will be written if no directory is specified. This may be changed
with setf."
(multiple-value-bind (gr dir-or-error)
(let ((*ignore-wildcards* t))
(pathname (concatenate 'simple-string dir-or-error "/")))
;;; %Set-Default-Directory -- Internal
;;;
(defun %set-default-directory (new-val)
(let ((namestring (unix-namestring new-val t)))
(unless namestring
(error "~S doesn't exist." new-val))
(multiple-value-bind (gr error)
(if gr
(setf (search-list "default:") (default-directory))
;;;
(defsetf default-directory %set-default-directory)
(defun filesys-init ()
(setf *default-pathname-defaults*
(%make-pathname *unix-host* nil nil nil nil :newest))
(setf (search-list "default:") (default-directory))
nil)