Commit | Line | Data |
---|---|---|
bae7117f WH |
1 | #include "f2c.h" |
2 | #include "fio.h" | |
3 | #ifdef KR_headers | |
4 | integer f_back(a) alist *a; | |
5 | #else | |
6 | integer f_back(alist *a) | |
7 | #endif | |
8 | { unit *b; | |
9 | int i, n, ndec; | |
10 | #ifdef MSDOS | |
11 | int j, k; | |
12 | long w, z; | |
13 | #endif | |
14 | long x, y; | |
15 | char buf[32]; | |
16 | if(a->aunit >= MXUNIT || a->aunit < 0) | |
17 | err(a->aerr,101,"backspace") | |
18 | b= &f__units[a->aunit]; | |
19 | if(b->useek==0) err(a->aerr,106,"backspace") | |
20 | if(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(b->ufd); | |
36 | y = x % b->url; | |
37 | if(y == 0) x--; | |
38 | x /= b->url; | |
39 | x *= b->url; | |
40 | (void) fseek(b->ufd,x,SEEK_SET); | |
41 | return(0); | |
42 | } | |
43 | ||
44 | if(b->ufmt==0) | |
45 | { (void) fseek(b->ufd,-(long)sizeof(int),SEEK_CUR); | |
46 | (void) fread((char *)&n,sizeof(int),1,b->ufd); | |
47 | (void) fseek(b->ufd,-(long)n-2*sizeof(int),SEEK_CUR); | |
48 | return(0); | |
49 | } | |
50 | #ifdef MSDOS | |
51 | w = -1; | |
52 | #endif | |
53 | for(ndec = 2;; ndec = 1) | |
54 | { | |
55 | y = x=ftell(b->ufd); | |
56 | if(x<sizeof(buf)) x=0; | |
57 | else x -= sizeof(buf); | |
58 | (void) fseek(b->ufd,x,SEEK_SET); | |
59 | n=fread(buf,1,(int)(y-x), b->ufd); | |
60 | for(i=n-ndec;i>=0;i--) | |
61 | { | |
62 | if(buf[i]!='\n') continue; | |
63 | #ifdef MSDOS | |
64 | for(j = k = 0; j <= i; j++) | |
65 | if (buf[j] == '\n') | |
66 | k++; | |
67 | fseek(b->ufd,x,SEEK_SET); | |
68 | do { | |
69 | if (getc(b->ufd) == '\n') { | |
70 | --k; | |
71 | if ((z = ftell(b->ufd)) >= y) { | |
72 | if (w == -1) | |
73 | goto break2; | |
74 | break; | |
75 | } | |
76 | w = z; | |
77 | } | |
78 | } while(k > 0); | |
79 | fseek(b->ufd, w, SEEK_SET); | |
80 | #else | |
81 | fseek(b->ufd,(long)(i+1-n),SEEK_CUR); | |
82 | #endif | |
83 | return(0); | |
84 | } | |
85 | #ifdef MSDOS | |
86 | break2: | |
87 | #endif | |
88 | if(x==0) | |
89 | { | |
90 | (void) fseek(b->ufd, 0L, SEEK_SET); | |
91 | return(0); | |
92 | } | |
93 | else if(n<=0) err(a->aerr,(EOF),"backspace") | |
94 | (void) fseek(b->ufd, x, SEEK_SET); | |
95 | } | |
96 | } |