	.title	sndarg
;+
;	integer function sndarg(buf, pid)
;-

	.if ndf	SRDRV
	.psect	$r.rod,con,ro,rel,lcl,d
accs:	.word	writ$r			; access for CREATE
seed:	.asciz	"arg"			; seed for fgenr8 call
	.even
	.psect	$$iob1,ovr,rw,rel,lcl,d
prot:	.blkw	1			; hold the current default file prot
file:	.blkb	40.			; space for arg file name
	.even
	.endc

	.psect	$r.roi,con,ro,rel,lcl,i
	.enabl	lsb
ap=%5
buf=2
pid=4
sndarg::

	.if df	SRDRV

	.mcall	qiow$s,alun$s
	mov	pid(ap),r0		; address of pid
	sub	#4,sp			; space for rad50 taskname
	mov	sp,r1			; address in r1
	call	r$rad5			; convert taskname to rad50
	mov	#err,r0			; assume error
	mov	(sp)+,r2		; low half in r2
	mov	(sp)+,r3		; high half in r3
	bcs	100$			; c set => error in conversion
	alun$s	r$endl,#"SR,#0		; assign lun to sr:
	bcs	90$			; c set => error
	mov	buf(ap),r1		; address of buffer
	clr	r0			; initialize count
10$:
	tstb	(r1)+			; EOS yet?
	beq	20$			; YES
	inc	r0			; increment count
	br	10$
20$:
	qiow$s	#io.wlb,r$endl,r$spef,,#r$iosb,,<buf(ap),r0,r2,r3,#0,#1>
	bcs	90$			; c set => error
	movb	r$iosb,r0		; copy return status
	br	100$
90$:
	mov	@#$dsw,r0		; return DSW
100$:

	.iff

	mov	ap,-(sp)		; save arg pointer
	mov	#file,-(sp)		; address of filename buffer
	mov	#seed,-(sp)		; address of seed
	mov	pid(ap),-(sp)		; address of pid
	mov	#3,-(sp)		; arg count
	mov	sp,ap			; arg pointer for fgenr8
	call	fgenr8			; call fgenr8(pid, seed, file)
	add	#8.,sp			; clean stack
	call	.rdffp			; get def file prot
	mov	r1,prot			; save it for later
	clr	r1			; [RWED,RWED,RWED,RWED]
	call	.wdffp			; ...
	mov	#accs,-(sp)		; access
	mov	#file,-(sp)		; file
	clr	-(sp)			; dummy arg count
	mov	sp,ap			; arg pointer for create
	call	create			; int = create(file, accs)
	add	#6,sp			; clean stack
	mov	(sp)+,ap		; restore arg pointer
	cmp	r0,#err			; ERROR?
	beq	50$			; YES
	call	r$lput			; lput(buf)
	call	r$clsf			; close the file
	mov	#is.suc,r0		; successful
	br	60$
50$:
	mov	#ie.eof,r0		; error
60$:
	mov	prot,r1			; restore def file prot
	call	.wdffp			; ...

	.endc

	return
	.end
