xref: /csrg-svn/usr.bin/f77/libI77/backspace.c (revision 23063)
12484Sdlw /*
2*23063Skre  * Copyright (c) 1980 Regents of the University of California.
3*23063Skre  * All rights reserved.  The Berkeley software License Agreement
4*23063Skre  * specifies the terms and conditions for redistribution.
52484Sdlw  *
6*23063Skre  *	@(#)backspace.c	5.1	06/07/85
7*23063Skre  */
8*23063Skre 
9*23063Skre /*
102484Sdlw  * Backspace records
112484Sdlw  */
122484Sdlw 
132484Sdlw #include "fio.h"
142484Sdlw 
1510674Sdlw static char	bksp[]	= "backspace";
1610674Sdlw char	last_char();
172484Sdlw 
1810674Sdlw f_back(a)
1910674Sdlw alist	*a;
202484Sdlw {	unit *b;
212484Sdlw 	int n,i;
222484Sdlw 	long x,y;
234728Sdlw 
242484Sdlw 	lfname = NULL;
252484Sdlw 	elist = NO;
262484Sdlw 	external = YES;
272484Sdlw 	errflag = a->aerr;
282484Sdlw 	lunit = a->aunit;
2910674Sdlw 	if (not_legal(lunit))
3010674Sdlw 		err(errflag, F_ERUNIT, bksp)
312484Sdlw 	b= &units[lunit];
3210674Sdlw 	if(!b->ufd && (n = fk_open(READ, SEQ, FMT, (ftnint)lunit)) )
3310674Sdlw 		err(errflag, n, bksp)
342484Sdlw 	lfname = b->ufnm;
352484Sdlw 	if(b->uend)
362484Sdlw 	{	b->uend = NO;
376600Sdlw 		clearerr(b->ufd);
382484Sdlw 		return(OK);
392484Sdlw 	}
4010674Sdlw 	if((x = ftell(b->ufd)) == 0)
4110674Sdlw 		return(OK);
4210674Sdlw 	if(!b->useek)
4310674Sdlw 		err(errflag, F_ERNOBKSP, bksp)
4414824Sdlw 	if(b->uwrt && (n = t_runc(b, errflag, bksp)))	/* sets 'reading' */
4510674Sdlw 		return(n);
462484Sdlw 	if(b->url)		/* direct access, purely academic */
472484Sdlw 	{	y = x%(long)b->url;
482484Sdlw 		x -= y?y:b->url;
492484Sdlw 		fseek(b->ufd,x,0);
502484Sdlw 		return(OK);
512484Sdlw 	}
522484Sdlw 	if(!b->ufmt)		/* unformatted sequential */
532484Sdlw 	{	fseek(b->ufd,-(long)sizeof(int),1);
542484Sdlw 		fread((char *)&n,sizeof(int),1,b->ufd);
552484Sdlw 		fseek(b->ufd,-(long)n-2*sizeof(int),1);
562484Sdlw 		return(OK);
572484Sdlw 	}
5810674Sdlw 	if(x == 1)			/* formatted sequential */
592484Sdlw 	{	rewind(b->ufd);
602484Sdlw 		return(OK);
612484Sdlw 	}
6210674Sdlw 	while (last_char(b->ufd) != '\n')	/* slow but simple */
6310674Sdlw 		;
642484Sdlw 	return(OK);
652484Sdlw }
66