Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
backspace.c
Go to the documentation of this file.00001 #include "f2c.h"
00002 #include "fio.h"
00003 #ifdef KR_headers
00004 integer f_back(a) alist *a;
00005 #else
00006 integer f_back(alist *a)
00007 #endif
00008 { unit *b;
00009 int i, n, ndec;
00010 #ifdef MSDOS
00011 int j, k;
00012 long w, z;
00013 #endif
00014 long x, y;
00015 char buf[32];
00016 if(a->aunit >= MXUNIT || a->aunit < 0)
00017 err(a->aerr,101,"backspace")
00018 b= &f__units[a->aunit];
00019 if(b->useek==0) err(a->aerr,106,"backspace")
00020 if(b->ufd==NULL) {
00021 fk_open(1, 1, a->aunit);
00022 return(0);
00023 }
00024 if(b->uend==1)
00025 { b->uend=0;
00026 return(0);
00027 }
00028 if(b->uwrt) {
00029 (void) t_runc(a);
00030 if (f__nowreading(b))
00031 err(a->aerr,errno,"backspace")
00032 }
00033 if(b->url>0)
00034 {
00035 x=ftell(b->ufd);
00036 y = x % b->url;
00037 if(y == 0) x--;
00038 x /= b->url;
00039 x *= b->url;
00040 (void) fseek(b->ufd,x,SEEK_SET);
00041 return(0);
00042 }
00043
00044 if(b->ufmt==0)
00045 { (void) fseek(b->ufd,-(long)sizeof(int),SEEK_CUR);
00046 (void) fread((char *)&n,sizeof(int),1,b->ufd);
00047 (void) fseek(b->ufd,-(long)n-2*sizeof(int),SEEK_CUR);
00048 return(0);
00049 }
00050 #ifdef MSDOS
00051 w = -1;
00052 #endif
00053 for(ndec = 1;; ndec = 0)
00054 {
00055 y = x = ftell(b->ufd);
00056 if(x < sizeof(buf))
00057 x = 0;
00058 else
00059 x -= sizeof(buf);
00060 (void) fseek(b->ufd,x,SEEK_SET);
00061 n=fread(buf,1,(int)(y-x), b->ufd);
00062 for(i = n - ndec; --i >= 0; )
00063 {
00064 if(buf[i]!='\n') continue;
00065 #ifdef MSDOS
00066 for(j = k = 0; j <= i; j++)
00067 if (buf[j] == '\n')
00068 k++;
00069 fseek(b->ufd,x,SEEK_SET);
00070 for(;;)
00071 if (getc(b->ufd) == '\n') {
00072 if ((z = ftell(b->ufd)) >= y && ndec) {
00073 if (w == -1)
00074 goto break2;
00075 break;
00076 }
00077 if (--k <= 0)
00078 return 0;
00079 w = z;
00080 }
00081 fseek(b->ufd, w, SEEK_SET);
00082 #else
00083 fseek(b->ufd,(long)(i+1-n),SEEK_CUR);
00084 #endif
00085 return(0);
00086 }
00087 #ifdef MSDOS
00088 break2:
00089 #endif
00090 if(x==0)
00091 {
00092 (void) fseek(b->ufd, 0L, SEEK_SET);
00093 return(0);
00094 }
00095 else if(n<=0) err(a->aerr,(EOF),"backspace")
00096 (void) fseek(b->ufd, x, SEEK_SET);
00097 }
00098 }