	!    Integer function NEXTREC
	!    Returns the relative record number of the last
	!    record in the file + 1.
	!    Applies to 1. Sequential files with fixed lengthed records
	!		2. Relative files with variable, fixed, or VFC records
	!
	!    Calling sequence:
	!
	!	NEXT_RECORD = NEXTREC( IFI_VARIABLE )
	!
	!	Where IFI_VARIABLE is the file variable associated
	!	with a file being processed by RMS Interface software.
	!	The file must be open.
	!	The results of this routine will not change as long as
	!	the file remains open if the file is sequential.
	!       The next record pointer is unpredictable upon return from
	!	this routine.
	!
	integer function nextrec ( ifi )
	implicit integer*4 (a - z)
	include 'rmslib:infodefs.for'
	include 'sysdefs:rmsdef.for'
	include 'sysdefs:fabdef.for'
	call rms_info ( ifi, rms__ebk,ebk,rms__ffb, ffb, rms__org_v,
     &		org,rms__rfm_v,rfm,rms__mrs,mrs,rms__fsz,fsz)
	nextrec=0
!	type *,'EBK ',ebk
!	type *,'FFB ',ffb
!	type *,'ORG ', org
!	type *,'RFM ',rfm
!	type *,'MRS ',mrs
!	type *,'fsz ',fsz
	bytecount = (ebk-1)*512 + ffb
	if ( org .eq. fab$c_idx)return
	if ( org .eq. fab$c_seq)then
	  ! It must be fixed length records
	  if ( .not. (rfm .eq. fab$c_fix) )return
	  nextrec = (bytecount / mrs) + 1
	  return
	endif
	if ( org .eq. fab$c_rel)then
	  if ( rfm .eq. fab$c_fix) then
	    nextrec = ( bytecount / (mrs+1) ) 
	  else if (rfm .eq. fab$c_var) then
	    nextrec = ( bytecount/ (mrs+3) ) 
	  else if (rfm .eq. fab$c_vfc) then
	    nextrec = ( bytecount/ (mrs+fsz+3) )  
	  endif
	  lastrec = nextrec 
	  ! Read backwards until BOF or record or empty cell
	  ! is found.
	  ! Store RAB options
	  call rms_info( ifi, rms__rop_m,rop)
	  call rms_rabdef( ifi_(ifi), rop_('nlk,nxr,rrl') )
	  do while (lastrec .gt. 0)
	   ioflag=rms_findkey(ifi, lastrec)
	   ! Scan backwards through non-existant records
	   if (ioflag .ne. rms$_ok_rnf)then  !no such record
	     nextrec = lastrec + 1
	     go to 17
	     endif
	   lastrec=lastrec-1
	  end do
	  nextrec=1
17	  call rms_rabdef( ifi_(ifi), rop_m_(rop) )
	endif
	return
	end
