xref: /original-bsd/usr.bin/f77/libI77/backspace.c (revision 2301fdfb)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)backspace.c	5.2	07/30/85
7  */
8 
9 /*
10  * Backspace records
11  */
12 
13 #include "fio.h"
14 
15 static char	bksp[]	= "backspace";
16 char	last_char();
17 
18 f_back(a)
19 alist	*a;
20 {	unit *b;
21 	int n,i;
22 	long x,y;
23 
24 	lfname = NULL;
25 	elist = NO;
26 	external = YES;
27 	errflag = a->aerr;
28 	lunit = a->aunit;
29 	if (not_legal(lunit))
30 		err(errflag, F_ERUNIT, bksp)
31 	b= &units[lunit];
32 	if(!b->ufd) return(OK);
33 	lfname = b->ufnm;
34 	if(b->uend)
35 	{	b->uend = NO;
36 		clearerr(b->ufd);
37 		return(OK);
38 	}
39 	if((x = ftell(b->ufd)) == 0)
40 		return(OK);
41 	if(!b->useek)
42 		err(errflag, F_ERNOBKSP, bksp)
43 	if(b->uwrt && (n = t_runc(b, errflag, bksp)))	/* sets 'reading' */
44 		return(n);
45 	if(b->url)		/* direct access, purely academic */
46 	{	y = x%(long)b->url;
47 		x -= y?y:b->url;
48 		fseek(b->ufd,x,0);
49 		return(OK);
50 	}
51 	if(!b->ufmt)		/* unformatted sequential */
52 	{	fseek(b->ufd,-(long)sizeof(int),1);
53 		fread((char *)&n,sizeof(int),1,b->ufd);
54 		fseek(b->ufd,-(long)n-2*sizeof(int),1);
55 		return(OK);
56 	}
57 	if(x == 1)			/* formatted sequential */
58 	{	rewind(b->ufd);
59 		return(OK);
60 	}
61 	while (last_char(b->ufd) != '\n')	/* slow but simple */
62 		;
63 	return(OK);
64 }
65