1da686dbb488e94caab44d7b61eb4bcd98e96c97
[gcc.git] / libf2c / libI77 / backspace.c
1 #include <sys/types.h>
2 #include "f2c.h"
3 #include "fio.h"
4 #ifdef KR_headers
5 integer f_back(a) alist *a;
6 #else
7 integer f_back(alist *a)
8 #endif
9 { unit *b;
10 long v, w, x, y, z;
11 uiolen n;
12 FILE *f;
13
14 f__curunit = b = &f__units[a->aunit]; /* curunit for error messages */
15 if (f__init & 2)
16 f__fatal (131, "I/O recursion");
17 if(a->aunit >= MXUNIT || a->aunit < 0)
18 err(a->aerr,101,"backspace");
19 if(b->useek==0) err(a->aerr,106,"backspace");
20 if((f = b->ufd) == NULL) {
21 fk_open(1, 1, a->aunit);
22 return(0);
23 }
24 if(b->uend==1)
25 { b->uend=0;
26 return(0);
27 }
28 if(b->uwrt) {
29 (void) t_runc(a);
30 if (f__nowreading(b))
31 err(a->aerr,errno,"backspace");
32 }
33 if(b->url>0)
34 {
35 x=ftell(f);
36 y = x % b->url;
37 if(y == 0) x--;
38 x /= b->url;
39 x *= b->url;
40 (void) fseek(f,x,SEEK_SET);
41 return(0);
42 }
43
44 if(b->ufmt==0)
45 { fseek(f,-(long)sizeof(uiolen),SEEK_CUR);
46 fread((char *)&n,sizeof(uiolen),1,f);
47 fseek(f,-(long)n-2*sizeof(uiolen),SEEK_CUR);
48 return(0);
49 }
50 w = x = ftell(f);
51 z = 0;
52 loop:
53 while(x) {
54 x -= x < 64 ? x : 64;
55 fseek(f,x,SEEK_SET);
56 for(y = x; y < w; y++) {
57 if (getc(f) != '\n')
58 continue;
59 v = ftell(f);
60 if (v == w) {
61 if (z)
62 goto break2;
63 goto loop;
64 }
65 z = v;
66 }
67 err(a->aerr,(EOF),"backspace");
68 }
69 break2:
70 fseek(f, z, SEEK_SET);
71 return 0;
72 }