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
00039
00040
00041
00042 #pragma ident "@(#) libf/fio/unitbksp.c 92.1 06/18/99 18:38:26"
00043
00044 #include <errno.h>
00045 #include <foreign.h>
00046 #include <liberrno.h>
00047 #include "fio.h"
00048 #ifdef __mips
00049 typedef long long _ftelltype;
00050 #define LIBFSEEK fseek64
00051 #define LIBFTELL ftell64
00052 #define FTELLZERO 0LL
00053 #else
00054 #ifdef KEY
00055
00056 typedef off_t _ftelltype;
00057 #define LIBFSEEK fseeko
00058 #define LIBFTELL ftello
00059 #define FTELLZERO ((_ftelltype)0)
00060 #else
00061 typedef long _ftelltype;
00062 #define LIBFSEEK fseek
00063 #define LIBFTELL ftell
00064 #define FTELLZERO 0L
00065 #endif
00066 #endif
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 int
00081 _unit_bksp(unit *cup)
00082 {
00083 int errn;
00084 unsigned char tbuf[TBUFSZB], *tp;
00085 struct ffsw fst;
00086 int first_read;
00087
00088 if (cup->useq == 0)
00089 return(FEBKSPIV);
00090
00091
00092
00093
00094
00095 WAITIO(cup, return(cup->uffsw.sw_error));
00096
00097 if (cup->uwrt) {
00098 if (cup->utrunc) {
00099
00100
00101
00102
00103 errn = _unit_trunc(cup);
00104 if (errn != 0)
00105 return(errn);
00106 }
00107 cup->uwrt = 0;
00108 }
00109
00110
00111
00112
00113
00114 switch( cup->ufs ) {
00115
00116 case FS_FDC:
00117 if ((cup->uend != LOGICAL_ENDFILE) || (cup->uspcproc==1)) {
00118
00119 errn = XRCALL(cup->ufp.fdc, backrtn) cup->ufp.fdc,
00120 &fst);
00121
00122 if (errn < 0)
00123 return(fst.sw_error);
00124 }
00125 break;
00126
00127
00128 case FS_TEXT:
00129 case STD:
00130
00131
00132
00133 if (!cup->useek)
00134 return(FENOBKPI);
00135
00136 if (!cup->ufmt)
00137 return(FENOBKSP);
00138
00139 if (cup->uend) goto ok;
00140
00141 first_read = 1;
00142
00143 for ( ; ; ) {
00144 ssize_t i;
00145 size_t ret;
00146 _ftelltype x, y;
00147
00148 y = LIBFTELL(cup->ufp.std);
00149 #ifdef KEY
00150 if (((_ftelltype) -1) == y) {
00151 return FEINTUNK;
00152 }
00153 #endif
00154 x = y;
00155
00156 if (x < TBUFSZB)
00157 x = 0;
00158 else
00159 x -= TBUFSZB;
00160
00161 #ifdef KEY
00162 if (LIBFSEEK(cup->ufp.std, x, 0)) {
00163 return FEINTUNK;
00164 }
00165 #else
00166 (void) LIBFSEEK(cup->ufp.std, x, 0);
00167 #endif
00168
00169
00170
00171
00172
00173
00174
00175 ret = fread(tbuf, 1,(size_t)(y - x), cup->ufp.std);
00176
00177 if (ret < (y-x) && ferror(cup->ufp.std))
00178 return(FEBSPNRD);
00179
00180
00181
00182
00183 if (first_read) i = ret - 2;
00184 else i = ret;
00185 first_read = 0;
00186
00187 tp = tbuf;
00188
00189 for ( ; i >= 0; i--) {
00190 if ( *(tp+i) != '\n') continue;
00191 #ifdef KEY
00192 if (LIBFSEEK(cup->ufp.std, (_ftelltype)i+1-ret,
00193 1)) {
00194 return FEINTUNK;
00195 }
00196 #else
00197 (void) LIBFSEEK(cup->ufp.std, (_ftelltype)i+1-ret, 1);
00198 #endif
00199 goto ok;
00200 }
00201
00202 if (x == 0) {
00203 #ifdef KEY
00204 if (LIBFSEEK(cup->ufp.std, FTELLZERO, 0)) {
00205 return FEINTUNK;
00206 }
00207 #else
00208 (void) LIBFSEEK(cup->ufp.std, FTELLZERO, 0);
00209 #endif
00210 goto ok;
00211 }
00212 else
00213 if (ret <= 0)
00214 return(FEINTUNK);
00215
00216 #ifdef KEY
00217 if (LIBFSEEK(cup->ufp.std, x, 0)) {
00218 return FEINTUNK;
00219 }
00220 #else
00221 (void) LIBFSEEK(cup->ufp.std, x, 0);
00222 #endif
00223 }
00224
00225 case FS_BIN:
00226 return(FENOBKSP);
00227
00228
00229 case FS_AUX:
00230 errn = FEMIXAUX;
00231
00232 break;
00233 default:
00234 return(FEINTFST);
00235 }
00236
00237 ok:
00238 cup->uend = BEFORE_ENDFILE;
00239 cup->ulastyp = DT_NONE;
00240 cup->urecpos = 0;
00241
00242 return(0);
00243 }