- (declare (simple-string pathname))
- (let ((len (length pathname))
- (pending pathname))
- (declare (fixnum len) (simple-string pending))
- (if (zerop len)
- pathname
- (let ((result (make-string 1024 :initial-element (code-char 0)))
- (fill-ptr 0)
- (name-start 0))
- (loop
- (let* ((name-end (or (position #\/ pending :start name-start) len))
- (new-fill-ptr (+ fill-ptr (- name-end name-start))))
- (replace result pending
- :start1 fill-ptr
- :end1 new-fill-ptr
- :start2 name-start
- :end2 name-end)
- (let ((kind (unix-file-kind (if (zerop name-end) "/" result) t)))
- (unless kind (return nil))
- (cond ((eq kind :link)
- (multiple-value-bind (link err) (unix-readlink result)
- (unless link
- (error 'simple-file-error
- :pathname pathname
- :format-control
- "~@<error reading link ~S: ~2I~_~A~:>"
- :format-arguments (list (subseq
- result 0 fill-ptr)
- (strerror err))))
- (cond ((or (zerop (length link))
- (char/= (schar link 0) #\/))
- ;; It's a relative link.
- (fill result (code-char 0)
- :start fill-ptr
- :end new-fill-ptr))
- ((string= result "/../" :end1 4)
- ;; It's across the super-root.
- (let ((slash (or (position #\/ result :start 4)
- 0)))
- (fill result (code-char 0)
- :start slash
- :end new-fill-ptr)
- (setf fill-ptr slash)))
- (t
- ;; It's absolute.
- (and (> (length link) 0)
- (char= (schar link 0) #\/))
- (fill result (code-char 0) :end new-fill-ptr)
- (setf fill-ptr 0)))
- (setf pending
- (if (= name-end len)
- link
- (concatenate 'simple-string
- link
- (subseq pending name-end))))
- (setf len (length pending))
- (setf name-start 0)))
- ((= name-end len)
- (return (subseq result 0 new-fill-ptr)))
- ((eq kind :directory)
- (setf (schar result new-fill-ptr) #\/)
- (setf fill-ptr (1+ new-fill-ptr))
- (setf name-start (1+ name-end)))
- (t
- (return nil))))))))))
-
-(defun unix-simplify-pathname (src)
- (declare (simple-string src))
- (let* ((src-len (length src))
- (dst (make-string src-len))
- (dst-len 0)
- (dots 0)
- (last-slash nil))
- (macrolet ((deposit (char)
- `(progn
- (setf (schar dst dst-len) ,char)
- (incf dst-len))))
- (dotimes (src-index src-len)
- (let ((char (schar src src-index)))
- (cond ((char= char #\.)
- (when dots
- (incf dots))
- (deposit char))
- ((char= char #\/)
- (case dots
- (0
- ;; Either ``/...' or ``...//...'
- (unless last-slash
- (setf last-slash dst-len)
- (deposit char)))
- (1
- ;; Either ``./...'' or ``..././...''
- (decf dst-len))
- (2
- ;; We've found ..
- (cond
- ((and last-slash (not (zerop last-slash)))
- ;; There is something before this ..
- (let ((prev-prev-slash
- (position #\/ dst :end last-slash :from-end t)))
- (cond ((and (= (+ (or prev-prev-slash 0) 2)
- last-slash)
- (char= (schar dst (- last-slash 2)) #\.)
- (char= (schar dst (1- last-slash)) #\.))
- ;; The something before this .. is another ..
- (deposit char)
- (setf last-slash dst-len))
- (t
- ;; The something is some directory or other.
- (setf dst-len
- (if prev-prev-slash
- (1+ prev-prev-slash)
- 0))
- (setf last-slash prev-prev-slash)))))
- (t
- ;; There is nothing before this .., so we need to keep it
- (setf last-slash dst-len)
- (deposit char))))
- (t
- ;; Something other than a dot between slashes.
- (setf last-slash dst-len)
- (deposit char)))
- (setf dots 0))
- (t
- (setf dots nil)
- (setf (schar dst dst-len) char)
- (incf dst-len))))))
- (when (and last-slash (not (zerop last-slash)))
- (case dots
- (1
- ;; We've got ``foobar/.''
- (decf dst-len))
- (2
- ;; We've got ``foobar/..''
- (unless (and (>= last-slash 2)
- (char= (schar dst (1- last-slash)) #\.)
- (char= (schar dst (- last-slash 2)) #\.)
- (or (= last-slash 2)
- (char= (schar dst (- last-slash 3)) #\/)))
- (let ((prev-prev-slash
- (position #\/ dst :end last-slash :from-end t)))
- (if prev-prev-slash
- (setf dst-len (1+ prev-prev-slash))
- (return-from unix-simplify-pathname "./")))))))
- (cond ((zerop dst-len)
- "./")
- ((= dst-len src-len)
- dst)
- (t
- (subseq dst 0 dst-len)))))
+ (declare (type simple-string pathname))
+ ;; KLUDGE: The Win32 platform doesn't have symbolic links, so
+ ;; short-cut this computation (and the check for being an absolute
+ ;; unix pathname...)
+ #!+win32 (return-from unix-resolve-links pathname)
+ (aver (not (relative-unix-pathname? pathname)))
+ ;; KLUDGE: readlink and lstat are unreliable if given symlinks
+ ;; ending in slashes -- fix the issue here instead of waiting for
+ ;; libc to change...
+ ;;
+ ;; but be careful! Must not strip the final slash from "/". (This
+ ;; adjustment might be a candidate for being transferred into the C
+ ;; code in a wrap_readlink() function, too.) CSR, 2006-01-18
+ (let ((len (length pathname)))
+ (when (and (> len 1) (eql #\/ (schar pathname (1- len))))
+ (setf pathname (subseq pathname 0 (1- len)))))
+ (/noshow "entering UNIX-RESOLVE-LINKS")
+ (loop with previous-pathnames = nil do
+ (/noshow pathname previous-pathnames)
+ (let ((link (unix-readlink pathname)))
+ (/noshow link)
+ ;; Unlike the old CMU CL code, we handle a broken symlink by
+ ;; returning the link itself. That way, CL:TRUENAME on a
+ ;; broken link returns the link itself, so that CL:DIRECTORY
+ ;; can return broken links, so that even without
+ ;; Unix-specific extensions to do interesting things with
+ ;; them, at least Lisp programs can see them and, if
+ ;; necessary, delete them. (This is handy e.g. when your
+ ;; managed-by-Lisp directories are visited by Emacs, which
+ ;; creates broken links as notes to itself.)
+ (if (null link)
+ (return pathname)
+ (let ((new-pathname
+ (simplify-namestring
+ (if (relative-unix-pathname? link)
+ (let* ((dir-len (1+ (position #\/
+ pathname
+ :from-end t)))
+ (dir (subseq pathname 0 dir-len)))
+ (/noshow dir)
+ (concatenate 'string dir link))
+ link))))
+ (if (unix-file-kind new-pathname)
+ (setf pathname new-pathname)
+ (return pathname)))))
+ ;; To generalize the principle that even if portable Lisp code
+ ;; can't do anything interesting with a broken symlink, at
+ ;; least it should be able to see and delete it, when we
+ ;; detect a cyclic link, we return the link itself. (So even
+ ;; though portable Lisp code can't do anything interesting
+ ;; with a cyclic link, at least it can see it and delete it.)
+ (if (member pathname previous-pathnames :test #'string=)
+ (return pathname)
+ (push pathname previous-pathnames))))
+\f
+;;; UNIX specific code, that has been cleanly separated from the
+;;; Windows build.
+#!-win32
+(progn
+ (defconstant micro-seconds-per-internal-time-unit
+ (/ 1000000 sb!xc:internal-time-units-per-second))
+
+ (declaim (inline system-internal-real-time system-internal-run-time))
+ (defun system-internal-real-time ()
+ (multiple-value-bind (ignore seconds useconds) (unix-gettimeofday)
+ (declare (ignore ignore) (type (unsigned-byte 32) seconds useconds))
+ (let ((uint (truncate useconds
+ micro-seconds-per-internal-time-unit)))
+ (declare (type (unsigned-byte 32) uint))
+ (+ (* seconds sb!xc:internal-time-units-per-second)
+ uint))))
+
+ (defun system-internal-run-time ()
+ (multiple-value-bind (ignore utime-sec utime-usec stime-sec stime-usec)
+ (unix-fast-getrusage rusage_self)
+ (declare (ignore ignore)
+ (type (unsigned-byte 31) utime-sec stime-sec)
+ ;; (Classic CMU CL had these (MOD 1000000) instead, but
+ ;; at least in Linux 2.2.12, the type doesn't seem to
+ ;; be documented anywhere and the observed behavior is
+ ;; to sometimes return 1000000 exactly.)
+ (type (integer 0 1000000) utime-usec stime-usec))
+ (let ((result (+ (* (+ utime-sec stime-sec)
+ sb!xc:internal-time-units-per-second)
+ (floor (+ utime-usec
+ stime-usec
+ (floor micro-seconds-per-internal-time-unit 2))
+ micro-seconds-per-internal-time-unit))))
+ result))))
+\f
+;;;; A magic constant for wait3().
+;;;;
+;;;; FIXME: This used to be defined in run-program.lisp as
+;;;; (defconstant wait-wstopped #-svr4 #o177 #+svr4 wait-wuntraced)
+;;;; According to some of the man pages, the #o177 is part of the API
+;;;; for wait3(); that said, under SunOS there is a WSTOPPED thing in
+;;;; the headers that may or may not be the same thing. To be
+;;;; investigated. -- CSR, 2002-03-25
+(defconstant wstopped #o177)
+