/[cmucl]/src/code/filesys.lisp
ViewVC logotype

Contents of /src/code/filesys.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Tue Sep 3 20:44:02 1991 UTC (22 years, 7 months ago) by ram
Branch: MAIN
Changes since 1.11: +4 -15 lines
Make user-homedir-pathname just return home:.
1 ram 1.1 ;;; -*- Log: code.log; Package: Lisp -*-
2     ;;; ### Some day fix to accept :wild in any pathname component.
3     ;;; **********************************************************************
4 ram 1.8 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;; If you want to use this code or any part of CMU Common Lisp, please contact
7     ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
8     ;;;
9     (ext:file-comment
10 ram 1.12 "$Header: /tiger/var/lib/cvsroots/cmucl/src/code/filesys.lisp,v 1.12 1991/09/03 20:44:02 ram Exp $")
11 ram 1.8 ;;;
12 ram 1.1 ;;; **********************************************************************
13     ;;;
14     ;;; Ugly pathname functions for Spice Lisp.
15     ;;; these functions are part of the standard Spice Lisp environment.
16     ;;;
17     ;;; Written by Jim Large and Rob MacLachlan
18     ;;;
19     ;;; **********************************************************************
20 ram 1.2
21     (in-package "LISP")
22    
23 ram 1.1 (export '(pathname pathnamep *default-pathname-defaults* truename
24     parse-namestring merge-pathnames make-pathname
25     pathname-host pathname-device pathname-directory
26     pathname-name pathname-type pathname-version
27     namestring file-namestring directory-namestring
28     host-namestring enough-namestring user-homedir-pathname
29     probe-file rename-file delete-file file-write-date
30     file-author directory))
31    
32     (use-package "EXTENSIONS")
33    
34     (in-package "EXTENSIONS")
35     (export '(print-directory complete-file ambiguous-files default-directory
36 wlott 1.5 file-writable unix-namestring))
37 ram 1.2 (in-package "LISP")
38 ram 1.1
39    
40 wlott 1.5
41     ;;; Pathname structure
42 ram 1.1
43    
44     ;;; *Default-Pathname-defaults* has all values unspecified except for the
45     ;;; host. All pathnames must have a host.
46     (defvar *default-pathname-defaults* ()
47     "Set to the default pathname-defaults pathname (Got that?)")
48    
49     (defun filesys-init ()
50 wlott 1.3 (setq *default-pathname-defaults*
51     (%make-pathname "Mach" nil nil nil nil nil))
52     (multiple-value-bind (won dir)
53     (mach:unix-current-directory)
54     (when won
55     (setf (search-list "default:") (list dir)))))
56 ram 1.1
57    
58     ;;; The pathname type is defined with a defstruct.
59     ;;; This declaration implicitly defines the common lisp function Pathnamep
60     (defstruct (pathname
61     (:conc-name %pathname-)
62     (:print-function %print-pathname)
63     (:constructor
64     %make-pathname (host device directory name type version))
65     (:predicate pathnamep))
66     "Pathname is the structure of the file pathname. It consists of a
67     host, a device, a directory, a name, and a type."
68     (host nil :type (or simple-string null))
69     (device nil :type (or simple-string (member nil :absolute)))
70     (directory nil :type (or simple-vector null))
71     (name nil :type (or simple-string null))
72     (type nil :type (or simple-string null))
73     (version nil :type (or integer (member nil :newest))))
74    
75     (defun %print-pathname (s stream d)
76     (declare (ignore d))
77 ram 1.11 (format stream "#p~S" (namestring s)))
78 ram 1.1
79     (defun make-pathname (&key defaults (host nil hostp) (device nil devicep)
80     (directory nil directoryp) (name nil namep)
81     (type nil typep) (version nil versionp))
82     "Create a pathname from :host, :device, :directory, :name, :type and :version.
83     If any field is ommitted, it is obtained from :defaults as though by
84     merge-pathnames."
85 ram 1.10 (flet ((make-it (host device directory name type version)
86     (%make-pathname
87     (if host
88     (if (stringp host) (coerce host 'simple-string) host)
89     (%pathname-host *default-pathname-defaults*))
90     (if (stringp device) (coerce device 'simple-string) device)
91     (if (stringp directory)
92     (%pathname-directory (parse-namestring directory))
93     directory)
94     (if (stringp name) (coerce name 'simple-string) name)
95     (if (stringp type) (coerce type 'simple-string) type)
96     version)))
97 ram 1.1 (if defaults
98     (let ((defaults (pathname defaults)))
99 ram 1.10 (make-it (if hostp host (%pathname-host defaults))
100     (if devicep device (%pathname-device defaults))
101     (if directoryp directory (%pathname-directory defaults))
102     (if namep name (%pathname-name defaults))
103     (if typep type (%pathname-type defaults))
104     (if versionp version (%pathname-version defaults))))
105     (make-it host device directory name type version))))
106 ram 1.1
107    
108     ;;; These can not be done by the accessors because the pathname arg may be
109     ;;; a string or a symbol or etc.
110    
111     (defun pathname-host (pathname)
112     "Returns the host slot of pathname. Pathname may be a string, symbol,
113     or stream."
114     (%pathname-host (if (pathnamep pathname) pathname (pathname pathname))))
115    
116     (defun pathname-device (pathname)
117     "Returns the device slot of pathname. Pathname may be a string, symbol,
118     or stream."
119     (%pathname-device (if (pathnamep pathname) pathname (pathname pathname))))
120    
121     (defun pathname-directory (pathname)
122     "Returns the directory slot of pathname. Pathname may be a string,
123     symbol, or stream."
124     (%pathname-directory (if (pathnamep pathname) pathname (pathname pathname))))
125    
126     (defun pathname-name (pathname)
127     "Returns the name slot of pathname. Pathname may be a string,
128     symbol, or stream."
129     (%pathname-name (if (pathnamep pathname) pathname (pathname pathname))))
130    
131     (defun pathname-type (pathname)
132     "Returns the type slot of pathname. Pathname may be a string,
133     symbol, or stream."
134     (%pathname-type (if (pathnamep pathname) pathname (pathname pathname))))
135    
136     (defun pathname-version (pathname)
137     "Returns the version slot of pathname. Pathname may be a string,
138     symbol, or stream."
139     (%pathname-version (if (pathnamep pathname) pathname (pathname pathname))))
140    
141    
142    
143 ram 1.2 ;;;; PARSE-NAMESTRING and PATHNAME.
144 ram 1.1
145 ram 1.2 ;;; SPLIT-FILENAME -- internal
146     ;;;
147     ;;; Splits the filename into the name and type. If someone wants to change
148     ;;; this yet again, just change this.
149     ;;;
150     (defun split-filename (filename)
151     (declare (simple-string filename))
152     (let ((posn (position #\. filename :from-end t)))
153     (cond ((null posn)
154     (values filename nil))
155     ((or (zerop posn) (= posn (1- (length filename))))
156     (values filename ""))
157     (t
158     (values (subseq filename 0 posn)
159     (subseq filename (1+ posn)))))))
160 ram 1.1
161 ram 1.2 ;;; DO-FILENAME-PARSE -- internal
162 ram 1.1 ;;;
163 ram 1.2 ;;; Split string into a logical name, a vector of directories, a file name and
164     ;;; a file type.
165 ram 1.1 ;;;
166 ram 1.2 (defun do-filename-parse (string &optional (start 0) end)
167     (declare (simple-string string))
168     (let ((end (or end (length string))))
169     (let* ((directories nil)
170     (filename nil)
171     (absolutep (and (> end start) (eql (schar string start) #\/)))
172     (logical-name
173     (cond (absolutep
174     (setf start (position #\/ string :start start :end end
175     :test-not #'char=))
176     :absolute)
177     ((find #\: string
178     :start start
179     :end (or (position #\/ string :start start :end end)
180     end))
181     (let ((posn (position #\: string :start start)))
182     (prog1
183     (subseq string start posn)
184     (setf start (1+ posn))))))))
185     (loop
186     (unless (and start (> end start))
187     (return))
188     (let ((next-slash (position #\/ string :start start :end end)))
189     (cond (next-slash
190     (push (subseq string start next-slash) directories)
191     (setf start
192     (position #\/ string :start next-slash :end end
193     :test-not #'char=)))
194     (t
195     (setf filename (subseq string start end))
196     (return)))))
197     (multiple-value-bind (name type)
198     (if filename (split-filename filename))
199     (values (cond (logical-name logical-name)
200     (directories "Default"))
201     (if (or logical-name directories)
202     (coerce (nreverse directories) 'vector))
203     name
204     type)))))
205 ram 1.1
206 ram 1.2 (defun parse-namestring (thing &optional host
207     (defaults *default-pathname-defaults*)
208     &key (start 0) end junk-allowed)
209     "Convert THING (string, symbol, pathname, or stream) into a pathname."
210     (declare (ignore junk-allowed))
211     (let* ((host (or host (pathname-host defaults)))
212     (pathname
213     (etypecase thing
214     ((or string symbol)
215 ram 1.9 (with-array-data ((string (string thing))
216     (start start)
217     (end end))
218 ram 1.2 (multiple-value-bind (device directories name type)
219     (do-filename-parse string start end)
220     (make-pathname :host host
221     :device device
222     :directory directories
223     :name name
224     :type type))))
225     (pathname
226     thing)
227     (stream
228     (pathname (file-name thing))))))
229     (unless (or (null host)
230     (null (pathname-host pathname))
231     (string-equal host (pathname-host pathname)))
232     (cerror "Ignore it."
233     "Host mismatch in ~S: ~S isn't ~S"
234     'parse-namestring
235     (pathname-host pathname)
236     host))
237 ram 1.10 ;;
238     ;; ### ??? what should the second value be???
239     (values pathname (or end start))))
240 ram 1.1
241    
242     (defun pathname (thing)
243     "Turns thing into a pathname. Thing may be a string, symbol, stream, or
244     pathname."
245     (values (parse-namestring thing)))
246    
247    
248    
249     ;;; Merge-Pathnames -- Public
250     ;;;
251     ;;; Returns a new pathname whose fields are the same as the fields in PATHNAME
252     ;;; except that () fields are filled in from defaults. Type and Version field
253     ;;; are only done if name field has to be done (see manual for explanation).
254     ;;;
255     (defun merge-pathnames (pathname &optional
256     (defaults *default-pathname-defaults*)
257     default-version)
258     "Fills in unspecified slots of Pathname from Defaults (defaults to
259     *default-pathname-defaults*). If the version remains unspecified,
260     gets it from Default-Version."
261     ;;
262     ;; finish hairy argument defaulting
263 ram 1.10 (let ((pathname (pathname pathname))
264     (defaults (pathname defaults)))
265     ;;
266     ;; make a new pathname
267     (let ((name (%pathname-name pathname))
268     (device (%pathname-device pathname)))
269     (%make-pathname
270     (or (%pathname-host pathname) (%pathname-host defaults))
271     (or device (%pathname-device defaults))
272     (or (%pathname-directory pathname) (%pathname-directory defaults))
273     (or name (%pathname-name defaults))
274     (or (%pathname-type pathname) (%pathname-type defaults))
275     (or (%pathname-version pathname)
276     (if name
277     default-version
278     (or (%pathname-version defaults) default-version)))))))
279 ram 1.1
280    
281     ;;;; NAMESTRING and other stringification stuff.
282    
283     ;;; %Dirstring -- Internal
284     ;;;
285     ;;; %Dirstring converts a vector of the form #("foo" "bar" ... "baz") into a
286     ;;; string of the form "foo/bar/ ... /baz/"
287    
288     (defun %dirstring (dirlist)
289     (declare (simple-vector dirlist))
290     (let* ((numdirs (length dirlist))
291     (length numdirs))
292     (declare (fixnum numdirs length))
293     (dotimes (i numdirs)
294     (incf length (the fixnum (length (svref dirlist i)))))
295     (do ((result (make-string length))
296     (index 0 (1+ index))
297     (position 0))
298     ((= index numdirs) result)
299     (declare (simple-string result))
300     (let* ((string (svref dirlist index))
301     (len (length string))
302     (end (+ position len)))
303     (declare (simple-string string)
304     (fixnum len end))
305     (replace result string :start1 position :end1 end :end2 len)
306     (setf (schar result end) #\/)
307     (setq position (+ end 1))))))
308    
309     (defun quick-integer-to-string (n)
310     (cond ((zerop n) "0")
311     ((eql n 1) "1")
312     ((minusp n)
313     (concatenate 'simple-string "-"
314     (the simple-string (quick-integer-to-string (- n)))))
315     (t
316     (do* ((len (1+ (truncate (integer-length n) 3)))
317     (res (make-string len))
318     (i (1- len) (1- i))
319     (q n)
320     (r 0))
321     ((zerop q)
322     (incf i)
323     (replace res res :start2 i :end2 len)
324 wlott 1.3 (shrink-vector res (- len i)))
325 ram 1.1 (declare (simple-string res)
326     (fixnum len i r))
327     (multiple-value-setq (q r) (truncate q 10))
328     (setf (schar res i) (schar "0123456789" r))))))
329    
330     (defun %device-string (device)
331     (cond ((eq device :absolute) "/")
332     (device
333     (if (string-equal device "Default")
334     ""
335     (concatenate 'simple-string (the simple-string device) ":")))
336     (T "")))
337    
338     (defun namestring (pathname)
339     "Returns the full form of PATHNAME as a string."
340 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
341     (directory (%pathname-directory pathname))
342 ram 1.1 (name (%pathname-name pathname))
343     (type (%pathname-type pathname))
344     (result (%device-string (%pathname-device pathname))))
345     (declare (simple-string result))
346     (when directory
347     (setq result (concatenate 'simple-string result
348     (the simple-string (%dirstring directory)))))
349     (when name
350     (setq result (concatenate 'simple-string result
351     (the simple-string name))))
352 ram 1.2 (when (and type (not (zerop (length type))))
353 ram 1.1 (setq result (concatenate 'simple-string result "."
354     (the simple-string type))))
355     result))
356    
357 wlott 1.4 (defun namestring-without-device (pathname)
358 ram 1.1 "NAMESTRING of pathname ignoring the device slot."
359 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
360     (directory (%pathname-directory pathname))
361 ram 1.1 (name (%pathname-name pathname))
362     (type (%pathname-type pathname))
363     (result ""))
364     (declare (simple-string result))
365     (when directory
366     (setq result (concatenate 'simple-string result
367     (the simple-string (%dirstring directory)))))
368     (when name
369     (setq result (concatenate 'simple-string result
370     (the simple-string name))))
371 ram 1.2 (when (and type (not (zerop (length type))))
372 ram 1.1 (setq result (concatenate 'simple-string result "."
373     (the simple-string type))))
374     result))
375    
376     ;;; This function is somewhat bummed to make the Hemlock directory command
377     ;;; is fast.
378     ;;;
379     (defun file-namestring (pathname)
380     "Returns the name, type, and version of PATHNAME as a string."
381 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
382     (name (%pathname-name pathname))
383 ram 1.1 (type (%pathname-type pathname))
384     (result (or name "")))
385     (declare (simple-string result))
386 ram 1.2 (if (and type (not (zerop (length type))))
387 ram 1.1 (concatenate 'simple-string result "." type)
388     result)))
389    
390     (defun directory-namestring (pathname)
391     "Returns the device & directory parts of PATHNAME as a string."
392 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
393     (directory (%pathname-directory pathname))
394 ram 1.1 (result (%device-string (%pathname-device pathname))))
395     (declare (simple-string result))
396     (when directory
397     (setq result (concatenate 'simple-string result
398     (the simple-string (%dirstring directory)))))
399     result))
400    
401     (defun host-namestring (pathname)
402     "Returns the host part of PATHNAME as a string."
403 wlott 1.4 (%pathname-host (if (pathnamep pathname) pathname (pathname pathname))))
404 ram 1.1
405    
406 wlott 1.4 ;;; Do-Search-List -- Internal
407     ;;;
408     ;;; Bind var in turn to each element of search list with the specifed
409     ;;; name.
410     ;;;
411     (defmacro do-search-list ((var name &optional exit-form) . body)
412     "Do-Search-List (Var Name [Exit-Form]) {Form}*"
413     `(dolist (,var (resolve-search-list ,name nil) ,exit-form)
414     (declare (simple-string ,var))
415     ,@body))
416    
417 wlott 1.5 ;;; UNIX-NAMESTRING -- public
418     ;;;
419     (defun unix-namestring (pathname &optional (for-input t))
420 wlott 1.4 "Convert PATHNAME into a string that can be used with UNIX system calls."
421     (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
422     (device (%pathname-device pathname)))
423     (cond ((or (eq device :absolute)
424     (null device)
425     (string= device "Default"))
426     (namestring pathname))
427     (for-input
428     (let ((remainder (namestring-without-device pathname))
429     (first nil))
430     (do-search-list (entry device first)
431     (let ((name (concatenate 'simple-string entry remainder)))
432     (unless first
433     (setf first name))
434     (when (mach:unix-file-kind name)
435     (return name))))))
436     (t
437     (concatenate 'simple-string
438     (car (resolve-search-list device t))
439     (namestring-without-device pathname))))))
440    
441    
442 ram 1.1
443     ;;;; ENOUGH-NAMESTRING
444    
445     (defun enough-namestring (pathname &optional
446     (defaults *default-pathname-defaults*))
447     "Returns a string which uniquely identifies PATHNAME w.r.t. DEFAULTS."
448 ram 1.10 (let* ((pathname (pathname pathname))
449     (defaults (pathname defaults))
450     (device (%pathname-device pathname))
451 ram 1.1 (directory (%pathname-directory pathname))
452     (name (%pathname-name pathname))
453     (type (%pathname-type pathname))
454     (result "")
455     (need-name nil))
456     (declare (simple-string result))
457     (when (and device (string-not-equal device (%pathname-device defaults)))
458     (setq result (%device-string device)))
459     (when (and directory
460     (not (equalp directory (%pathname-directory defaults))))
461     (setq result (concatenate 'simple-string result
462     (the simple-string (%dirstring directory)))))
463     (when (and name (string-not-equal name (%pathname-name defaults)))
464     (setq result (concatenate 'simple-string result
465     (the simple-string name))
466     need-name t))
467     (when (and type (or need-name
468     (string-not-equal type (%pathname-type defaults))))
469     (setq result (concatenate 'simple-string result "."
470     (the simple-string type))))
471     result))
472    
473    
474    
475     ;;;; TRUENAME and other stuff probing stuff.
476    
477     ;;; Truename -- Public
478     ;;;
479 wlott 1.4 ;;; Another silly file function trivially different from another function.
480 ram 1.1 ;;;
481     (defun truename (pathname)
482     "Return the pathname for the actual file described by the pathname
483     An error is signalled if no such file exists."
484     (let ((result (probe-file pathname)))
485     (unless result
486     (error "The file ~S does not exist." (namestring pathname)))
487     result))
488    
489     ;;; Probe-File -- Public
490     ;;;
491 wlott 1.4 ;;; If PATHNAME exists, return it's truename, otherwise NIL.
492 ram 1.1 ;;;
493     (defun probe-file (pathname)
494     "Return a pathname which is the truename of the file if it exists, NIL
495     otherwise. Returns NIL for directories and other non-file entries."
496 wlott 1.4 (let ((namestring (unix-namestring pathname t)))
497     (when (mach:unix-file-kind namestring)
498     (let ((truename (mach:unix-resolve-links
499     (mach:unix-maybe-prepend-current-directory
500     namestring))))
501     (when truename
502     (pathname (mach:unix-simplify-pathname truename)))))))
503 ram 1.1
504    
505 wlott 1.4 ;;;; Other random operations.
506 ram 1.1
507     ;;; Rename-File -- Public
508     ;;;
509     ;;; If File is a File-Stream, then rename the associated file if it exists,
510     ;;; otherwise just change the name in the stream. If not a file stream, then
511     ;;; just rename the file.
512     ;;;
513     (defun rename-file (file new-name)
514     "Rename File to have the specified New-Name. If file is a stream open to a
515     file, then the associated file is renamed. If the file does not yet exist
516     then the file is created with the New-Name when the stream is closed."
517 wlott 1.4 (let* ((original (truename file))
518     (original-namestring (namestring original))
519     (new-name (merge-pathnames new-name original))
520     (new-namestring (unix-namestring new-name nil)))
521     (multiple-value-bind (res error)
522     (mach:unix-rename original-namestring
523     new-namestring)
524     (unless res
525     (error "Failed to rename ~A to ~A: ~A"
526     original new-name (mach:get-unix-error-msg error)))
527     (when (streamp file)
528     (file-name file new-namestring))
529     (values new-name original (truename new-namestring)))))
530 ram 1.1
531     ;;; Delete-File -- Public
532     ;;;
533     ;;; Delete the file, Man.
534     ;;;
535     (defun delete-file (file)
536     "Delete the specified file."
537 wlott 1.4 (let ((namestring (unix-namestring file t)))
538 ram 1.1 (when (streamp file)
539     (close file :abort t))
540 wlott 1.4 (when namestring
541     (multiple-value-bind (res err) (mach:unix-unlink namestring)
542     (unless res
543     (error "Could not delete ~A: ~A."
544     namestring
545     (mach:get-unix-error-msg err))))))
546 ram 1.1 t)
547 wlott 1.4
548 ram 1.1
549     ;;; User-Homedir-Pathname -- Public
550     ;;;
551 ram 1.12 ;;; Return Home:, which is set up for us at initialization time.
552 ram 1.1 ;;;
553     (defun user-homedir-pathname (&optional host)
554     "Returns the home directory of the logged in user as a pathname.
555 ram 1.12 This is obtained from the logical name \"home:\"."
556 ram 1.1 (declare (ignore host))
557 ram 1.12 #p"home:")
558 ram 1.1
559     ;;; File-Write-Date -- Public
560     ;;;
561     (defun file-write-date (file)
562     "Return file's creation date, or NIL if it doesn't exist."
563 wlott 1.4 (multiple-value-bind (res dev ino mode nlink uid gid
564     rdev size atime mtime)
565     (mach:unix-stat (unix-namestring file t))
566     (declare (ignore dev ino mode nlink uid gid rdev size atime))
567     (when res
568     (+ unix-to-universal-time mtime))))
569 ram 1.1
570     ;;; File-Author -- Public
571     ;;;
572     (defun file-author (file)
573     "Returns the file author as a string, or nil if the author cannot be
574     determined. Signals an error if file doesn't exist."
575 wlott 1.4 (multiple-value-bind (winp dev ino mode nlink uid)
576     (mach:unix-stat (unix-namestring (pathname file) t))
577     (declare (ignore dev ino mode nlink))
578     (if winp (lookup-login-name uid))))
579 ram 1.1
580    
581    
582     ;;;; DIRECTORY.
583    
584 wlott 1.4 ;;; PARSE-PATTERN -- internal.
585 ram 1.1 ;;;
586 wlott 1.4 ;;; Parse-pattern extracts the name portion of pathname and converts it into
587     ;;; a pattern usable in match-pattern-p.
588     ;;;
589     (defun parse-pattern (pathname)
590     (let ((string (file-namestring pathname))
591     (pattern nil)
592     (last-regular-char nil)
593     (index 0))
594     (flet ((flush-pending-regulars ()
595     (when last-regular-char
596     (push (subseq string last-regular-char index) pattern))
597     (setf last-regular-char nil)))
598     (loop
599     (when (>= index (length string))
600     (return))
601     (let ((char (schar string index)))
602     (cond ((char= char #\?)
603     (flush-pending-regulars)
604     (push :single-char-wild pattern)
605     (incf index))
606     ((char= char #\*)
607     (flush-pending-regulars)
608     (push :multi-char-wild pattern)
609     (incf index))
610     ((char= char #\[)
611     (flush-pending-regulars)
612     (let ((close-bracket (position #\] string :start index)))
613     (unless close-bracket
614     (error "``['' with no corresponding ``]'': ~S" string))
615     (push :character-set pattern)
616     (push (subseq string (1+ index) close-bracket)
617     pattern)
618     (setf index (1+ close-bracket))))
619     (t
620     (unless last-regular-char
621     (setf last-regular-char index))
622     (incf index)))))
623     (flush-pending-regulars))
624     (nreverse pattern)))
625 ram 1.1
626 wlott 1.4 ;;; MATCH-PATTERN-P -- internal.
627     ;;;
628     ;;; Determine if string (starting at start) matches pattern.
629     ;;;
630     (defun match-pattern-p (string pattern &optional (start 0))
631     (cond ((null pattern)
632     (= start (length string)))
633     ((eq (car pattern) :single-char-wild)
634     (and (> (length string) start)
635     (match-pattern-p string (cdr pattern) (1+ start))))
636     ((eq (car pattern) :character-set)
637     (and (> (length string) start)
638     (find (schar string start) (cadr pattern))
639     (match-pattern-p string (cddr pattern) (1+ start))))
640     ((eq (car pattern) :multi-char-wild)
641     (do ((new-start (length string) (1- new-start)))
642     ((< new-start start) nil)
643     (when (match-pattern-p string (cdr pattern) new-start)
644     (return t))))
645     ((stringp (car pattern))
646     (let* ((expected (car pattern))
647     (len (length expected))
648     (new-start (+ start len)))
649     (and (>= (length string) new-start)
650     (string= string expected :start1 start :end1 new-start)
651     (match-pattern-p string (cdr pattern) new-start))))
652     (t
653     (error "Bogus thing in pattern: ~S" (car pattern)))))
654    
655     ;;; MATCHING-FILES-IN-DIR -- internal
656     ;;;
657     ;;; Return a list of all the files in the directory dirname that match
658     ;;; pattern. If all is nil, ignore files starting with a ``.''.
659     ;;;
660     (defun matching-files-in-dir (dirname pattern all)
661     (let ((dir (mach:open-dir dirname)))
662     (if dir
663     (unwind-protect
664     (let ((results nil))
665     (loop
666     (let ((name (mach:read-dir dir)))
667     (cond ((null name)
668     (return))
669     ((or (string= name ".")
670     (string= name "..")
671     (and (not all)
672     (char= (schar name 0) #\.))))
673     ((or (null pattern)
674     (match-pattern-p name pattern))
675     (if (zerop (length dirname))
676     (push name results)
677     (push (concatenate 'string dirname name)
678     results))))))
679     (values results t))
680     (mach:close-dir dir))
681     (values nil nil))))
682    
683     ;;; DIRECTORY -- public.
684     ;;;
685     (defun directory (pathname &key (all t) (check-for-subdirs t))
686 ram 1.1 "Returns a list of pathnames, one for each file that matches the given
687     pathname. Supplying :all as nil causes this to ignore Unix dot files. This
688     never includes Unix dot and dot-dot in the result."
689 wlott 1.4 (let* ((pathname (if (pathnamep pathname) pathname (pathname pathname)))
690     (device (%pathname-device pathname))
691     (pattern (parse-pattern pathname))
692     (results nil)
693     (really-won nil))
694     (if (or (eq device :absolute)
695     (null device)
696     (string= device "Default"))
697     (multiple-value-setq (results really-won)
698     (matching-files-in-dir (directory-namestring pathname) pattern all))
699     (let ((remainder (namestring-without-device
700     (directory-namestring pathname))))
701     (do-search-list (dir device)
702     (multiple-value-bind
703     (files won)
704     (matching-files-in-dir (concatenate 'simple-string
705     dir
706     remainder)
707     pattern
708     all)
709     (when won
710     (setf really-won t))
711     (setf results (append results files))))))
712     (unless really-won
713     (error "Could not find ~S." pathname))
714     (setf results
715     (sort (delete-duplicates results :test #'string=)
716     #'string<))
717     (mapcar #'(lambda (name)
718     (if (and check-for-subdirs
719     (eq (mach:unix-file-kind name) :directory))
720     (pathname (concatenate 'string name "/"))
721     (pathname name)))
722     results)))
723 ram 1.1
724 wlott 1.4
725     ;;;; Printing directories.
726 ram 1.1
727     ;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
728     ;;;
729     (defun print-directory (pathname &optional stream &key all verbose return-list)
730     "Like Directory, but prints a terse, multi-coloumn directory listing
731     instead of returning a list of pathnames. When :all is supplied and
732     non-nil, then Unix dot files are included too (as ls -a). When :vervose
733     is supplied and non-nil, then a long listing of miscellaneous
734     information is output one file per line."
735     (setf pathname (pathname pathname))
736     (let ((*standard-output* (out-synonym-of stream)))
737     (if verbose
738     (print-directory-verbose pathname all return-list)
739     (print-directory-formatted pathname all return-list))))
740    
741     (defun print-directory-verbose (pathname all return-list)
742 wlott 1.7 (let ((contents (directory pathname :all all :check-for-subdirs nil))
743     (result nil))
744     (format t "Directory of ~A :~%" (namestring pathname))
745     (dolist (file contents)
746     (let* ((namestring (unix-namestring file))
747     (tail (subseq namestring
748     (1+ (or (position #\/ namestring
749     :from-end t
750     :test #'char=)
751     -1)))))
752     (multiple-value-bind
753     (reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
754     (mach:unix-stat namestring)
755     (declare (ignore ino gid rdev atime)
756     (fixnum uid mode))
757     (cond (reslt
758     ;;
759     ;; Print characters for file modes.
760     (macrolet ((frob (bit name &optional sbit sname negate)
761     `(if ,(if negate
762     `(not (logbitp ,bit mode))
763     `(logbitp ,bit mode))
764     ,(if sbit
765     `(if (logbitp ,sbit mode)
766     (write-char ,sname)
767     (write-char ,name))
768     `(write-char ,name))
769     (write-char #\-))))
770     (frob 15 #\d nil nil t)
771     (frob 8 #\r)
772     (frob 7 #\w)
773     (frob 6 #\x 11 #\s)
774     (frob 5 #\r)
775     (frob 4 #\w)
776     (frob 3 #\x 10 #\s)
777     (frob 2 #\r)
778     (frob 1 #\w)
779     (frob 0 #\x))
780     ;;
781     ;; Print the rest.
782     (multiple-value-bind (sec min hour date month year)
783     (get-decoded-time)
784     (declare (ignore sec min hour date month))
785     (format t "~2D ~8A ~8D ~12A ~A~@[/~]~%"
786     nlink
787     (or (lookup-login-name uid) uid)
788     size
789     (decode-universal-time-for-files mtime year)
790     tail
791     (= (logand mode mach::s_ifmt) mach::s_ifdir))))
792     (t (format t "Couldn't stat ~A -- ~A.~%"
793     tail
794     (mach:get-unix-error-msg dev-or-err))))
795 ram 1.1 (when return-list
796 wlott 1.7 (push (if (= (logand mode mach::s_ifmt) mach::s_ifdir)
797     (pathname (concatenate 'string namestring "/"))
798     file)
799     result)))))
800     (nreverse result)))
801 ram 1.1
802     (defun decode-universal-time-for-files (time current-year)
803     (multiple-value-bind (sec min hour day month year)
804     (decode-universal-time (+ time unix-to-universal-time))
805     (declare (ignore sec))
806     (format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
807     (svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
808     "Sep" "Oct" "Nov" "Dec")
809     (1- month))
810     day (= current-year year) year hour min)))
811    
812     (defun print-directory-formatted (pathname all return-list)
813     (let ((width (or (line-length *standard-output*) 80))
814     (names ())
815     (cnt 0)
816     (max-len 0)
817 wlott 1.7 (result (directory pathname :all all)))
818 ram 1.1 (declare (list names) (fixnum max-len cnt))
819     ;;
820     ;; Get the data.
821 wlott 1.7 (dolist (file result)
822     (let* ((name (unix-namestring file))
823     (length (length name))
824     (end (if (and (plusp length)
825     (char= (schar name (1- length)) #\/))
826     (1- length)
827     length))
828     (slash-name (subseq name
829     (1+ (or (position #\/ name
830     :from-end t
831     :end end
832     :test #'char=)
833     -1))))
834     (len (length slash-name)))
835     (declare (simple-string slash-name)
836     (fixnum len))
837     (if (> len max-len) (setq max-len len))
838     (incf cnt)
839     (push slash-name names)))
840     (setq names (nreverse names))
841     ;;
842     ;; Do the output.
843     (let* ((col-width (1+ max-len))
844     (cols (max (truncate width col-width) 1))
845     (lines (ceiling cnt cols)))
846     (declare (fixnum cols lines))
847     (format t "Directory of ~A :~%" (namestring pathname))
848     (dotimes (i lines)
849     (declare (fixnum i))
850     (dotimes (j cols)
851     (declare (fixnum j))
852     (let ((name (nth (+ i (the fixnum (* j lines))) names)))
853     (when name
854     (write-string name)
855     (unless (eql j (1- cols))
856     (tab-over
857     (- col-width (length (the simple-string name))))))))
858     (terpri)))
859     (when return-list
860     result)))
861 ram 1.1
862    
863    
864     ;;;; Translating uid's and gid's.
865    
866     (defvar *uid-hash-table* (make-hash-table)
867     "Hash table for keeping track of uid's and login names.")
868    
869     ;;; LOOKUP-LOGIN-NAME translates a user id into a login name. Previous
870     ;;; lookups are cached in a hash table since groveling the passwd(s) files
871     ;;; is somewhat expensive. The table may hold nil for id's that cannot
872     ;;; be looked up since this means the files are searched in their entirety
873     ;;; each time this id is translated.
874     ;;;
875     (defun lookup-login-name (uid)
876     (multiple-value-bind (login-name foundp) (gethash uid *uid-hash-table*)
877     (if foundp
878     login-name
879     (setf (gethash uid *uid-hash-table*)
880     (get-group-or-user-name :user uid)))))
881    
882     (defvar *gid-hash-table* (make-hash-table)
883     "Hash table for keeping track of gid's and group names.")
884    
885     ;;; LOOKUP-GROUP-NAME translates a group id into a group name. Previous
886     ;;; lookups are cached in a hash table since groveling the group(s) files
887     ;;; is somewhat expensive. The table may hold nil for id's that cannot
888     ;;; be looked up since this means the files are searched in their entirety
889     ;;; each time this id is translated.
890     ;;;
891     (defun lookup-group-name (gid)
892     (multiple-value-bind (group-name foundp) (gethash gid *gid-hash-table*)
893     (if foundp
894     group-name
895     (setf (gethash gid *gid-hash-table*)
896     (get-group-or-user-name :group gid)))))
897    
898    
899     ;;; GET-GROUP-OR-USER-NAME first tries "/etc/passwd" ("/etc/group") since it is
900     ;;; a much smaller file, contains all the local id's, and most uses probably
901     ;;; involve id's on machines one would login into. Then if necessary, we look
902     ;;; in "/etc/passwds" ("/etc/groups") which is really long and has to be
903     ;;; fetched over the net.
904     ;;;
905     (defun get-group-or-user-name (group-or-user id)
906     "Returns the simple-string user or group name of the user whose uid or gid
907     is id, or NIL if no such user or group exists. Group-or-user is either
908     :group or :user."
909     (let ((id-string (let ((*print-base* 10)) (prin1-to-string id))))
910     (declare (simple-string id-string))
911     (multiple-value-bind (file1 file2)
912     (ecase group-or-user
913     (:group (values "/etc/group" "/etc/groups"))
914     (:user (values "/etc/passwd" "/etc/passwd")))
915     (or (get-group-or-user-name-aux id-string file1)
916     (get-group-or-user-name-aux id-string file2)))))
917    
918     (defun get-group-or-user-name-aux (id-string passwd-file)
919     (with-open-file (stream passwd-file)
920     (loop
921     (let ((entry (read-line stream nil)))
922     (unless entry (return nil))
923     (let ((name-end (position #\: (the simple-string entry)
924     :test #'char=)))
925     (when name-end
926     (let ((id-start (position #\: (the simple-string entry)
927     :start (1+ name-end) :test #'char=)))
928     (when id-start
929     (incf id-start)
930     (let ((id-end (position #\: (the simple-string entry)
931     :start id-start :test #'char=)))
932     (when (and id-end
933     (string= id-string entry
934     :start2 id-start :end2 id-end))
935     (return (subseq entry 0 name-end))))))))))))
936    
937    
938 wlott 1.4 ;;;; File completion.
939 ram 1.1
940 wlott 1.4 (defun complete-file (pathname &key (defaults *default-pathname-defaults*)
941     ignore-types)
942     ;; Find all possible pathnames.
943     (let ((files
944     (directory (concatenate 'string
945     (namestring (merge-pathnames pathname
946     defaults))
947     "*")
948     :check-for-subdirs nil)))
949     (cond ((null files)
950     (values nil nil))
951     ((null (cdr files))
952     (values (merge-pathnames (file-namestring (car files))
953     pathname)
954     t))
955     (t
956     (let ((good-files
957     (delete-if #'(lambda (pathname)
958     (and (pathname-type pathname)
959     (member (pathname-type pathname)
960     ignore-types
961     :test #'string=)))
962     files)))
963     (cond ((null good-files))
964     ((null (cdr good-files))
965     (return-from complete-file
966     (values (merge-pathnames (file-namestring
967     (car good-files))
968     pathname)
969     t)))
970     (t
971     (setf files good-files)))
972     (let ((common (file-namestring (car files))))
973     (dolist (file (cdr files))
974     (let ((name (file-namestring file)))
975     (dotimes (i (min (length common) (length name))
976     (when (< (length name) (length common))
977     (setf common name)))
978     (unless (char= (schar common i) (schar name i))
979     (setf common (subseq common 0 i))
980     (return)))))
981     (values (merge-pathnames common pathname)
982     nil)))))))
983 ram 1.1
984 wlott 1.4 ;;; Ambiguous-Files -- Public
985 ram 1.1 ;;;
986 wlott 1.4 (defun ambiguous-files (pathname &optional defaults)
987     "Return a list of all files which are possible completions of Pathname.
988     We look in the directory specified by Defaults as well as looking down
989     the search list."
990     (directory (concatenate 'string
991 wlott 1.6 (namestring
992     (merge-pathnames pathname
993     (make-pathname :defaults defaults
994     :name nil
995     :type nil)))
996     "*")))
997 wlott 1.4
998 ram 1.1
999     ;;; File-writable -- exported from extensions.
1000     ;;;
1001     ;;; Determines whether the single argument (which should be a pathname)
1002     ;;; can be written by the the current task.
1003    
1004     (defun file-writable (name)
1005     "File-writable accepts a pathname and returns T if the current
1006     process can write it, and NIL otherwise."
1007 wlott 1.4 (let ((truename (probe-file name)))
1008     (values
1009     (mach:unix-access
1010     (unix-namestring (or truename (directory-namestring name)) t)
1011     (if truename mach:w_ok (logior mach:w_ok mach:x_ok))))))
1012 ram 1.1
1013    
1014     ;;; Pathname-Order -- Internal
1015     ;;;
1016     ;;; Predicate to order pathnames by. Goes by name.
1017     ;;;
1018     (defun pathname-order (x y)
1019     (let ((xn (%pathname-name x))
1020     (yn (%pathname-name y)))
1021     (if (and xn yn)
1022     (let ((res (string-lessp xn yn)))
1023     (cond ((not res) nil)
1024     ((= res (length (the simple-string xn))) t)
1025     ((= res (length (the simple-string yn))) nil)
1026     (t t)))
1027     xn)))
1028    
1029    
1030     ;;; Default-Directory -- Public
1031     ;;;
1032     ;;; This fills in a hole in Common Lisp. We return the first thing we
1033     ;;; find by doing a ResolveSearchList on Default.
1034     ;;;
1035     (defun default-directory ()
1036     "Returns the pathname for the default directory. This is the place where
1037     a file will be written if no directory is specified. This may be changed
1038     with setf."
1039     (multiple-value-bind (gr dir-or-error)
1040     (mach:unix-current-directory)
1041     (if gr
1042 wlott 1.3 (pathname (concatenate 'simple-string dir-or-error "/"))
1043     (error dir-or-error))))
1044 ram 1.1
1045     ;;;
1046     ;;; Maybe this shouldn't go here...
1047     (defsetf default-directory %set-default-directory)
1048    
1049     ;;; %Set-Default-Directory -- Internal
1050     ;;;
1051     ;;; The setf method for Default-Directory. We actually set the environment
1052     ;;; variable Current which is by convention the head of the search list.
1053     ;;;
1054     (defun %set-default-directory (new-val)
1055     (multiple-value-bind (gr error)
1056 wlott 1.4 (mach:unix-chdir (unix-namestring new-val t))
1057 ram 1.1 (if gr
1058     (car (setf (search-list "default:")
1059 wlott 1.3 (list (default-directory))))
1060 ram 1.1 (error (mach:get-unix-error-msg error)))))

  ViewVC Help
Powered by ViewVC 1.1.5