Google
 

Trailing-Edge - PDP-10 Archives - decuslib10-02 - 43,50304/mtfile.mac
There are no other files named mtfile.mac in the archive.
TITLE MTFILE - MAGTAPE FILE HANDLER
SUBTTL	R. Freedman(First Data)/DMC/J Chan  UWO	08 Nov 74

	ENTRY	MTFILE
IFNDEF %%SCNM,<SEARCH C,SCNMAC>

	VFILE==4
	VUPDATE==0
	VEDIT==43		;[43] RNF 6-DEC-74 DATE75 FIXES
				;[43] ALSO CHECK FOR DV.TTY SINCE
				;[43] DV.M14 DOES NOT PRECLUDE DV.TTY AT FDC
				;[42] RNF 08-Nov-74 include AECL edits
				;[41] RNF 9-AUG-74 FIX TO USE DEFAULT
				;[41] OUTPUT PATH INSTEAD OF [PRJ,PRG]
				;[40] RNF 5-AUG-74 DATE75 CHANGES
				;[40] DEFAULT PROT IF GETPPN FAILS IS 057
	VCUSTOM==8
COMMENT !
[AECL.001] PUT FTELL (FILE NAME OUTPUT) IN RIGHT PLACE
[AECL.002] STORE INPUT FILE NAME FOR REPORTING
[AECL.003] FIX FLAG FOR 'NOT FIRST FILE' (IE ONE FILE ALREADY CLOSED)
[AECL.004] USE PHYSICAL NAMES
[AECL.005] IF YOU WANT /ALL MAKE LKWILD DO ALL STRUCTURS
[AECL.006] FIX INITIAL SET UP OF REGISTER F
[AECL.007] COUNT WORDS IN FILE RATHER THAT USE RIB BECAUSE RIB MAY
           BE FROM FIRST OF SEVERAL FILES
[AECL.008] RECOGNIZE PHYSICAL EOT AS A KNOWN ERROR CONDITION

END OF COMMENT !

	LOC	<.JBVER==137>
	<VCUSTOM>B2+<VFILE>B11+<VUPDATE>B17+VEDIT

F=0	;AC DEFINITIONS
T=<T1==1>
T2=2
T3=3
T4=4
P1=<W=5>
P2=<E=6>
P3=<N=7>
P4=10
CH=11
G=12
H=13
I=14
J=15
P=17

ICHN==1
OCHN==2

XX==0			;DENOTES BYTE TO BE STORED

MAGSIZ==203		;BUFFER SIZE FOR MTA
DSKSIZ==203		;BUFFER SIZE FOR DSK
CSIZE==5		;CLUSTER SIZE FOR DSK

;MTFILE will ask for <CSIZE+1> buffers for DSK, and as many buffers
;for tape as will fill up the last K of low segment core (at least two).
;LEFT HALF FLAG BITS
S.ALL==1	;GET ALL OCCURRENCES OF FILENAME
S.FAST==2	;FAST DIRECTORY
S.G==4		;GARBAGE COLLECT
S.IGN==10	;IGNORE PPN ON MTFILE TAPE
S.LIST==20	;DIRECTORY
S.NORM==40	;NOT AN MTFILE DEVICE
S.TIME==100	;LONG DIRECTORY
S.Q==200	;ALLOW NUMERIC FILE NAMES
S.W==400	;REWIND
S.X==1000	;ONE-TO-ONE COPY
S.Z==2000	;CLEAR DIRECTORY
S.NTFR==4000	;[AECL.003]SET NOT FIRST CLOSE ON INPUT (PROBLY MERGING FILES)

;RIGHT HALF FLAG BITS
F.TTY==1	;TTY USED FOR IO
F.AIN==2	;INPUT IN ASCII
F.AOU==4	;OUTPUT IN ASCII
F.SI==10	;INPUT SPECIFICATION HAS BEEN SEEN
F.SO==20	;OUTPUT SPECIFICATION HAS BEEN SEEN
F.SP==40	;OUTPUT DEVICE IS SPOOLED (ENTER IS OPTIONAL)
F.I==100	;INPUT MAGTAPE IS A MTFILE DEVICE
F.O==200	;OUTPUT MAGTAPE IS A MTFILE DEVICE
F.STAR==400	;FILE SPECIFICATION INCLUDES WILD CARD
F.DI==1000	;INPUT DEVICE HAS A DIRECTORY
F.DO==2000	;OUTPUT DEVICE HAS A DIRECTORY
F.FSTI==4000	;FIRST BUFFER OF INPUT FILE - LONG HEADER
F.FSTO==10000	;FIRST BUFFER OF OUTPUT FILE - LONG HEADER
F.FFLI==20000	;FIRST INPUT FILE
F.ERR==40000	;INPUT ERROR DURING GARBAGE COLLECTION (PASS 1)
F.PRT==100000	;NON-STANDARD PROTECTION, REMEMBER TO RENAME
F.DTI==200000	;INPUT DEVICE IS A DECTAPE
F.NEW==400000	;NEW STAR-SEARCH BEGUN

IFNDEF PURE,<PURE==1>
IFN PURE,<
	TWOSEG
	RELOC	400000>
IFE PURE,<RELOC>

IFNDEF CHK,<CHK==1		;CHKACC ATTEMPTED IF NON-ZERO>
IFNDEF SFD,<SFD==1		;SFD'S FOR DISK>
IFNDEF NSPEC,<NSPEC==10		;NO OF SCANNER SPECS IN A + SET>

DEFINE SYSGET (TABLE,INDEX,STORE,DEFAULT) <
	MOVE	T,[INDEX,,TABLE]
	GETTAB	T,
	   MOVE	T,[DEFAULT]
	MOVEM	T,STORE>
SUBTTL COMMAND DECODER
MTFILE:	TDZA	T1,T1
	MOVEI	T1,1
	MOVEM	T1,OFFSET
	RESET
	MOVEI	T,B.DC##	;INPUT BUFFER HEADER
	MOVEM	T,IBLCK+2
	MOVE	T,[OBUF,,OBUFI]
	MOVEM	T,OBLCK+2
	MOVE	P,[XWD CLEAR,CLEAR+1]
	MOVSI	F,S.IGN	;[AECL.006]WE WANT S.IGN NOT LEFT HALF OF AC 10
	SETZM	CLEAR
	BLT	P,CLREND-1
	DATE	T,
	MOVEM	T,THSDAT
	SETOM	PTHARG		;GET USER'S DEFAULT PATH
	MOVE	T,[PTHLEN,,PTHARG]
	PATH.	T,
	  SETZM	PTHARG
	GETPPN	T,
	MOVEM	T,USRPPN	;SAVE USER'S PPN
	SKIPN	PTHARG
	MOVEM	T,PTHDIR
	SYSGET	(16,0,MFDPPN,<XWD 1,1>)
	SYSGET	(16,2,FSFPPN,<XWD 1,2>)
	SYSGET	(16,12,STPROT,057B8);[40]
	SYSGET	(16,13,UFDPRT,775B8)
	MOVE	T,.JBFF##
	MOVEM	T,TTYBF
	MOVE	P,PDP
	MOVE	T1,[3,,[IOWD 1,['MTFILE']
			OFFSET,,'MTF'
			TYPE]]
	PUSHJ	P,.ISCAN##	;INITIALIZE COMMAND SCANNER
TYIB:	MOVE	T,[11,,[IOWD MTSWTL,MTSWTN
			MTSWTD,,MTSWTM
			MTSWTP
			-1
			CLANS,,CLFIL
			AIN,,AOUT
			0
			0
			0]]
	PUSHJ	P,.TSCAN##
	SKIPN	ODEV		;HAS INPUT SPEC BEEN SEEN?
	PUSHJ	P,INITO
	SKIPN	ISPEC1		;HAS INPUT SPEC BEEN SEEN?
	JRST	TYIB		;NO, COMMAND LIKE "MTA:/W="
	TLNE	F,S.FAST!S.TIME
	TLO	F,S.LIST
	PUSHJ	P,CONEND
	TLNN	F,S.X
	PUSHJ	P,CLOSEO
	JRST	TYIB
CLANS:	SETZM	ZERB
	MOVE	T,[ZERB,,ZERB+1]
	BLT	T,ZERND
	SETZM	B.DC
	RELEASE	OCHN,
	RELEASE	ICHN,
	SETZM	DEVO
	SETZM	DEVI
	MOVEI	T,ISPEC1		;SET POINTER FOR + SET
	MOVEM	T,INZR
	SETZM	NXZR
	HRRZ	T,TTYBF
	MOVEM	T,.JBFF
	ADDI	T,DSKSIZ*<CSIZE+1>+MAGSIZ*2
	CAMLE	T,.JBREL##
	CORE	T,
	  JFCL
	AND	F,[S.ALL!S.IGN!S.Q,,0]	;CLEAR ALL TEMPORARY FLAG BITS
	POPJ	P,

CLFIL:	TRZE	F,F.SO		;OUTPUT SPEC JUST SEEN?
	PUSHJ	P,INITO		;YES, USE IT BEFORE LOCAL SWITCHES GO AWAY
	TRNE	F,F.SI
	PUSHJ	P,CONSET	;GO DO INPUT SPEC SET
	SKIPE	CONFLG		;WAS A + TERMINATOR
	POPJ	P,		;YES
	SETZM	LOCAL
	MOVE	T,[LOCAL,,LOCAL+1]
	BLT	T,LOCEND
	POPJ	P,

AIN:	SKIPN	OSPEC		;HAS AN OUTPUT SPEC BEEN SEEN?
	JRST	NOOUT		;NO, MUST BE AN ERROR
	TRO	F,F.SI		;FLAG INPUT SEEN
	MOVEI	T,ILSPEC
	SKIPN	NXZR
	MOVE	T,INZR
	ADDB	T,NXZR
	CAIL	T,SPECLM
	JRST	ERRTMI
	MOVEI	T2,ILSPEC
	POPJ	P,

AOUT:	TRO	F,F.SO
	MOVEI	T,OSPEC
	MOVEI	T2,OLSPEC
	POPJ	P,
;HERE AFTER AN INPUT SPEC IS FOUND

;CONSET -- SEE IF TERMINATOR IS '+'AND DETERMINE WHEN TO PROCESS
;		THE INPUT SPECS
CONEND:	TLNN	F,S.G
	JRST	CONSET		;G SW NOT SET
	SKIPE	NISPEC		;ONLY ONE INPUT MTFILE
	JRST	MULG		;MORE THAN ONE

CONSET:	AOS	NISPEC
	MOVE 	T,NXZR
	MOVE	T2,.FXMOD(T)		;CHK TERMINATOR	
	TLNE	T2,(FX.TRM)
	JRST	CONST1			;+ SIGN SEEN
	SKIPE	CONFLG			;WAS A +SET FOUND
	JRST	CONST2			;YES
	SETZM	NXZR
	MOVE	T,[ISPEC1,,ISPEC]
	BLT	T,ISPEC+ILSPEC-1
	MOVEI	T,ISPEC1		;POINT TO ISPEC	
	MOVEM	T,XINZR
	MOVEM	T,XNXZR
	SETZM	XCONFG
	JRST	CONST7			;GO DO THIS SPEC

CONST1:	AOS	CONFLG
	POPJ	P,

CONST2:	PUSHJ	P,CONST1		;MUST BE LAST SPEC OF A SET
	MOVE	T,INZR
CONST3:	MOVEM	T,XINZR
	SETOM	XCONFG
CONST4:	AOS	XCONFG
	MOVE	T2,XINZR		;FIRST OF A SET
	MOVSI	T3,(FX.NOM)
	ORB	T3,.FXMOD(T)	
	MOVSI	T4,(FX.PHY)
	AND	T3,T4			;/PHYSICAL FOUND IN THE SET
	ORM	T3,.FXMOD(T2)
	MOVE	T3,.FXDEV(T)	
	ADDI	T,ILSPEC
	CAMG	T,NXZR	
	CAME	T3,.FXDEV(T)
	JRST	CONST5
	HRLI	T2,-2*.FXLND
	MOVE	T4,T			;SAVE T FOR NEXT TIME THRU
CONSTA:	MOVE	T3,.FXDIR(T4)
	AOS	T4
	CAMN	T3,.FXDIR(T2)
	AOBJN	T2,CONSTA
	JUMPGE	T2,CONST4	
;HERE PROCESS ONE SET OF INPUT SCANNER SPECS

CONST5:	SUBI	T,ILSPEC		;RESET POINTER
	MOVEM	T,XNXZR
	HRRI	T,ISPEC	
	HRL	T,XINZR	
	BLT 	T,ISPEC+ILSPEC-1	
	TLNN	F,S.Q			;NO FILE NUMBER
	SKIPN	XCONFG			;ONLY ONE SPEC
	 JRST	CONST7			;YES - SKIP FOLLOWING LINES
	MOVE	T,XINZR
	MOVE	T3,.FXMOD(T)
	MOVE	T2,.FXDEV(T)		;CHK IF MTA
	TLNE	T3,(FX.PHY)
	DEVCHR	T2,UU.PHY
	TLNN	T3,(FX.PHY)
	DEVCHR	T2,
	TLNN	T2,(DV.MTA)
	 JRST	CONST7			;NOT A MTA
	MOVE	T2,S.BEG
	MOVEM	T2,XBEG
	MOVE	T3,S.END
	MOVEM	T3,XEND
	JUMPE	T3,CONST6		;CHK IF S.BEG GR S.END
	CAMGE	T3,T2
	JRST	ILLBEG

CONST6:	MOVE	T3,.FXNAM(T)		;LOOP TO FIND ID NAM NUMERIC
	MOVEM	T3,NUMCHK		;ADJUST S.BEG AND S.END IF NECESSARY
	PUSHJ	P,CHKNUM		;CHK NAM IS NUMERIC
	 JRST	CONSTB			;NO
	PUSH	P,T2
	PUSHJ	P,GETSPT
	POP	P,T3
	MOVEM	T3,INUM-1(T2)		;SAVE IT FOR CHKFNM
	CAMG	T3,S.BEG
	MOVEM	T3,S.BEG
	MOVE	T4,S.END
	JUMPE	T4,CONSTB
	CAML	T3,T4
	MOVEM	T3,S.END
CONSTB:	ADDI	T,ILSPEC
	CAMG	T,XNXZR
	JRST	CONST6

CONST7:	PUSHJ	P,INITI0		;PROCESS IT
	MOVE	T,XNXZR
	ADDI	T,ILSPEC
	CAMG	T,NXZR	
	JRST	CONST3	
	SETZM	CONFLG
	SETZM	NXZR
	SETZM	ISPEC
	MOVE	T,[ISPEC,,ISPEC+1]
	BLT	T,SPECLM
	POPJ	P,
SUBTTL DEVICE INITIALIZATION
INITO:	SKIPN	T,ODEV
	MOVSI	T,'DSK'
	MOVEM	T,ODEV
	MOVEM	T,DEVO
	MOVE	T3,OPROT
	TLNE	T3,(FX.PHY)
	DEVCHR	T,UU.PHY
	TLNN	T3,(FX.PHY)
	DEVCHR	T,
	MOVEM	T,DVCHRO
	MOVE	T2,DEVO		;GET NAME BACK [AECL.004]
	DEVNAM	T2,		;FIND OUT  ITS REAL NAME [AECL.004]
		JFCL		;WE'LL  CATCH THIS LATER [AECL.004]
	MOVEM	T2,DEVONM	;SAVE FOR IN=OUT COMPARE [AECL.004]
	TLNE	T,(DV.TTY)	;OUTPUT TO TTY?
	TRO	F,F.TTY		;YES, SUPPRESS INFO OUTPUT
	ANDI	T3,FX.PAR!FX.DEN
	LSH	T3,^L<FX.PAR!FX.DEN>-^L<7B28>
	TDNN	F,[S.LIST+F.TTY];[43] DIRECTORY LISTING?
	TRNN	T,DV.M14	;[43] NO,IS BINARY ILLEGAL?
	TROA	F,F.AOU		;YES, USE ASCII
	SKIPA	T2,[.IOBIN]
	MOVEI	T2,.IOASC
	OR	T2,T3
	MOVE	T3,OPROT
	TLNE	T3,(FX.PHY)
	HRLI	T2,(UU.PHS)
	MOVEM	T2,OBLCK
	OPEN	OCHN,OBLCK
	  JSP	T2,NOTAVL
	TLZE	F,S.W
	MTREW.	OCHN,
	SKIPN	T2,MTOP
	JRST	NOMTOP
	MTAPE	OCHN,@MTAPES-1(T2)
	CAIE	T2,MTOPB
	JRST	NOMTOP
	MTWAT.	OCHN,
	STATO	OCHN,IO.BOT
	MTSKF.	OCHN,
NOMTOP:	TLZN	F,S.NORM
	TLNN	T,(DV.MTA)
	SKIPA
	TRO	F,F.O
	TLNN	T,(DV.DIR)
	TRNE	F,F.O
	TRO	F,F.DO		;ENTER'S WILL BE NECESSARY
	MOVE	T2,DEVO		;IS DEVICE SPOOLED?
	MOVE	T4,[DEVTYP T2,]
	TLNE	T3,(FX.PHY)
	TRO	T4,UU.PHY
	XCT	T4
	  JRST	NOTSPL
	TLNE	T2,(TY.SPL)
	TRO	F,F.SP
NOTSPL:	TLZN	F,S.Z		;CLEAR DIRECTORY?
	JRST	INITO3		;NO
	TLNE	T,(DV.DTA)
	UTPCLR	OCHN,		;CLEAR DECTAPE
	TLNE	T,(DV.DSK)
	JRST	USEPIP		;DON'T CLEAR DISK
	TRNN	F,F.O		;MTFILE TAPE?
	JRST	INITO3
	MTREW.	OCHN,
	MOVEI	T2,1
	MOVEM	T2,S.BEG
	MTWAT.	OCHN,
	MTEOF.	OCHN,
INITO3:	MOVEI	T2,OBLCK	;GET BUFFER SIZE
	DEVSIZ	T2,
	  JRST	NOOBUF
	JUMPLE	T2,NOOBUF
	TLNE	T,(DV.DSK)	;IS OUTPUT DEVICE A DISK?
	JRST	[OUTBUF	OCHN,CSIZE+1
		JRST	NOOBUF]
	HRRZ	T,.JBREL
	SUBI	T,DSKSIZ*<CSIZE+1>
	SUB	T,.JBFF
	IDIVI	T,(T2)
	SKIPG	T
	MOVEI	T,2
	TRNN	F,F.O		;FILE OUTPUT DEVICE?
	AOSA	T
	INBUF	OCHN,1		;YES, INPUT MAY BE NEEDED FOR POSITIONING
	OUTBUF	OCHN,-1(T)
NOOBUF:	MOVE	T2,.JBFF	;SAVE PRESENT LIMIT IN CASE OF
	HRLM	T2,TTYBF	;SEVERAL INPUT DEVICES
	SKIPN	T2,S.BEG	;B SWITCH?
	POPJ	P,		;NO
	MOVEM	T2,RECNUM	;YES, SKIP TO FILE NUMBER GIVEN
	PJRST	SEEKD
INITI0:	MOVE	T,IDEV		;IS THERE A NEW INPUT DEVICE?
	CAMN	T,DEVI
	JRST	INITI2
	MOVEM	T,DEVI
	TRZ	F,F.AIN!F.I!F.DI!F.DTI
	MOVE	T3,IMOD
	MOVE	T2,T	;GET THE NAME [AECL.004]
	DEVNAM  T2,	;FIND  ITS REAL NAME [AECL.004]
		JFCL  ;WE'LL GET THIS LATER [AECL.004]
	TLNE	T3,(FX.PHY)
	DEVCHR	T,UU.PHY
	TLNN	T3,(FX.PHY)
	DEVCHR	T,		;GET ITS CHARACTER
	TLNN	T,(DV.DSK)	;IF DISK, ALWAYS OK [AECL.004]
	CAME	T2,DEVONM	;SEE IF  SAME DEVICE? [AECL.004]
	CAIA			;ALL  OK [AECL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/G OR /L UNLESS /BEG:#
	MTREW.	ICHN,
SUBTTL FILE MANAGEMENT
PROCESS:MOVEM	P1,SAVP1
	MOVEM	P2,SAVP2
	MOVEM	P3,SAVP3
	MOVEM	P4,SAVP4
	TLNE	F,S.LIST
	JRST	LISTER
	TLNE	F,S.G
	JRST	GC
	TRNN	F,F.FFLI
	SETZM	ASAV
	TRON	F,F.FFLI
	TRNE	F,F.TTY
	JRST	NHEAD
	OUTSTR	[ASCIZ /FILES TRANSFERRED:
/]
NHEAD:	TRO	F,F.NEW
	HLLZ	T2,IEXT
	OR	T2,INAM
	TRNN	F,F.DI		;DOES INPUT DEVICE HAVE A DIRECTORY?
	JUMPE	T2,PROC2	;NO, THEN NULL NAME IS NOT WILD
	MOVE	T3,INAMN	;CHECK FOR "WILD" FILE NAME
	HRRO	T2,IEXT
	AOSN	T3
	AOSE	T2
	TRO	F,F.STAR
PROC2:	JRST	LOOKI
LOOKOK:	TLNE	F,S.LIST	;DIRECTORY LISTING?
	JRST	[PUSHJ	P,LSTFIL
		JRST	STRCHK]
	PUSHJ	P,FTELL		;TYPE THE NAME OF THE FILE TO BE COPIED [AECL.001]
	TLNE	F,S.X		;ONE-TO-ONE COPY?
	JRST	PROC1		;YES, ENTER MUST BE PERFORMED
	MOVE	T,ASAV		;[AECL.003]GET IT
	TLNN	F,S.NTFR	;[AECL.003]SEE IF NEED OUTPUT ENTER
PROC1:	PUSHJ	P,ENTO
	TRNN	F,F.I		;FILE INPUT DEVICE?
	PUSHJ	P,INP		;NO, LOOKUP DID NOT INCLUDE AN INPUT
PROCG:	PUSHJ	P,HDRI		;SET UP AOBJN POINTER IN T3
PROC3:	SOSG	OBUF+2
	PUSHJ	P,[PUSHJ P,OUP
		PJRST HDRO]
	MOVE	T2,(T3)
	IDPB	T2,OBUF+1
	AOBJN	T3,PROC3
	PUSHJ	P,INP
	PUSHJ	P,HDRI
	JRST	PROC3
SUBTTL FILE MANAGEMENT - LOOKUPS
LOOKI:	TRNE	F,F.I		;INPUT FROM MTFILE DEVICE?
	JRST	SEEKR		;YES, DO MANUAL LOOKUP
	TRNN	F,F.DI
	JRST	SETPPN
	MOVSI	T1,(FX.NOM)	;SUPPRESS "NO SUCH" MESSAGE FROM WILD
	TLNE	F,S.ALL		;/ALL SPECIFIED?[AECL.005]
	TLO	T1,(FX.STR)	;YES, TELL LKWLD TO LOOK EVERYWHERE[AECL.005]
	ORM	T1,IMOD		;BY SETTING /OKNONE
ALOOK:	MOVE	T1,[4,,[XINZR,,XNXZR
			IBLCK,,ERIB
			ILSPEC,,RIBLEN
			400000,,PFSPEC]]
	PUSHJ	P,.LKWLD##
	  JRST	[AOJE	T1,ESTAR
		PUSHJ	P,REOPE
		JRST	ALOOKB]
ALOOKB:	MOVEI	T2,FNAMI-1
	PUSH	T2,ERIB+.RBNAM
	PUSH	T2,ERIB+.RBEXT
	PUSH	T2,ERIB+.RBPRV
	PUSH	T2,ERIB+.RBPPN
	TRNE	F,F.DTI		;INPUT FROM DECTAPE?
	JRST	ALOOKD		;YES
	LOOKUP	ICHN,ERIB
	  JRST	FNTFNE
	PUSHJ	P,.CHKTM##
	  JRST	ALOOK
	TRZ	F,F.NEW
	HRRZ	T2,ERIB+.RBEXT	;[43]
	HRRM	T2,FNAMI+1	;[43]
	MOVE	T2,ERIB+.RBPRV
	MOVEM	T2,FNAMI+2
	JRST	LOOKOK

NODIR:	MSTIME	T3,		;GENERATE CREATION TIME,PROTECTION
	IDIVI	T3,^D1000*^D60	;MSECS TO MINUTES
	ASH	T3,^D12
	DATE	T4,
	LDB	T1,[POINT 3,T4,23]	;[43] HI DATE
	DPB	T4,[POINT 12,T3,35]	;[40] LO DATE
NODIR2:	OR	T3,STPROT
	TRNN	F,F.O		;[41] DEFAULT PPN IF TO MTFILE TAPE
	TDZA	T4,T4		;[41] OTHERWISE LET MONITOR SUPPLY IT
	MOVE	T4,PTHDIR	;[41] 
	POPJ	P,

SETPPN:	MOVEI	T2,FNAMI-1
	PUSH	T2,INAM
	PUSH	T2,IEXT
	TRNE	F,F.STAR	;"WILD" FILE NAME?
	JRST	ILLWI		;NOT ALLOWED ON NON-DIRECTORY DEVICE
	PUSHJ	P,NODIR
	SKIPN	FNAMI		;NULL FILE NAME?
	TLNN	F,S.X		;NOT ALLOWED ON /X COPYING
	JRST	OKFNI
	M.FAIL	<Need input file names>
ALOOKD:	TRNE	F,F.NEW		;COULD THIS BE FIRST FILE FOUND?
	PUSHJ	P,REOPE		;IF SO, DTA WAS INITED BY WILD (FOR DIRECTORY)
	LOOKUP	ICHN,FNAMI
	  JRST	FNTFND
	LDB	T3,[POINT 3,FNAMI+1,20]	;[40]
	MOVE	T2,FNAMI+2
	PUSHJ	P,CHKTM
	  JRST	ALOOK
	LDB	T1,[POINT 3,FNAMI+1,20]	;[40] 
	LDB	T3,[POINT 12,FNAMI+2,35];[40] 
	PUSHJ	P,NODIR2
OKFNI:	TRZ	F,F.NEW
	DPB	T1,[POINT 3,FNAMI+1,20]	;[40] 
	MOVEM	T3,FNAMI+2
	MOVEM	T4,FNAMI+3
	SETZM	ERIB
	JRST	LOOKOK
ESTAR:	TRZ	F,F.STAR
	TRNE	F,F.NEW		;ANY FILES FOUND?
	JRST	NSUCH		;NO,LOOKUP FAILURE
	TLNE	F,S.LIST
	PUSHJ	P,ELIST
	POPJ	P,

CHKTM:	LDB	T1,[POINT 11,T2,23]	;GET CREATION TIME
	IMULI	T1,^D60000		;IN MILLISECONDS
	ANDI	T2,RB.CRD
	DPB	T3,[POINT 3,T2,23]	;[40] 
	PUSHJ	P,.CNVDT##		;CONVERT TO INTERNAL FORMAT
	CAMGE	T1,ISNC			;TOO OLD?
	POPJ	P,
	SKIPLE	IBFR
	CAMGE	T1,IBFR			;TOO RECENT?
	AOS	(P)
	POPJ	P,

REOPE:	TRNE	F,F.AIN
IFE .IOASC,<	TDZA	T2,T2>
IFN .IOASC,<	SKIPA	T2,[.IOASC]>
	MOVEI	T2,.IOBIN
	DPB	T2,[POINT 4,IBLCK,35]
	OPEN	ICHN,IBLCK
	  JSP	T2,NOTAVL
	POPJ	P,
;ROUTINE TO PRINT FILE AND EXTENSION IN FNAMI,FNAMI+1 [AECL.001]
FTELL:	TRNE	F,F.TTY		;TYPE FILE NAME IF TTY AVAILABLE
	POPJ	 P,		;RETURN
	MOVE	T2,FNAMI
	PUSHJ	P,QTYPE
	HLLZ	T2,FNAMI+1
	JUMPE	T2,CROUT	;FINISH  WITH <CR>
	PUSHJ	P,DOT
	PUSHJ	P,QTYPE
	PJRST	CROUT
SUBTTL FILE MANAGEMENT - ENTERS
ENTO:	TRNN	F,F.DO!F.SP	;DOES OUTPUT DEVICE HAVE A DIRECTORY?
	JRST	SPEAK
	MOVE	T2,OPROT
	MOVEI	T4,0
	TLNN	F,S.X		;ONE-TO-ONE COPY?
	JRST	[MOVS	T3,ONAMN
		AOJN	T3,ILLFNO
		HRLO	T3,OEXT
		TLNN	T2,(FX.NUL)
		AOJN	T3,ILLFNO
		MOVE	T3,ONAM
		MOVEM	T3,FNAMO
		HLLZ	T3,OEXT
		MOVEM	T3,FNAMO+1
		PUSHJ	P,NODIR
		JRST	ENTX]
	MOVE	T3,ONAM		;INPUT NAME REPLACES OUTPUT IF
	AND	T3,ONAMN	;OUTPUT IS WILD OR NULL
	MOVEM	T3,FNAMO
	MOVE	T3,FNAMI
	ANDCM	T3,ONAMN
	ORM	T3,FNAMO
	HRLZ	T3,OEXT
	AND	T3,OEXT
	MOVEM	T3,FNAMO+1
	HRLZ	T3,OEXT
	ANDCA	T3,FNAMI+1
	ORM	T3,FNAMO+1
	TRNE	F,F.O		;OUTPUT TO TAPE?
	MOVE	T4,PTHDIR	;YES, MONITOR WILL NOT SUPPLY DEFAULT
	MOVE	T3,FNAMI+2	;GET INPUT PROT,MODE,TIME,DATE
	LDB	T1,[POINT 3,FNAMI+1,20]	;[40] 
ENTX:	DPB	T1,[POINT 3,FNAMO+1,20]	;[40] 
	MOVEM	T3,FNAMO+2	;STORE PROT,MODE,TIME,DATE
	TLNE	T2,(FX.DIR)	;DIRECTORY SPECIFIED?
	PUSHJ	P,GETPTH	;YES, CHECK IT OUT
	MOVEM	T4,FNAMO+3
	TRNE	T2,FX.PRO	;PROTECTION SPECIFIED?
	DPB	T2,[POINT 9,FNAMO+2,8]
SPEAK:
NSPEAK:	SKIPE	FNAMO		;DO WE HAVE A FILE NAME FOR ENTER?
	TRNN	F,F.DO!F.SP	;YES, CAN THE DEVICE USE IT?
	POPJ	P,		;NO,FORGET THE ENTER
	TRNE	F,F.O		;OUTPUT TO MTFILE DEVICE?
	JRST	EPCHK		;YES, DO MANUAL ENTER
	LDB	T3,[POINT 4,FNAMO+2,12]
	CAIE	T3,14
	JRST	RNN
	HLLZ	T3,FNAMO+2
	TLZ	T3,000777
	CAIE	T3,STPROT	;NON-STANDARD PROTECTION?
RNN:	TRO	F,F.PRT		;YES,REMEMBER TO RENAME
	MOVE	T3,DVCHRO
	TLNE	F,S.X		;ONE-TO-ONE COPY?
	TLNN	T3,(DV.DSK)	;YES, OUTPUT TO DISK?
	JRST	NORIBO		;NO, DON'T ATTEMPT EXTENDED ENTER
	SKIPN	ERIB		;DO WE HAVE A RIB?
	JRST	NORIBO		;NO
	MOVEI	T2,ERIB
	PUSH	T2,FNAMO+3
	PUSH	T2,FNAMO
	PUSH	T2,FNAMO+1
	PUSH	T2,FNAMO+2
ENRIB:	ENTER	OCHN,ERIB
	  JRST	[HRRZ	T2,ERIB+.RBEXT
		CAIE	T2,ERPOA%
		JRST	FULL+1
		MOVE	T2,FNAMO+1
		MOVEM	T2,ERIB+.RBEXT
		MOVEI	T2,0
		EXCH	T2,ERIB+.RBALC
		MOVEM	T2,ERIB+.RBEST
		JRST	ENRIB]
	POPJ	P,
NORIBO:	MOVEI	T2,7777
	TLNE	T3,(DV.DTA)	;TO DTA?
	ANDM	T2,FNAMO+2	;YES, SCRAP PROT, MODE, TIME
	MOVE	T2,FNAMO+1
	ENTER	OCHN,FNAMO
	  JRST	FULL
	MOVEM	T2,FNAMO+1
	POPJ	P,
EPCHK:	LDB	T2,[POINT 9,STPROT,8]
	HRLI	T2,2
	MOVE	T3,FNAMO+3
	MOVE	T4,USRPPN
IFN CHK,<
	MOVEI	T,T2
	CHKACC	T,		;CAN USER WRITE THIS PPN?
	  JRST	MANUFD
	JUMPE	T,SEEKW		;YES
	JRST	ENOK
MANUFD:>
	MOVE	T2,UFDPRT	;CHKACC WON'T SAY, LET'S FIGURE IT OUT
	TLNE	T2,(2B8)	;CAN PUBLIC CREATE IN UFD?
	JRST	SEEKW		;YES, WHAT A FRIENDLY INSTALLATION!
	CAMN	T4,FSFPPN	;PRIVILEGED PPN CAN CREATE ANYWHERE
	JRST	SEEKW
	XOR	T4,FNAMO+3
	TRNN	T4,-1		;IS USER THE OWNER?
	JRST	SEEKW		;YES, HE CAN CREATE
	TRNN	T4,-1		;IS USER THE SAME PROJECT?
	TLNE	T2,(20B8)	;YES, DOES PROJECT HAVE PRIVILEGE?
	JRST	SEEKW
ENOK:	M.FAIL	<Can't write that PPN>
GETPTH:	MOVE	T4,FNAMI+3
	ANDCM	T4,ODIRM
	OR	T4,ODIRM
	AND	T4,ODIR		;CHANGE [*,X] TO [INPUT,X]
	TRNN	T4,-1
	HRR	T4,PTHDIR
	TLNN	T4,-1
	HLL	T4,PTHDIR	;CHANGE [,X] TO [DEFAULT,X]
IFN SFD,<
	MOVEM	T4,EDIR
	PUSH	P,T2
	MOVEI	T2,0
	MOVSI	T3,-<.FXLND-1>
PTHLP:	SKIPE	T4,ODIR+3(T2)
	AOJN	T4,ILLPO
	SKIPN	T4,ODIR+2(T2)
	MOVE	T4,PTHDIR+1(T3)
	MOVEM	T4,EDIR+1(T3)
	JUMPE	T4,NOPTH
	ADDI	T2,2
	AOBJN	T3,PTHLP
NOPTH:	JUMPE	T2,T2POPJ
	TRNE	F,F.O
	JRST	ILLPO		;CAN'T SPECIFY PATH TO MTFILE-TAPE
	SKIPA	T4,[EARG]
T2POPJ:	MOVE	T4,EDIR
	POP	P,T2>
	POPJ	P,
SUBTTL FILE MANAGEMENT - DATA
HDRI:	TRNN	F,F.I		;MTFILE TAPE OR NORMAL DEVICE?
	JRST	NORMI
	MOVE	T2,B.DC+1	;MTFILE TAPE
	HLLZ	T3,1(T2)	;LH(HEADER WORD 1)=-DATA WORD COUNT
	JUMPE	T3,CLOSEN	;QUIT IF NULL FILE
	HRRI	T3,2(T2)
	TRZN	F,F.FSTI
	POPJ	P,
	ADDI	T3,4		;FIRST RECORD HAS BIGGER HEADER
	HRRZ	T4,3(T2)
	JUMPE	T4,CPOPJ
	ADD	T3,6(T2)	;ADD RIBLEN TO DATA OFFSET IF EXTENDED RIB
	POPJ	P,
NORMI:	MOVN	T4,B.DC+2	;NORMAL DEVICE
	AOS	T3,B.DC+1	;DATA WORD COUNT FROM MONITOR
	TRNE	F,F.AIN
	IDIVI	T4,5
	HRL	T3,T4
	POPJ	P,

HDRO:	TRNN	F,F.O		;MTFILE TAPE OR NORMAL DEVICE?
	JRST	NORMO
	AOS	T,OBUF+1	;MTFILE TAPE, GENERATE HEADER
	TRZE	F,F.FSTO
	JRST	HDRO1
	SOS	T2,OBUF+2	;HEADER (EXCEPT FOR FIRST BLOCK)
	MOVNS	T2		; -DATA WORD COUNT,,FILE NUMBER
	HRL	T2,NUMSAV
	MOVSM	T2,(T)
	POPJ	P,
HDRO1:	MOVSI	T2,FNAMO	;HEADER (FOR FIRST BLOCK)
	HRRI	T2,1(T)		; -DATA WORD COUNT,,FILE NUMBER
	BLT	T2,4(T)		; FILENAME
	MOVEI	T2,1	;[40] 	;<EXT>B17+<HI DATE>B20+<RIBFLAG>B35
	ANDCAM	T2,2(T)	;[40]	 	;RIBFLAG=1 IF RIB IS PRESENT
	SKIPE	ERIB
	JRST	EHDRO		; <PROT>B8+<MODE>B12+<TIME>B23+<DATE>B35
	MOVEI	T2,4(T)		; PRJ,,PRG
	HRRM	T2,OBUF+1	; RIB (IF PRESENT)
	MOVNI	T2,5		; . . .
HDRO2:	ADDB	T2,OBUF+2
	MOVNS	T2
	HRL	T2,NUMSAV
	MOVSM	T2,(T)
	POPJ	P,
NORMO:	TRNN	F,F.AOU		;NORMAL DEVICE
	POPJ	P,		;BUFFER HEADER ALREADY BINARY MODE
	MOVE	T4,OBUF+2	;CONVERT ASCII HEADER TO 36-BIT BYTES
	IDIVI	T4,5
	MOVEM	T4,OBUF+2
	MOVSI	T4,(POINT 36,XX,35)
	HLLM	T4,OBUF+1
	POPJ	P,
EHDRO:	AOS	2(T)
	PUSH	P,T3
	SKIPE	ERIB+.RBVER	;GET VERSION INTO RIB IF IT SEEMS RIGHT
	JRST	VERNON
	HLRZ	T2,ERIB+.RBEXT
	CAIE	T2,'HGH'	;SEE IF VERSION SEARCH IS MEANINGFUL
	CAIN	T2,'SHR'
	JRST	VERHGH
	CAIE	T2,'SAV'
	CAIN	T2,'LOW'
	JRST	VERLOW
	CAIE	T2,'SVE'
	JRST	VERNON		;NO, FORGET IT
VERLOW:	MOVEI	T2,1(T3)
VERLO1:	MOVE	T4,-1(T2)
	JUMPE	T4,VERNON	;T4 SHOULD BE FIRST IOWD
	SKIPA
VERLO2:	AOBJP	T4,[AOJA T2,VERLO1]
	HRRZ	T3,T4
	CAIGE	T3,.JBVER-1
	AOJA	T2,VERLO2
	CAIE	T3,.JBVER-1	;MAKE SURE WE DIDN'T GO PAST
	JRST	VERNON
	MOVE	T4,0(P)
	HRLE	T3,T4		;AND MAKE SURE THIS WORD IS WITHIN BUFFER
	MOVMS	T3
	ADDI	T3,-1(T4)
	CAMGE	T3,T2
	JRST	VERNON
	MOVE	T2,(T2)
	JRST	VERGOT
VERHGH:	MOVE	T2,.JBHVR##(T3)
VERGOT:	MOVEM	T2,ERIB+.RBVER
VERNON:	MOVSI	T2,ERIB
	POP	P,T3
	HRRI	T2,5(T)
	MOVEI	T4,4(T)
	ADD	T4,ERIB
	BLT	T2,0(T4)
	HRRM	T4,OBUF+1
	MOVEI	T2,(T)
	SUBI	T2,1(T4)
	JRST	HDRO2
INP:	IN	ICHN,
	POPJ	P,
	STATZ	ICHN,IO.EOF
	JRST	CLOSEI
	GETSTS	ICHN,E
	PUSHJ	P,ABORT
	JRST	IMESS

OUP:	OUT	OCHN,
	POPJ	P,
	GETSTS	OCHN,E
	PUSHJ	P,ABORT
	JRST	OMESS

ABORT:	TRNN	F,F.O		;FILE-DEVICE OUTPUT?
	JRST	[CLOSE	OCHN,CL.RST
		POPJ	P,]
	MTBSF.	OCHN,
	MTWAT.	OCHN,
	STATO	OCHN,IO.BOT
	MTSKF.	OCHN,
	MTEOF.	OCHN,
	MTBSF.	OCHN,
	POPJ	P,
CLOSEN:	IN	ICHN,
	JRST	[PUSHJ	P,IMPLAU
		JRST	IIMP]
CLOSEI:	TLO	F,S.NTFR	;[AECL.003]SAY NOT FIRST
	MOVEM	T,ASAV		;[AECL.003]STORE THIS
	CLOSE	ICHN,CL.OUT
	POP	P,(P)		;DON'T RETURN TO CALLER
	TLNN	F,S.G!S.X	;ONE-TO-ONE COPY OR GARBAGE COLLECT?
	JRST	STRCHK		;NO,OUTPUT MAY BE CONTINUED
CLOSEO:	TRNN	F,F.O		;YES,CLOSE OUTPUT FILE
	JRST	CLOS2
	TRNE	F,F.FSTO	;HAS OUTPUT HEADER BEEN CONSTRUCTED YET?
	JRST	[PUSHJ	P,OUP
		PUSHJ	P,HDRO
		AOS	OBUF+2
		JRST	CLOS1]
CLOS1:	HLRE	T2,(T)		;DATA WORD COUNT IN LAST BUFFER
	ADD	T2,OBUF+2	;MUST BE CORRECTED
	SUBI	T2,1
	HRLM	T2,(T)
CLOS2:	PUSHJ	P,OUP
	TRZE	F,F.PRT		;NON-STANDARD PROTECTION?
	RENAME	OCHN,FNAMO	;NON-ERROR RENAME SKIPS THE CLOSE
	CLOSE	OCHN,CL.IN
STRCHK:	TDNE	F,[S.ALL,,F.STAR]
	JRST	PROC2		;ITERATE IF * SPECIFICATION
	TLNE	F,S.LIST
	PUSHJ	P,ELIST
	TLNE	F,S.G
	POPJ	P,
	MOVE	P1,SAVP1
	MOVE	P2,SAVP2
	MOVE	P3,SAVP3
	MOVE	P4,SAVP4
	POPJ	P,
SUBTTL DIRECTORY FOR FILE TAPE
LISTER:	TRNE	F,F.O
	JRST	ILLOUT
	TROE	F,F.FFLI
	JRST	NOHEAD
	PUSHJ	P,ENTO
	SETZM	NFIL
	SETZM	NBLOK
	MOVEI	P1,0		;SET HORIZONTAL POSITION TO ZERO
	TLNE	F,S.FAST	;SHORT DIRECTORY?
	JRST	NHEADL		;YES, SKIP HEADER
	MOVE	T2,[POINT 6,[SIXBIT /DIRECTORY: #/]]
	PUSHJ	P,LSTN
	MOVE	T2,DEVI
	PUSHJ	P,LSTQ
	PUSHJ	P,LSTTAB
	PUSHJ	P,LSTTAB
	MSTIME	T2,
	IDIVI	T2,^D1000*^D60
	PUSHJ	P,LTIME+1
	DATE	P2,
	PUSHJ	P,LDATE
	MOVEI	P3,64
	PUSHJ	P,SKIPTO
	MOVE	T2,['MTFILE']
	PUSHJ	P,LSTQ
	PUSHJ	P,LSPC
	MOVE	P2,.JBVER
	PUSHJ	P,LVER
	PUSHJ	P,LCRLF
	MOVE	T2,[POINT 6,[SIXBIT /NO.     FILNAM  EXT SIZE PROT   #/]]
	PUSHJ	P,LSTN
	MOVE	T2,[POINT 6,[SIXBIT /CREATION   #/]]
	TLNE	F,S.TIME
	MOVE	T2,[POINT 6,[SIXBIT /----CREATION----   MODE #/]]
	PUSHJ	P,LSTN
	MOVE	T2,[POINT 6,[SIXBIT /VERSION    OWNER#/]]
	PUSHJ	P,LSTN
	PUSHJ	P,LCRLF
	PUSHJ	P,LST7
NHEADL:	MTWAT.	ICHN,		;WAIT HERE FOR REWIND TO FINISH
NOHEAD:	SKIPE	INAM
	JRST	NHEAD
LSTLP:	IN	ICHN,
	JRST	[PUSHJ	P,LSTFIL
		JRST	LSTLP]
	STATO	ICHN,IO.EOF
	JRST	LERR1
	MTBSF.	ICHN,
ELIST:	PUSHJ	P,LCRLF
	MOVEI	P3,4		;PRINT 'TOTAL OF 'NBLOK' BLOCKS IN 'NFIL' FILES'
	PUSHJ	P,SKIPTO
	MOVE	T2,[POINT 6,[SIXBIT /TOTAL OF #/]]
	PUSHJ	P,LSTN
	MOVE	T2,NBLOK
	PUSHJ	P,DCVT
	MOVE	T2,[POINT 6,[SIXBIT / RECORDS IN #/]]
	PUSHJ	P,LSTN
	MOVE	T2,NFIL
	PUSHJ	P,DCVT
	PUSHJ	P,LSPC
	MOVE	T2,[SIXBIT /FILES/]
	PUSHJ	P,LSTQ
	PJRST	LCRLF

LERR:	PUSHJ	P,LSTTAB
	MOVE	T2,[POINT 6,[SIXBIT .***READ ERROR***#.]]
	PUSHJ	P,LSTN
	PUSHJ	P,LCRLF
	INPUT	ICHN,
	STATO	ICHN,IO.EOF
	JRST	.-2
	CLOSE	ICHN,CL.OUT
	SETSTS	ICHN,.IOBIN
	POPJ	P,

LERR1:	PUSHJ	P,LERR
	JRST	LSTLP
LSTFIL:	MOVE	P4,B.DC+1
	AOS	NFIL			;COUNT FILES
	HRRZ	T2,1(P4)	;GET FILE NUMBER
	PUSHJ	P,D3CVT
	PUSHJ	P,LSTTAB
	MOVE	T2,2(P4)
	PUSHJ	P,LSTQ		;GET FILE NAME
	PUSHJ	P,LSTTAB
	HLLZ	T2,3(P4)	;GET FILE EXTENSION
	PUSHJ	P,LSTQ
				;[AECL.007] WE MUST COUNT THE WORDS USED IN
				;THIS FILE BECAUSE THE ERIB MAY HAVE COME
				;FROM THE FIRST OF SEVERAL BLOCKS MERGED!!
	MOVEI	P2,TBLK-1	;PUSH THE REST OF THE ERIB INTO TBLK 
				;(ONLY 4 ARE NEEDED)
	PUSH	P2,3(P4)	;EXT =FLAG FOR RIB PRESENT
	PUSH	P2,4(P4)	;+1=PROTECTION ETC.
	PUSH	P2,5(P4)	;+2=PPN
	PUSH	P2,14(P4)	;+3=VERSION IF PRESENT
;	NOW COUNT THE ACTUAL SIZE OF THE FILE
;	START THE COUNT WITH THIS BUFFER
	HLRO	P2,1(P4)	;-COUNT
	MOVEM	P2,COUNT	;INITIALIZED
SUMLOP:	AOS	NBLOK
	IN	ICHN,		;GET ANOTHER BLOCK
	JRST	MOR		;IT WAS THERE
	STATO	ICHN,IO.EOF	;OK IF EOF
	JRST	LERR		;BUT NOT OTHERWISE
	CLOSE	ICHN,CL.OUT	;DONE WITH THIS ONE
	JRST	FINSUM		;GO SERVICE THE SUM
MOR:	MOVE	P4,B.DC+1	;GET POINTER TO BUFFER
	HLRO	P2,1(P4)	;WAS -WORD COUNT,,FILE NUMBER
	ADDM	P2,COUNT	;KEEP SUM IN MEMORY (NEGATIVE SUM)
	JRST	SUMLOP		;GO FOR MORE

FINSUM:	TLNE	F,S.FAST	;IF SHORT WANTED FORGET SUM
	JRST	NXTLST
	MOVEI	P3,24		;FORMAT THE PAGE
	PUSHJ	P,SKIPTO
	MOVN	P2,COUNT	;COMPLEMENT THE SUM
				;[AECL.007]END OF CHANGES
	ADDI	P2,177
	ASH	P2,-7		;ROUND UP TO # OF BLOCKS
	PUSHJ	P,LDEC4
	SKIPA
NRIBL1:	PUSHJ	P,LSTTAB
	PUSHJ	P,LSPC
	MOVE	P2,TBLK+1	;GET PROTECTION WORD[AECL.007]
	MOVEI	T,"<"
	PUSHJ	P,LST7
	LDB	T2,[POINT 9,P2,8]
	PUSHJ	P,L3Q
	MOVEI	T,">"
	PUSHJ	P,LST7
	PUSHJ	P,LSTTAB
	TLNE	F,S.TIME	;CREATION TIME WANTED?
	PUSHJ	P,LTIME
	ANDI	P2,7777
	LDB	T2,[POINT 3,TBLK,20]	;[40] 
	DPB	T2,[POINT 3,P2,23]	;[40] 
	PUSHJ	P,LDATE
	LDB	P2,[POINT 4,TBLK+1,12]	;[AECL.007]
	TLNE	F,S.TIME
	PUSHJ	P,MODE
	LDB	P2,[POINT 1,TBLK,35]	;[40] 
	JUMPE	P2,NRIBL2
	MOVEI	P3,53
	TLNE	F,S.TIME
	MOVEI	P3,70
	PUSHJ	P,SKIPTO
	SKIP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RZ	T,0(P)
	PJRST	LSTNUM

L3Q:	MOVE	T3,[POINT 3,T2,26]	;T2/ 3-DIGIT OCTAL NUMBER
	ILDB	T,T3
	PUSHJ	P,LSTNUM
	TLNE	T3,770000
	JRST	.-3
	POPJ	P,

DTAB:	ASCII	/-Jan--Feb--Mar--Apr--May--Jun-/
	ASCII	/-Jul--Aug--Sep--Oct--Nov--Dec-/

LTIME:	LDB	T2,[POINT 11,P2,23]
	IDIVI	T2,^D60
	HRLM	T3,0(P)
	PUSHJ	P,D2CVT
	MOVEI	T,":"
	PUSHJ	P,LST7
	HLRZ	T2,0(P)
	PUSHJ	P,D2CVT
	MOVEI	P3,47
SKIPTO:	PUSHJ	P,LSPC
	CAMGE	P1,P3
	JRST	SKIPTO
	POPJ	P,

LSTTAB:	MOVEI	T,11
	JRST	LST7
SUBTTL POSITION FOR WRITING
SEEKD:	CAIG	T2,1
	JRST	LOWNUM
	MTBSF.	OCHN,		;GET BEARINGS
	STATZ	OCHN,IO.BOT
	SOJA	T2,FORSP
	MTBSR.	OCHN,
	IN	OCHN,
	SKIPA
	JRST	OMESS1
	PUSHJ	P,PLAUSO
	  JRST	OIMP
	ILDB	T3,OBUFI+1
	PUSHJ	P,FINOSH
	SUBI	T2,1(T3)
	JUMPE	T2,CPOPJ	;HOW ABOUT THAT FOR LUCK!
	JUMPG	T2,FORSP	;NO LUCK TODAY,GO FORWARD OR BACK?
	MTBSR.	OCHN,
	MTBSF.	OCHN,
	AOJL	T2,.-1
FORSP:	MTSKF.	OCHN,
	SOJG	T2,.-1
FORSP2:	PUSHJ	P,READND	;CHECK THAT POSITIONING IS OK
	MOVE	T2,RECNUM
	CAIE	T2,1(T)
	JRST	OMESS1
	POPJ	P,
LOWNUM:	MTREW.	OCHN,
	MTWAT.	OCHN,		;WAIT HERE FOR REWIND TO FINISH
	JRST	FORSP2

READND:	MTBSF.	OCHN,
	MOVEI	T,0
	MTWAT.	OCHN,
	STATZ	OCHN,IO.BOT
	POPJ	P,
	MTBSR.	OCHN,
	IN	OCHN,
	SKIPA
	JRST	OMESS1
	PUSHJ	P,PLAUSO
	  JRST	OIMP
	ILDB	T,OBUFI+1
FINOSH:	IN	OCHN,		;SKIP THE REST OF THE CURRENT FILE
	JRST	.-1		;(SKIP FILE WILL NOT DO BECAUSE
	STATO	OCHN,IO.EOF	;OF MULTIPLE BUFFERING - THE TAPE
	JRST	OMESS1		;MAY BE ALREADY AT EOF)
	CLOSE	OCHN,CL.OUT
	POPJ	P,
OMESS1:	OUTSTR	[ASCIZ /Positioning output tape
/]
	GETSTS	OCHN,E
	JRST	OMESS

SEEKW:	SKIPE	T,RECNUM	;POSITION SPECIFIED?
	JRST	WRITEA
	SKIPE	T,NUMSAV	;DID WE JUST WRITE FILE N?
	JRST	NUMLST		;YES
	MTEOT.	OCHN,
	PUSHJ	P,READND	;NO, LAST FILE ON TAPE IS FILE N
NUMLST:	HRRZI	T,1(T)		;WRITE FILE N+1

WRITEA:	MOVEM	T,NUMSAV
	SETZM	RECNUM
	TRO	F,F.FSTO
	POPJ	P,

;CHKNUM -- CHECK NUMERIC
;ENTRY 	VALUE IN NUMCHK
;CALL	PUSHJ	P,CHKNUM
;	  RETURN	;NON-NUMERIC
;	SKIP RETURN	;T2 HOLDS THE NUMBER
CHKNUM:	MOVE	T4,[POINT 6,NUMCHK]
	MOVEI	T2,0
CHKNM1:	ILDB	T3,T4
	JUMPE	T3,CPOPJ1	;NUMERIC - SKIP RETURN
	CAIL	T3,'0'
	CAILE	T3,'9'
	POPJ	P,		;NON-NUMERIC
	IMULI	T2,^D10
	ADDI	T2,-20(T3)
	JRST	CHKNM1
SUBTTL POSITION FOR READING
SEEKR:	SKIPE	XCONFG		;SEE IF MORE THAN ONE SPEC
	 JRST	NOTNUM		;MORE THAN ONE 
	MOVE	T4,IMOD		;EXTENSION GIVEN?
	TLNE	T4,(FX.NUL)
	TLNE	F,S.Q		;QUOTE MODE FOR FILE NAME?
	JRST	NOTNUM		;YES, NO FILE NUMBER
	SKIPN	T,INAM
	JRST	NOTNUM
	MOVEM	T,NUMCHK
	PUSHJ	P,CHKNUM
	 JRST	NOTNUM
	JRST	NUMEND

SEEKDI:	CAIG	T2,1		;LOW NUMBER?
	JRST	[MTREW.	ICHN,
		MTWAT.	ICHN,
		POPJ	P,]
	MTBSF.	ICHN,		;GET BEARINGS
	STATZ	ICHN,IO.BOT
	SOJA	T2,FORSPI
	MTBSR.	ICHN,
	IN	ICHN,
	SKIPA
	JRST	IMESS1
	PUSHJ	P,PLASI1
	  JRST	IIMP
	ILDB	T3,B.DC+1
	PUSHJ	P,FINISH
	SUBI	T2,1(T3)
	JUMPE	T2,CPOPJ	;HOW ABOUT THAT FOR LUCK!
	JUMPG	T2,FORSPI	;NO LUCK TODAY,GO FORWARD OR BACK?
	MTBSR.	ICHN,
	MTBSF.	ICHN,
	AOJL	T2,.-1
FORSPI:	MTSKF.	ICHN,
	SOJG	T2,.-1
	POPJ	P,

IMESS1:	OUTSTR	[ASCIZ /Positioning input tape
/]
	GETSTS	ICHN,E
	JRST	IMESS
NOTNUM:	TRNN	F,F.NEW
	JRST	SEEKNM
	MTBSF.	ICHN,		;FILE NAME CONTAINS AT LEAST ONE LETTER
	MTWAT.	ICHN,
	STATO	ICHN,IO.BOT
	MTSKF.	ICHN,
	JRST	SEEKNM

FINISH:	IN	ICHN,		;SKIP THE REST OF THE CURRENT FILE
	JRST	.-1
	STATO	ICHN,IO.EOF
	JRST	IMESS1
	CLOSE	ICHN,CL.OUT
	POPJ	P,

;GETSPT -- GET SCANNER SPEC POINTER STARTING FROM ISPEC1
;CALL	MOVEI	T,SPEC 
;	PUSHJ	P,GETSPT
; T2 HOLDS THE POINTER	USE T3 
GETSPT:	MOVEI	T3,ISPEC1
	MOVEI	T2,1
	CAMG	T,T3
	POPJ	P,
	ADDI	T3,ILSPEC
	CAMLE	T3,NXZR
	POPJ	P,
	AOS	T2
	JRST	GETSPT+2

;CHKFNM -- CHK IF MTA:M WAS FOUND AND ALSO IF M = N
;CALL	MOVEI	T,N
;	MOVEI	P2,SPEC
;	PUSHJ	P,CHKFNM
;	  RETURN		MATCH
;	SKIP RETURN		NOT MATCH
CHKFNM:	PUSH	P,T		;SAVE T
	MOVE	T,P2
	PUSHJ	P,GETSPT
	POP	P,T
	SKIPE	T3,INUM-1(T2)	;GET FILE NUMBER
	CAME	T,T3		;SEE IF MATCH
	AOS	(P)		;NOT MATCH
	POPJ	P,

;CHKRGE -- CHK T IF IN RANGE XBEG TO XEND
;CALL	MOVEI	T,N
;	PUSHJ	P,CHKRGE
;	 RETURN		OUT OF RANGE
;	SKIP RETURN	IN RANGE
CHKRGE:	CAMGE	T,XBEG
	 POPJ	P,
	SKIPE	T3,XEND
	CAMG	T,T3
	AOS	(P)
	POPJ	P,
SEEKOK:	PUSHJ	P,PLAUSI
	  JRST	IIMP
SEEKIS:	MOVE	T4,B.DC+1
	HRRZ	T,1(T4)		;GET NUMBER N
	PUSHJ	P,CHKFNM	;CHK MTA:M = N
	 JRST	NMFND		;GOT IT
	PUSHJ	P,CHKRGE		;CHK IF IN RANGE OF XBEG TO XEND
	 JRST	SEEKMS		;OUT OF RANGE
	HLLZ	T3,3(T4)	;RIGHT HALF OF 3(T4) MAY BE NONZERO
	MOVEM	T3,FNAMI+1
	XOR	T3,.FXEXT(P2)
	HRLZ	T,T3
	AND	T3,T
	JUMPN	T3,SEEKMS
	MOVE	T3,2(T4)
	MOVEM	T3,FNAMI	;STORE IN NAME BLOCK [AECL.002]
	XOR	T3,.FXNAM(P2)
	AND	T3,.FXNMM(P2)
	JUMPN	T3,SEEKMS
	LDB	T3,[POINT 3,3(T4),20]	;[40] 
	MOVE	T2,4(T4)	;CHECK /BEFORE AND /SINCE
	PUSHJ	P,CHKTM
	  JRST	[MOVE	T4,B.DC+1
		JRST	SEEKMS]
	MOVE	T4,B.DC+1
	TLNE	F,S.IGN		;CHECK PP?
	JRST	NMFND		;NO
	SKIPN	T3,.FXDIR(P2)	;PP GIVEN?
	MOVE	T3,USRPPN	;NO,GET USER'S PP
	CAMN	T3,5(T4)
	JRST	NMFND
SEEKMS:	ADDI	P2,ILSPEC
	CAMG	P2,XNXZR	;SEE IF AT END
	JRST	SEEKIS		;LOOP BACK
	HRRZ	T,1(T4)
	PUSHJ	P,FINISH
	SKIPN	T4,S.END	;LIMITED SEARCH?
	JRST	SEEKNM		;NO,KEEP LOOKING
	CAML	T,T4		;YES,HAVE WE REACHED LIMIT?
	JRST	EOS		;YES,GIVE UP
;HERE SEEK NAME ON INPUT MTFILE

SEEKNM:	MOVE 	P2,XINZR
	IN	ICHN,
	JRST	SEEKOK
	STATO	ICHN,IO.EOF
	JRST	IMESS1
EOS:	CLOSE	ICHN,CL.OUT
	MTBSF.	ICHN,
	TRNE	F,F.NEW
	OUTSTR	[ASCIZ /End of search range encountered
/]
	TDNE	F,[S.ALL,,F.STAR]
	JRST	ESTAR
NSUCH:	MOVEI	N,ISPEC
	HRRI	T2,-1
	M.FAIF	<NO FILE NAMED>
;FILE NAME CONTAINS NO LETTERS - MUST BE FILE NUMBER
NUMEND:	TRZ	F,F.STAR
	HRRZM	T2,NUMCHK
	PUSHJ	P,SEEKDI
	IN	ICHN,		;IN POSITION FOR READ
	SKIPA	T4,B.DC+1
	JRST	IMESS1
	PUSHJ	P,PLAUSI
	  JRST	IIMP
	HRRZ	T2,1(T4)	;GET FILE NUMBER
	CAME	T2,NUMCHK	;AS EXPECTED?
	JRST	IMESS1		;NO,CALL IT AN IO ERROR
NMFND:	TRZ	F,F.NEW
	LDB	T2,[POINT 9,4(T4),8];GET PROTECTION CODE
	HRLI	T2,5
	MOVE	T3,5(T4)
	MOVE	T4,USRPPN
IFN CHK,<
	MOVEI	T,T2
	CHKACC	T,
	  JRST	MANCHK		;UUO NOT IMPLEMENTED,MANUAL CHECK NEEDED
	JUMPE	T,PROTOK
	JRST	PRTERR
MANCHK:>
	CAMN	T4,FSFPPN
	JRST	PROTOK		;NO CHECK FOR PROTECTION VIOLATION
	XOR	T3,T4
	TRNN	T3,-1		;SAME PROGRAMMER?
	JRST	PROTOK		;YES,MUST HAVE ACCESS PRIVILEGES
	LDB	T,[POINT 4,T2,32];GET PROJECT PROTECTION
	TLNE	T3,-1		;IS USER A PROJECT MEMBER?
	LDB	T,[POINT 3,T2,35];NO,GET PUBLIC PROTECTION
	CAILE	T,5		;IS READ ALLOWED?
	JRST	PRTERR		;NO
PROTOK:	MOVE	T2,B.DC+1
	MOVSI	T3,2(T2)
	HRRI	T3,FNAMI
	BLT	T3,FNAMI+3
	HRRZ	T3,3(T2)	;DOES INPUT FILE HAVE RIB?
	JUMPE	T3,NORIBI
	MOVSI	T3,6(T2)
	HRRI	T3,ERIB
	MOVE	T4,6(T2)
	BLT	T3,ERIB-1(T4)
NORIBI:	TRO	F,F.FSTI
	HRRZ	T3,1(T2)	;CHECK WHETHER SEARCH RANGE IS EXHAUSTED
	CAMN	T3,S.END	;IN CASE OF *-TYPE SEARCH
	TRZ	F,F.STAR	;YES, INHIBIT FURTHER SEARCH
	JRST	LOOKOK
SUBTTL TYPEOUT ROUTINES
.TNEWL::
CROUT:	OUTSTR	[ASCIZ /
/]
	POPJ	P,
SPACE:	TDZA	T,T
DOT:	MOVEI	T,'.'
TYPE6:	ADDI	T," "		;SIXBIT TO ASCII
TYPE:	OUTCHR	T
CPOPJ:	POPJ	P,

TYIA:	CLRBFI
	MOVE	P,PDP
	JRST	TYIB

	PUSHJ	P,TYPE6
QTYPE:	MOVEI	T,0
	ROTC	T,6
	JUMPN	T,QTYPE-1
	POPJ	P,

TTYCHK:	SKPINC	T		;SEE IF WE CAN INPUT
	   JFCL			;BUT ONLY TO DEFEAT ^O
	POPJ	P,
SUBTTL ERRORS
OMESS:	SKIPA	N,[OSPEC]
IMESS:	MOVEI	N,ISPEC
	PUSHJ	P,TTYCHK
	MOVEI	T1,[ASCIZ /IO error on /]
	TRNE	E,IO.BKT
	MOVEI	T1,[ASCIZ /Block too large on /]
	TRNE	E,IO.DTE
	MOVEI	T1,[ASCIZ /Parity error on /]
	TRNE	E,IO.DER
	MOVEI	T1,[ASCIZ /Data missed on /]
	TRNE	E,IO.IMP
	MOVEI	T1,[ASCIZ /Write-lock error on /]
	TRNE	E,IO.EOT	;PHYSICAL EOT ON MTA  (OR PTY?)[AECL.008]
	MOVEI	T1,[ASCIZ  /physical EOT on /]	;yep[AECL.008]
	HRRI	T2,-1
	PJRST	.FMSGF##

NOTAVL:	PUSHJ	P,TTYCHK
	MOVE	T2,-2(T2)	;GET INSTRUCTION
	MOVE	N,1(T2)		;GET DEVICE NAME
	M.FAIN	<Can't INIT>
CLERR:	M.FAIL	<Will not clear input device>
ILLOUT:	M.FAIL	<Can't do directories to MTFILE-format devices>
ILLIN:	M.FAIL	<MTFILE-format input required>
ERRTMI:	M.FAIL	<Too many concatenations>
USEPIP:	M.FAIL	<Can't clear DSK, use PIP>
TTYERR:	M.FAIL	<Can't use same device for input and output>
MULG:	M.FAIL	<Only one input specification allowed with /G>
ILLFNO:	SETZM	FNAMO
	TRNN	F,F.DO		;DID WE REALLY NEED A FILENAME?
	JRST	SPEAK		;NO, MUST BE SPOOLED DEVICE
	M.FAIL	<Wild output name requires /X>
ILLWI:	M.FAIL	<Wild input name requires directory device>
IFN SFD,<
ILLPO:	M.FAIL	<Illegal or improper output PATH>>
ILLBEG:	M.FAIL	</BEGIN greater than /END>
NOOUT:	M.FAIL	<No output specification seen>
FNTFNE:	SKIPA	T2,ERIB+.RBEXT
FNTFND:	MOVE	T2,FNAMI+1
	MOVEI	N,ISPEC
	PUSHJ	P,TTYCHK
	M.FAIF	<LOOKUP failure>
FULL:	MOVE	T2,FNAMO+1
	PUSHJ	P,TTYCHK
	OUTSTR	[ASCIZ	/? ENTER failure /]
	HRRZ	T,T2
	PUSHJ	P,.TOCTW##
	PUSHJ	P,SPACE
	MOVE	T2,DEVO
	MOVEI	T3,FNAMO
	PUSHJ	P,FILERR
	TRNE	F,F.I
	PUSHJ	P,FINISH
	JRST	TYIA
PRTERR:	PUSHJ	P,TTYCHK
	OUTSTR	[ASCIZ	/? Protection violation /]
	MOVE	T2,DEVI
	MOVEI	T3,FNAMI
	PUSHJ	P,FILERR
	PUSHJ	P,FINISH
	JRST	TYIA
FILERR:	PUSHJ	P,QTYPE
	MOVEI	T,":"
	OUTCHR	T
	MOVE	T2,0(T3)
	PUSHJ	P,QTYPE
	SKIPN	T2,1(T3)
	PJRST	CROUT
	PUSHJ	P,DOT
	PUSHJ	P,QTYPE
	PJRST	CROUT
;CHECK THAT TAPE RECORD IS MTFILE-FORMAT
PLAUSI:	MOVE	T3,B.DC+1
	HRRZ	T,1(T3)
	JUMPLE	T,IMPLAU	;RECORD NO. .GT. 0
	HLLZ	T,1(T3)
	SKIPE	2(T3)		;FILNAM NONZERO
	SKIPLE	T		;-<WORD COUNT> .LE. 0
	JRST	IMPLAU
	MOVE	T,4(T3)		;PROT,MODE,TIME,DATE WORD
	SKIPE	5(T3)		;PPN NONZERO
	TLNN	T,(777B8)	;PROT NONZERO
	JRST	IMPLAU
	ANDI	T,7777		;DATE NOT IN THE FUTURE
	LDB	T3,[POINT 3,3(T3),20]	;[40] 
	DPB	T3,[POINT 3,T,23]	;[40] 
	CAMG	T,THSDAT
	JRST	CPOPJ1
	DATE	T3,		;(HOW LONG HAVE WE BEEN RUNNING?)
	CAMG	T,T3
	JRST	CPOPJ1
IMPLAU:	OUTSTR	[ASCIZ /? Illegal format file on /]
	POPJ	P,

PLASI1:	SKIPA	T3,B.DC+1
PLAUSO:	MOVE	T3,OBUFI+1
	HRRZ	T,1(T3)
	JUMPLE	T,IMPLAU
	HLLZ	T,1(T3)
	SKIPLE	T
	JRST	IMPLAU
CPOPJ1:	AOS	(P)
	POPJ	P,

OIMP:	PUSHJ	P,FINOSH
	MTBSF.	OCHN,
	MTBSF.	OCHN,
	STATO	OCHN,IO.BOT
	MTSKF.	OCHN,
	MOVE	T2,DEVO
	JRST	ILLFX
IIMP:	PUSHJ	P,FINISH
	MTBSF.	ICHN,
	MTBSF.	ICHN,
	STATO	ICHN,IO.BOT
	MTSKF.	ICHN,
	MOVE	T2,DEVI
ILLFX:	PUSHJ	P,QTYPE
	PUSHJ	P,CROUT
	JRST	TYIA
SUBTTL GARBAGE COLLECTION
GC:	MOVE	W,.JBFF
;SCAN SOURCE TAPE AND BUILD A TABLE OF READABLE FILES
	MTWAT.	ICHN,		;WAIT HERE FOR REWIND TO FINISH
	TDZA	CH,CH		;GREATEST FILE NUMBER SO FAR
GC1:	SETSTS	ICHN,10		;TURN OFF EOF AND ERROR BITS
	IN	ICHN,
	JRST	GC4		;GO PROCESS FILE
	STATZ	ICHN,IO.EOF	;TWO SUCCESSIVE EOFS ARE LOGICAL EOT
	JRST	GC6		;GO ANALYZE TABLE
GC3:	STATZ	ICHN,IO.EOF	;(IO ERROR) SKIP TO END OF FILE
	JRST	GCSKP
	INPUT	ICHN,
	JRST	.-3

GC4:	MOVEI	T3,3(W)
	CAMLE	T3,.JBREL	;ROOM FOR ANOTHER FILE ENTRY?
	JRST	[CORE	T3,
		JRST	CORERR
		JRST	GC4+3]
	MOVE	T3,B.DC+1
	MOVSI	J,2(T3)		;COPY FILE BLOCK FROM INPUT BUFFER
	HRR	J,W
	BLT	J,3(W)
	IN	ICHN,
	JRST	.-1
	STATZ	ICHN,IO.EOF
	JRST	GCSKP
	SETZM	0(W)		;I/O ERROR,MARK FILE AS UNREADABLE
	JRST	GC3
GCSKP:	CLOSE	ICHN,CL.OUT
	ADDI	W,4
	AOJA	CH,GC1
;ANALYZE TABLE AND TYPE OUT RESULTS
GC6:	CLOSE	ICHN,
	MTREW.	ICHN,
	OUTSTR	[ASCIZ /Largest file number is /]
	MOVE	T,CH
	PUSHJ	P,.TDECW##
	PUSHJ	P,CROUT
	JUMPE	CH,GFIN
	MOVEI	H,1
	MOVE	W,.JBFF
GTBL1:	SKIPN	0(W)

	PUSHJ	P,GMISS
	ADDI	W,4
	CAMGE	H,CH
	AOJA	H,GTBL1
	TRNN	F,F.ERR
	OUTSTR	[ASCIZ /No input errors on pass one/]
	PUSHJ	P,CROUT

;GARBAGE COLLECT ON INTERNAL TABLE
	MOVE	G,.JBFF		;ADDRESS OF FIRST ENTRY
	MOVE	H,CH
	ASH	H,2
	ADDI	H,-4(G)
GC7:	SKIPN	I,(H)
	JRST	GC8
	CAMN	I,(G)		;FILE NAMES MATCH?
	JRST	GMATCH		;YES, BUT COMPONENTS MUST MATCH
GC9:	ADDI	G,4
	CAMGE	G,H
	JRST	.-4
GC8:	SUBI	H,4
	MOVE	G,.JBFF
	CAMLE	H,G
	JRST	GC7
	MOVN	E,CH
	HRLZS	E
	HRR	E,.JBFF
	HRRZM	E,LAST
	MTWAT.	ICHN,		;WAIT HERE FOR REWIND TO FINISH
GTBL2:	SKIPE	0(E)
	PUSHJ	P,GCOPY		;COPY CURRENT FILE
	ADDI	E,3		;FIND ANOTHER FILE TO COPY
	AOBJN	E,GTBL2
GFIN:	MTREW.	ICHN,
	JRST	TYIB

GMATCH:	HLLZ	J,1(G)		;KEEP A FILE UNLESS NAME.EXT[PRJ,PRG]
	XOR	J,1(H)		;EXISTS IN A LATER VERSION
	TLNE	J,-1
	JRST	GC9
	MOVE	J,3(G)
	CAME	J,3(H)
	JRST	GC9
	LDB	I,[POINT 12,2(G),35];FILES MATCH, WHICH ONE DIES?
	LDB	J,[POINT 3,1(G),20]	;[40] 
	DPB	J,[POINT 3,I,23]	;[40] 
	PUSH	P,I			;[40] 
	LDB	J,[POINT 12,2(H),35]
	LDB	I,[POINT 3,1(H),20]	;[40] 
	DPB	I,[POINT 3,J,23]	;[40] 
	POP	P,I			;[40] 
	CAMLE	I,J
	JRST	KILLH
	CAME	I,J
	JRST	KILLG
	LDB	I,[POINT 11,2(G),23];DATES MATCH, CHECK TIMES
	LDB	J,[POINT 11,2(H),23]
	CAMLE	I,J
	JRST	KILLH
KILLG:	SETZM	0(G)		;IF TIME AND DATE BOTH MATCH,
	JRST	GC7		;KEEP HIGHER-NUMBERED FILE
KILLH:	SETZM	0(H)
	JRST	GC8

CORERR:	OUTSTR	[ASCIZ /More core required, type CONT to continue
/]
	EXIT	1,
	JRST	GC4
;COPY FROM ONE FILE DEVICE TO ANOTHER
GCOPY:	PUSH	P,E
	TRO	F,F.FSTI
	HRRZ	T2,E
	SUBM	T2,LAST
	ADDI	T2,4
	EXCH	T2,LAST
	ASH	T2,-2		;SKIP-FILE OVER CHAFF
	JUMPE	T2,.+3
	MTSKF.	ICHN,
	SOJG	T2,.-1
	PUSHJ	P,INP
	MOVE	E,B.DC+1	;BLT LOOKUP BLOCK INTO PLACE
	MOVSI	T2,2(E)
	HRRI	T2,FNAMO
	BLT	T2,FNAMO+3
	HRRZ	T2,3(E)		;CHECK INPUT FILE FOR RIB
	JUMPE	T2,GNRIB
	MOVSI	T2,6(E)		;BLT RIB INTO PLACE
	HRRI	T2,ERIB
	MOVE	N,6(E)
	BLT	T2,ERIB-1(N)
GNRIB:	PUSHJ	P,SEEKW		;GET OUTPUT DEVICE INTO POSITION
	PUSHJ	P,PROCG
	POP	P,E		;FILE HAS BEEN COPIED,RESUME SCAN
	POPJ	P,

GMISS:	MOVEI	T,","
	TRNE	F,F.ERR
	OUTCHR	T
	TRON	F,F.ERR
	OUTSTR	[ASCIZ	/Missing or unreadable files: /]
	MOVE	T,H
	PJRST	.TDECW##
DEFINE	SWTCHS <
SS	ALL,<POINT 1,F,^L<S.ALL>-^D18>,1
SS	0ALL,<POINT 1,F,^L<S.ALL>-^D18>,0
SP	BEGIN,<POINT 35,S.BEG,35>,.SWDEC##
SP	END,<POINT 35,S.END,35>,.SWDEC##
SS	FAST,<POINT 1,F,^L<S.FAST>-^D18>,1
SS	GARBAGE,<POINT 1,F,^L<S.G>-^D18>,1
SS	IGNORE,<POINT 1,F,^L<S.IGN>-^D18>,1
SS	0IGNORE,<POINT 1,F,^L<S.IGN>-^D18>,0
SS	LIST,<POINT 1,F,^L<S.LIST>-^D18>,1
SL	MTAPE,<POINT 35,MTOP,35>,MTOP,0
SS	NORMAL,<POINT 1,F,^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