	.enabl	lsb
	.title	rwgter	Rewrite, get and erase records
	.ident	"V02.00"
;+
; Index		Rewrite a record
; Index		Get a record
; Index		Erase a record
;
; Usage
;
;	rewrit(stream, buffer, size, key, keysize, options)
;	int stream;      /* Stream number from open/create */
;	char *buffer;    /* Buffer containing updated info */
;	int size;        /* Size of the updated buffer */
;	char *key;       /* Key to search on */
;	int keysize;     /* Size of key buffer */
;	char *options;   /* String of options */
;
;
;	rget(stream, buffer, size, key, keysize, options [, kor)
;	int stream;      /* Stream number from open/create */
;	char *buffer;    /* Buffer to copy record into */
;	int size;        /* Maximum number of bytes to read */
;	char *key;       /* Key to search for */
;	int keysize;     /* Size of key buffer */
;	char *options;   /* String of options */
;	int kor;	 /* New key-of-reference number */
;			 /* (if "s" option was given) */
;
;
;	rerase(stream, buffer, size, key, keysize, options)
;	int stream;      /* Stream number from open/create */
;	char *buffer;    /* Record to delete */
;	int size;        /* Size of the buffer */
;	char *key;       /* Key to search on */
;	int keysize;     /* Size of key buffer */
;	char *options;   /* String of options */
;
;
; Description
;
;	rewrit() updates an existing record. It is valid only for
;	relative and indexed files. It returns a -1 on error and a 1
;	on success. The following option is available:
;
; 		Options:
;	     	   w - wait on locked record
;
;	rget() reads the contents of an existing record into the user
;	buffer. It returns -1 on error or EOF and the actual size of
;	the record on success. The following options are available:
;
; 		Options:
;	     	   l - lock record on read
;		   w - wait on locked record
;		   s - set new key of reference (ISAM only)
;
;	rerase() erases an existing record. It is valid only for binary
;	sequential, relative and indexed files. It returns -1 on error
;	and 1 on success. The following option is available:
;
; 		Options:
;	           w - wait on locked record
;-
; Edit History
; V02.00 19-Oct-82 TTC		Rework of old CS library
;

	.MCALL	FSDEF$
	FSDEF$

	.psect	c$strn
;
; Options.
;
wait:	.asciz	"w"
lock:	.asciz	"l"
skr:	.asciz	"s"
	.even

	.psect	c$data
;
; Table of options for get
;
getopt:	.word	lock,	O.LOK
	.word	wait,	O.WAI
	.word	skr,	O.SKR  	; *** Note if this changes look out below
	.word	0

;
; Table of options for rewrite and erase
;
reropt:	.word	wait,	O.WAI
	.word	0

	.psect	c$code

rewrit::
	mov	#FS.UPD,fsqio+q.iofn	; Function: FS.UPD
	mov	#reropt,r1		; r1 --> rewrite option table
	br	10$			; Branch to common code

rerase::
	mov	#FS.ERA,fsqio+q.iofn	; Function: FS.ERA
	mov	#reropt,r1		; r1 --> erase option table
	br	10$			; Branch to common code

rget::
	mov	#FS.GET,fsqio+q.iofn	; Function: FS.GET
	mov	#getopt,r1		; r1 --> get option table

10$:	mov	14(sp),r0		; r0 --> list of passed options
	call	option			; check options
	tst	r0			; Error?
	beq	30$			; (Yes)
	bit	#O.SKR,r1		; SKR? (*** HARDWIRED ***)
	beq	15$			; (No)
	swab	r1
	bisb	16(sp),r1		; (Yes), pit KOR number into hi byte
	swab	r1
15$:	mov	r1,-(sp)		; Save option word
	mov	sp,r1			; r1 --> passed parameters
	add	#4,r1
	mov	#fsqio+q.iopl,r0	; r0 --> parameter list
	mov	(r1)+,(r0)+		; Parameter #1: Stream Number
	mov	(r1)+,(r0)+		; Parameter #2: Buffer address
	mov	(r1)+,(r0)+		; Parameter #3: Buffer size
	mov	(r1)+,(r0)+		; Parameter #4: Key buffer addr
	mov	(r1),(r0)+		; Parameter #5: Key size
	mov	(sp)+,(r0)		; Parameter #6: Inclu-or of opts
	mov	#fsqio,r0		; r0 must = qio for xqiow
	call	.xqiow			; execute qiow
	cmp	#FS.GET,fsqio+q.iofn	; Call to get?
	bne	20$			; (No)
	mov	f.iosb+2,r0		; (Yes), so r0 = record length
	br	25$
20$:	mov	#1,r0			; Assume success
25$:	cmp	$dsw,#is.suc		; How'd things go
	blo	30$
	cmpb	f.iosb,#is.suc
	beq	40$
30$:	mov	#-1,r0			; Error or EOF -- r0 = -1
40$:	return
	.end
