	.title	openf
	.list	meb
;
;
;	routine to open software tools files
;
;	call sequence
;		status = openf(lun, dsc, typ, cc, acc, age, fdb)
;
;		where:
;			lun	logical unit to use for file

;			dsc	data set descriptor for file
;			typ	type of file (0=character, 1=binary)
;				this field is currently ignored
;			cc	carriage control (0=none, 1=fortran, 2=list)
;			acc	type of access desired
;				(1=read, 2=write, 3=readwrite, 4=append)
;			age	age of file (-1=old, 0=unknown, 1=new)
;			fdb	variable to return FDB address to upon
;				successful open.  this address is then
;				used in all other ST io primitives
;
	.mcall	fdat$r, fdof$l, fdop$r, open$, fcsbt$, nmblk$
	fdof$l
	fcsbt$
	.globl	gtfdb
;
;	local constants
;
ap=%5				; argument pointer
lun=2				; offset from ap for lun info
dsc=4
typ=6
cc=10
acc=12
age=14
fdb=16
read=1				; value of READ
write=2
readwr=3
append=4
err=-3				; value of ERR
charac=0
binary=1
;
;	local storage
;
dfnb:	nmblk$	,,0,SY,0
;
; start of code
;
	.enabl	lsb
openf::
	mov	@lun(ap),r2		; place lun in r2 for gtfdb
	call	gtfdb			; get a free FDB
	tst	r0
	bne	10$			; if != 0, were successful
	jmp	50$			; no more FDB's
10$:
	fdat$r	,#r.var,@cc(ap)		; only support sequential files
	fdop$r	,@lun(ap),dsc(ap),#dfnb,,#fa.enb!fa.dlk	; user lun, data-set
					; descriptor and default FNB
	cmp	#read,@acc(ap)		; see if opening at read access
	bne	20$
	open$	,#fo.rd!fa.shr		; open existing file at read access
					; permit file to be shared
	bcc	openok			; c clear => success
	jmp	operr
20$:
	cmp	#append,@acc(ap)	; see if opening at append access
	bne	30$
	open$	,#fo.apd
	bcs	create			; error, go create new file
	br	openok
30$:
	tst	@age(ap)		; see what kind of open to do
	bgt	create			; if > 0, then wants new file
	open$	,#fo.upd		; try to open existing file
	bcc	openok
create:
	open$	,#fo.wrt		; open new file at write access
	bcs	operr
openok:
	mov	r0,@fdb(ap)		; return FDB address to user
	tstb	f.ratt(r0)		; does file have record attributes?
	beq	notch			; if not, then not character file
	cmpb	f.rtyp(r0),#r.var	; is it variable length records?
	bne	notch			; if not, then not character file
	mov	#charac, r0		; return character file as value
	return
notch:
	mov	#binary, r0		; return binary file
	return
operr:
50$:
	mov	#err,r0			; return error status
	return
	.end
