xref: /csrg-svn/usr.bin/f77/libI77/sue.c (revision 23090)
12502Sdlw /*
2*23090Skre  * Copyright (c) 1980 Regents of the University of California.
3*23090Skre  * All rights reserved.  The Berkeley software License Agreement
4*23090Skre  * specifies the terms and conditions for redistribution.
52502Sdlw  *
6*23090Skre  *	@(#)sue.c	5.1	06/07/85
7*23090Skre  */
8*23090Skre 
9*23090Skre /*
102502Sdlw  * sequential unformatted external read/write routines
112502Sdlw  */
122502Sdlw 
132502Sdlw #include "fio.h"
142502Sdlw 
152502Sdlw extern int reclen;
1620984Slibs LOCAL long recloc;
1720984Slibs LOCAL char rsue[] = "read sue";
1820984Slibs LOCAL char wsue[] = "write sue";
192502Sdlw 
202502Sdlw s_rsue(a) cilist *a;
212502Sdlw {
222502Sdlw 	int n;
232502Sdlw 	reading = YES;
242502Sdlw 	if(n=c_sue(a,READ)) return(n);
254116Sdlw 	if(curunit->uwrt && ! nowreading(curunit)) err(errflag, errno, rsue)
262502Sdlw 	recpos = 0;
272502Sdlw 	if(fread(&reclen,sizeof(int),1,cf) == 1) return(OK);
282502Sdlw 	if(feof(cf))
292502Sdlw 	{	curunit->uend = YES;
302502Sdlw 		err(endflag, EOF, rsue)
312502Sdlw 	}
322502Sdlw 	clearerr(cf);
332502Sdlw 	err(errflag, errno, rsue)
342502Sdlw }
352502Sdlw 
362502Sdlw s_wsue(a) cilist *a;
372502Sdlw {
382502Sdlw 	int n;
392502Sdlw 	reading = NO;
402502Sdlw 	if(n=c_sue(a,WRITE)) return(n);
414116Sdlw 	if(!curunit->uwrt && ! nowwriting(curunit)) err(errflag, errno, wsue)
422502Sdlw 	reclen = 0;
432502Sdlw 	recloc=ftell(cf);
442502Sdlw 	fseek(cf,(long)sizeof(int),1);
452502Sdlw 	curunit->uend = NO;
462502Sdlw 	return(OK);
472502Sdlw }
482502Sdlw 
4920984Slibs LOCAL
502502Sdlw c_sue(a,flag) cilist *a;
512502Sdlw {	int n;
522502Sdlw 	external = sequential = YES;
532502Sdlw 	formatted = NO;
542502Sdlw 	lfname = NULL;
552502Sdlw 	elist = NO;
562502Sdlw 	errflag = a->cierr;
572502Sdlw 	endflag = a->ciend;
582502Sdlw 	lunit = a->ciunit;
594116Sdlw 	if(not_legal(lunit)) err(errflag,F_ERUNIT,rsue+5)
602502Sdlw 	curunit = &units[lunit];
612502Sdlw 	if(!curunit->ufd && (n=fk_open(flag,SEQ,UNF,(ftnint)lunit)))
624116Sdlw 		err(errflag,n,rsue+5)
632502Sdlw 	cf = curunit->ufd;
642502Sdlw 	elist = YES;
652502Sdlw 	lfname = curunit->ufnm;
664116Sdlw 	if(curunit->ufmt) err(errflag,F_ERNOUIO,rsue+5)
674116Sdlw 	if(curunit->url) err(errflag,F_ERNOSIO,rsue+5)
684116Sdlw 	if(!curunit->useek) err(errflag,F_ERSEEK,rsue+5)
692502Sdlw 	return(OK);
702502Sdlw }
712502Sdlw 
722502Sdlw e_wsue()
732502Sdlw {	long loc;
742502Sdlw 	fwrite(&reclen,sizeof(int),1,cf);
752502Sdlw 	loc=ftell(cf);
762502Sdlw 	fseek(cf,recloc,0);
772502Sdlw 	fwrite(&reclen,sizeof(int),1,cf);
782502Sdlw 	fseek(cf,loc,0);
792502Sdlw 	return(OK);
802502Sdlw }
812502Sdlw 
822502Sdlw e_rsue()
832502Sdlw {
842502Sdlw 	fseek(cf,(long)(reclen-recpos+sizeof(int)),1);
852502Sdlw 	return(OK);
862502Sdlw }
87