;;; -*- Mode:Lisp; Readtable:T; Package:UNIX; Base:8.; Patch-File:T -*-
;;; Patch file for Unix-Interface version 3.1
;;; Reason: better find-unix-stream
;;; Written 12/08/84 15:42:02 by pace,
;;; while running on Lambda Eight from band 1
;;; with Experimental System 99.9, Experimental LMI-System 99.94, Experimental Local-File 51.2, Experimental FILE-Server 9.3, Experimental ZMail 54.3, Experimental Unix-Interface 3.0, Experimental MagTape 34.1, microcode 675, almost microcompiler base band GC.



; From file LAMTTY.LISP#> QL.UNIX; LAM3:
#8R UNIX#:
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "UNIX")))
  (COMPILER#:PATCH-SOURCE-FILE "SYS: UNIX; LAMTTY  "

(defun find-unix-stream (with-login-p &aux str)
  (do-forever
    (catch-error-restart-explicit-if t (no-unix-stream nil "Try again.")
      (if (setq str (find-unix-stream-internal with-login-p))
	  (return-from find-unix-stream str))
      (ferror 'no-unix-stream
	      "couldn't find unix stream with~:[out~] login" with-login-p))))

(defun find-unix-stream-internal (with-login-p)
  (declare (values unix-stream-pathname port-number))
  (assure-*share-ttys*-set-up)
  (dotimes (i (length *share-ttys*))
    (let* ((shared-device-pathname (fs:parse-pathname (format nil "UNIX-STREAM-~d:" i)))
	   (shared-device (send shared-device-pathname :host)))
      (cond ((and (not (eq (car (send shared-device :lock)) current-process))
		  (send shared-device :get-lock nil))
	     (cond ((send shared-device :allocate-if-easy)
		    (cond ((eq (ldb-test si:%%share-tty-csr-opened
					 (si:%share-tty-ucsr
					   (get shared-device 'share-tty)))
			       with-login-p)
			   (return shared-device-pathname i)))
		    (send shared-device :deallocate)))
	     (send shared-device :free-lock))))))
))
