xref: /csrg-svn/usr.bin/f77/libI77/backspace.c (revision 24094)
12484Sdlw /*
223063Skre  * Copyright (c) 1980 Regents of the University of California.
323063Skre  * All rights reserved.  The Berkeley software License Agreement
423063Skre  * specifies the terms and conditions for redistribution.
52484Sdlw  *
6*24094Sjerry  *	@(#)backspace.c	5.2	07/30/85
723063Skre  */
823063Skre 
923063Skre /*
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];
32*24094Sjerry 	if(!b->ufd) return(OK);
332484Sdlw 	lfname = b->ufnm;
342484Sdlw 	if(b->uend)
352484Sdlw 	{	b->uend = NO;
366600Sdlw 		clearerr(b->ufd);
372484Sdlw 		return(OK);
382484Sdlw 	}
3910674Sdlw 	if((x = ftell(b->ufd)) == 0)
4010674Sdlw 		return(OK);
4110674Sdlw 	if(!b->useek)
4210674Sdlw 		err(errflag, F_ERNOBKSP, bksp)
4314824Sdlw 	if(b->uwrt && (n = t_runc(b, errflag, bksp)))	/* sets 'reading' */
4410674Sdlw 		return(n);
452484Sdlw 	if(b->url)		/* direct access, purely academic */
462484Sdlw 	{	y = x%(long)b->url;
472484Sdlw 		x -= y?y:b->url;
482484Sdlw 		fseek(b->ufd,x,0);
492484Sdlw 		return(OK);
502484Sdlw 	}
512484Sdlw 	if(!b->ufmt)		/* unformatted sequential */
522484Sdlw 	{	fseek(b->ufd,-(long)sizeof(int),1);
532484Sdlw 		fread((char *)&n,sizeof(int),1,b->ufd);
542484Sdlw 		fseek(b->ufd,-(long)n-2*sizeof(int),1);
552484Sdlw 		return(OK);
562484Sdlw 	}
5710674Sdlw 	if(x == 1)			/* formatted sequential */
582484Sdlw 	{	rewind(b->ufd);
592484Sdlw 		return(OK);
602484Sdlw 	}
6110674Sdlw 	while (last_char(b->ufd) != '\n')	/* slow but simple */
6210674Sdlw 		;
632484Sdlw 	return(OK);
642484Sdlw }
65