Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #pragma ident "@(#) libf/fio/unitbksp.c 92.1 06/18/99 18:38:26"
00039
00040 #include <errno.h>
00041 #include <foreign.h>
00042 #include <liberrno.h>
00043 #include "fio.h"
00044 #ifdef __mips
00045 typedef long long _ftelltype;
00046 #define LIBFSEEK fseek64
00047 #define LIBFTELL ftell64
00048 #define FTELLZERO 0LL
00049 #else
00050 typedef long _ftelltype;
00051 #define LIBFSEEK fseek
00052 #define LIBFTELL ftell
00053 #define FTELLZERO 0L
00054 #endif
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 int
00069 _unit_bksp(unit *cup)
00070 {
00071 int errn;
00072 unsigned char tbuf[TBUFSZB], *tp;
00073 struct ffsw fst;
00074 int first_read;
00075
00076 if (cup->useq == 0)
00077 return(FEBKSPIV);
00078
00079
00080
00081
00082
00083 WAITIO(cup, return(cup->uffsw.sw_error));
00084
00085 if (cup->uwrt) {
00086 if (cup->utrunc) {
00087
00088
00089
00090
00091 errn = _unit_trunc(cup);
00092 if (errn != 0)
00093 return(errn);
00094 }
00095 cup->uwrt = 0;
00096 }
00097
00098
00099
00100
00101
00102 switch( cup->ufs ) {
00103
00104 case FS_FDC:
00105 if ((cup->uend != LOGICAL_ENDFILE) || (cup->uspcproc==1)) {
00106
00107 errn = XRCALL(cup->ufp.fdc, backrtn) cup->ufp.fdc,
00108 &fst);
00109
00110 if (errn < 0)
00111 return(fst.sw_error);
00112 }
00113 break;
00114
00115
00116 case FS_TEXT:
00117 case STD:
00118
00119
00120
00121 if (!cup->useek)
00122 return(FENOBKPI);
00123
00124 if (!cup->ufmt)
00125 return(FENOBKSP);
00126
00127 if (cup->uend) goto ok;
00128
00129 first_read = 1;
00130
00131 for ( ; ; ) {
00132 ssize_t i;
00133 size_t ret;
00134 _ftelltype x, y;
00135
00136 y = LIBFTELL(cup->ufp.std);
00137 x = y;
00138
00139 if (x < TBUFSZB)
00140 x = 0;
00141 else
00142 x -= TBUFSZB;
00143
00144 (void) LIBFSEEK(cup->ufp.std, x, 0);
00145
00146
00147
00148
00149
00150
00151
00152 ret = fread(tbuf, 1,(size_t)(y - x), cup->ufp.std);
00153
00154 if (ret < (y-x) && ferror(cup->ufp.std))
00155 return(FEBSPNRD);
00156
00157
00158
00159
00160 if (first_read) i = ret - 2;
00161 else i = ret;
00162 first_read = 0;
00163
00164 tp = tbuf;
00165
00166 for ( ; i >= 0; i--) {
00167 if ( *(tp+i) != '\n') continue;
00168 (void) LIBFSEEK(cup->ufp.std, (_ftelltype)i+1-ret, 1);
00169 goto ok;
00170 }
00171
00172 if (x == 0) {
00173 (void) LIBFSEEK(cup->ufp.std, FTELLZERO, 0);
00174 goto ok;
00175 }
00176 else
00177 if (ret <= 0)
00178 return(FEINTUNK);
00179
00180 (void) LIBFSEEK(cup->ufp.std, x, 0);
00181 }
00182
00183 case FS_BIN:
00184 return(FENOBKSP);
00185
00186
00187 case FS_AUX:
00188 errn = FEMIXAUX;
00189
00190 break;
00191 default:
00192 return(FEINTFST);
00193 }
00194
00195 ok:
00196 cup->uend = BEFORE_ENDFILE;
00197 cup->ulastyp = DT_NONE;
00198 cup->urecpos = 0;
00199
00200 return(0);
00201 }