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 #include <limits.h>
00040 #include <mutex.h>
00041 #include <cmplrs/fio.h>
00042 #include "fmt.h"
00043 #include "iomode.h"
00044 #include "uio.h"
00045 #include "sue.h"
00046 #include "err.h"
00047 #include "util.h"
00048 #include "open.h"
00049 #include "due.h"
00050 #include "idxio.h"
00051 #include "bcompat.h"
00052
00053 int
00054 do_ui(unit *ftnunit, XINT *number, register char *ptr, ftnlen len)
00055 {
00056 register char *recp;
00057 register XINT i = *number * len;
00058
00059 if (!(ftnunit->uwrt & WR_OP)) {
00060 recp = ftnunit->f77fio_buf + ftnunit->f77recpos;
00061 if ((ftnunit->f77recpos += i) > ftnunit->url)
00062 err(ftnunit->f77errlist.cierr, 147, "indexed read");
00063 while (i-- > 0)
00064 *ptr++ = *recp++;
00065 } else {
00066 if (ftnunit->url > ftnunit->f77fio_size)
00067 check_buflen (ftnunit, ftnunit->url);
00068 recp = ftnunit->f77fio_buf + ftnunit->f77reclen;
00069 if ((ftnunit->f77reclen += i) > ftnunit->url)
00070 err(ftnunit->f77errlist.cierr, 148, "indexed write");
00071 while (i-- > 0)
00072 *recp++ = *ptr++;
00073 }
00074 return 0;
00075 }
00076
00077 static int
00078 #if 11
00079 s_rsue_com (cilist64 *a, unit **fu)
00080 #else
00081 s_rsue_com (cilist *a, unit **fu)
00082 #endif
00083 {
00084 int n;
00085 unit *ftnunit;
00086 int f77reclen_32bit;
00087
00088 if (!f77init)
00089 f_init ();
00090
00091 n = c_sue (a, fu);
00092 ftnunit = *fu;
00093 if (n) {
00094 if (n > 0) {
00095 errret(a->cierr, n, "s_rsue");
00096 } else {
00097 errret(a->ciend, n, "s_rsue");
00098 }
00099 }
00100
00101 ftnunit->f77recpos = ftnunit->f77reclen = 0;
00102
00103 #ifdef I90
00104 if (ftnunit->uaction == WRITEONLY )
00105 errret(ftnunit->f77errlist.cierr,180,"startread");
00106 #endif
00107
00108
00109
00110
00111
00112
00113 if ((ftnunit->uacc == DIRECT) && (ftnunit->ufmt == 0)) {
00114 if (ftnunit->url != 1) {
00115 ftnunit->f77do_unf = do_ud;
00116 ftnunit->f77reclen = ftnunit->url;
00117 } else {
00118
00119
00120
00121 #if (_MIPS_SIM == _MIPS_SIM_ABI64)
00122 ftnunit->f77reclen = LONGLONG_MAX;
00123 #else
00124 ftnunit->f77reclen = LONG_MAX;
00125 #endif
00126 ftnunit->f77do_unf = do_ud;
00127 }
00128 _fio_seq_pos( ftnunit->ufd, ftnunit );
00129 ftnunit->uwrt &= ~WR_OP;
00130 return (0);
00131 } else {
00132 if (ftnunit->uwrt & WR_OP)
00133 (void) f77nowreading (ftnunit);
00134 }
00135
00136
00137
00138 if (ftnunit->uacc == KEYED) {
00139 ftnunit->f77do_unf = do_ui;
00140 ftnunit->f77idxlist.cimatch = a->cimatch;
00141 ftnunit->f77idxlist.cikeytype = a->cikeytype;
00142 ftnunit->f77idxlist.cikeyval.cicharval = a->cikeyval.cicharval;
00143 ftnunit->f77idxlist.cikeyid = a->cikeyid;
00144 ftnunit->f77idxlist.cinml = a->cinml;
00145 ftnunit->f77idxlist.cikeyvallen = a->cikeyvallen;
00146 if (n = idxread(ftnunit)) {
00147 if (n > 0) {
00148 errret(a->cierr, n, "s_rsue");
00149 } else {
00150 errret(a->ciend, n, "s_rsue");
00151 }
00152 }
00153 } else if (ftnunit->url != 1) {
00154 ftnunit->f77do_unf = do_us;
00155 if (ftnunit->uerror)
00156 unf_position (ftnunit->ufd, ftnunit);
00157 if (fread ((char *) &f77reclen_32bit, sizeof (int), 1, ftnunit->ufd) != 1) {
00158 if (feof (ftnunit->ufd)) {
00159 ftnunit->uend = 1;
00160 errret(a->ciend, EOF, "start");
00161 }
00162 clearerr(ftnunit->ufd);
00163 errret(a->cierr, errno, "start");
00164 }
00165 ftnunit->f77reclen = f77reclen_32bit;
00166 } else {
00167 ftnunit->f77reclen = INT_MAX;
00168 ftnunit->f77do_unf = do_ud;
00169 }
00170 return (0);
00171 }
00172
00173 int
00174 s_rsue (cilist *a)
00175 {
00176 #if 11
00177 cilist64 dst;
00178 get_cilist64(&dst, a);
00179 return s_rsue_com(&dst, &f77curunit);
00180 #else
00181 return( s_rsue_com( a, &f77curunit) );
00182 #endif
00183 }
00184
00185 int
00186 s_rsue_mp (cilist *a, unit **fu)
00187 {
00188 #if 11
00189 cilist64 dst;
00190 get_cilist64(&dst, a);
00191 return s_rsue_com(&dst, fu);
00192 #else
00193 return( s_rsue_com( a, fu) );
00194 #endif
00195 }
00196
00197 #if 11
00198
00199 int
00200 s_rsue64 (cilist64 *a)
00201 {
00202 return( s_rsue_com( a, &f77curunit) );
00203 }
00204
00205 int
00206 s_rsue64_mp (cilist64 *a, unit **fu)
00207 {
00208 return( s_rsue_com( a, fu) );
00209 }
00210
00211 #endif
00212
00213
00214 int
00215 #if 11
00216 wsue (cilist64 *a, unit **fu)
00217 #else
00218 wsue (cilist *a, unit **fu)
00219 #endif
00220 {
00221 int n;
00222 unit *ftnunit;
00223
00224 if (!f77init)
00225 f_init ();
00226 if (n = c_sue (a, fu))
00227 return n;
00228 ftnunit = *fu;
00229 (void) f77nowwriting( ftnunit );
00230 ftnunit->f77reclen = 0;
00231
00232
00233
00234
00235 return (0);
00236 }
00237
00238 static int
00239 #if 11
00240 s_wsue_com (cilist64 *a, unit **fu)
00241 #else
00242 s_wsue_com (cilist *a, unit **fu)
00243 #endif
00244 {
00245 unit *ftnunit;
00246 int n;
00247
00248 n = wsue(a, fu);
00249 ftnunit = *fu;
00250 if (n) {
00251 errret(a->cierr, n, "s_wsue");
00252 }
00253 if (ftnunit->uacc == KEYED) {
00254 ftnunit->f77idxlist.cimatch = a->cimatch;
00255 ftnunit->f77idxlist.cikeytype = a->cikeytype;
00256 ftnunit->f77idxlist.cikeyval.cicharval = a->cikeyval.cicharval;
00257 ftnunit->f77idxlist.cikeyid = a->cikeyid;
00258 ftnunit->f77idxlist.cinml = a->cinml;
00259 ftnunit->f77idxlist.cikeyvallen = a->cikeyvallen;
00260 ftnunit->f77do_unf = do_ui;
00261 }
00262 else {
00263 if (ftnunit->uacc == DIRECT) {
00264 ftnunit->f77recpos = 0;
00265 ftnunit->f77do_unf = do_ud;
00266 _fio_seq_pos( ftnunit->ufd, ftnunit );
00267 } else {
00268 if (ftnunit->uwrt != WR_READY && f77nowwriting (ftnunit))
00269 errret(a->cierr, 160, "startwrt");
00270 est_reclen = ftnunit->f77reclen = 0;
00271 ftnunit->overflowed = 0;
00272 ftnunit->f77recpos = 4;
00273 ftnunit->f77do_unf = do_us;
00274 if (ftnunit->uerror)
00275 unf_position (ftnunit->ufd, ftnunit);
00276 }
00277 }
00278 return 0;
00279 }
00280
00281 int
00282 s_wsue (cilist *a)
00283 {
00284 #if 11
00285 cilist64 dst;
00286 get_cilist64(&dst, a);
00287 return s_wsue_com(&dst, &f77curunit);
00288 #else
00289 return( s_wsue_com( a, &f77curunit) );
00290 #endif
00291 }
00292
00293 int
00294 s_wsue_mp (cilist *a, unit **fu)
00295 {
00296 #if 11
00297 cilist64 dst;
00298 get_cilist64(&dst, a);
00299 return s_wsue_com(&dst, fu);
00300 #else
00301 return( s_wsue_com( a, fu) );
00302 #endif
00303 }
00304
00305 #if 11
00306
00307 int
00308 s_wsue64 (cilist64 *a)
00309 {
00310 return( s_wsue_com( a, &f77curunit) );
00311 }
00312
00313 int
00314 s_wsue64_mp (cilist64 *a, unit **fu)
00315 {
00316 return( s_wsue_com( a, fu) );
00317 }
00318
00319 #endif
00320
00321 int
00322 #if 11
00323 c_sue (cilist64 *a, unit **fu)
00324 #else
00325 c_sue (cilist *a, unit **fu)
00326 #endif
00327 {
00328 unit *ftnunit;
00329
00330 if ((ftnunit = map_luno (a->ciunit)) == NULL)
00331 errret(a->cierr, 101, "startio");
00332 while (fu != &f77curunit && test_and_set( &ftnunit->lock_unit, 1L ))
00333 ;
00334 *fu = ftnunit;
00335 if (ftnunit->uconn <= 0 && fk_open (SEQ, UNF, a->ciunit)) {
00336 ftnunit->uconn = 0;
00337 errret(a->cierr, 114, "sue");
00338 }
00339 ftnunit->f77errlist.cierr = a->cierr;
00340 ftnunit->f77errlist.ciend = a->ciend;
00341 ftnunit->f77errlist.cieor = a->cieor;
00342 ftnunit->f77errlist.cisize = a->cisize;
00343 ftnunit->f77errlist.iciunit = 0;
00344 if (ftnunit->ufmt > 0) {
00345 if ((ftnunit->ufd == stdin || ftnunit->ufd == stdout ||
00346 ftnunit->ufd == stderr) && ftnunit->useek)
00347
00348
00349
00350
00351 ftnunit->ufmt = 1;
00352 else
00353 errret(a->cierr, 103, "sue");
00354 }
00355 if (!ftnunit->useek && ftnunit->uacc == SEQUENTIAL)
00356 errret(a->cierr, 103, "sue");
00357 return (0);
00358 }
00359
00360 int
00361 e_rsue (void)
00362 {
00363 return( e_rsue_mp( &f77curunit ) );
00364 }
00365
00366 int
00367 e_rsue_mp (unit **fu)
00368 {
00369 unit *ftnunit = *fu;
00370 int n;
00371 if (ftnunit->uacc != KEYED && ftnunit->url != 1) {
00372 XINT nleft = ftnunit->f77reclen - ftnunit->f77recpos;
00373
00374 if ((ftnunit->uacc == DIRECT) && (ftnunit->ufmt == 0)) {
00375 return( e_rdue_mp (fu) );
00376 } else if (ftnunit->uacc == DIRECT) {
00377 if (nleft > 0) {
00378 if (nleft <= ftnunit->f77fio_size && nleft < 1000) {
00379 fread (ftnunit->f77fio_buf, nleft, 1, ftnunit->ufd);
00380 } else {
00381 (void) fseek (ftnunit->ufd, nleft, 1);
00382 }
00383 }
00384 } else {
00385 if (nleft + sizeof (int) <= ftnunit->f77fio_size && nleft < 1000) {
00386 fread (ftnunit->f77fio_buf, nleft + sizeof (int), 1, ftnunit->ufd);
00387 } else {
00388 (void) fseek (ftnunit->ufd, (long) (nleft + sizeof (int)), 1);
00389 }
00390 }
00391 if (ferror (ftnunit->ufd))
00392 errret(ftnunit->f77errlist.cierr, errno, "sue");
00393 }
00394 ftnunit->lock_unit = 0;
00395 return (0);
00396 }
00397
00398
00399
00400 int
00401 unf_position (FILE *fd, unit *ftnunit)
00402 {
00403
00404
00405
00406 ftnll pos = FTELL(fd);
00407
00408
00409
00410 int reclen_short;
00411
00412 if (ftnunit->uerror == EOF) {ftnunit->uerror = 0; return(0);}
00413 ftnunit->uerror = 0;
00414 fseek (fd, 0, 0);
00415 while (FTELL (fd) < pos) {
00416
00417 fread ((char *) &reclen_short, sizeof (int), 1, fd);
00418 fseek (fd, (long) (reclen_short + sizeof (int)), SEEK_CUR);
00419 }
00420 return (0);
00421 }
00422
00423 #if 11
00424 #pragma weak e_rsue64 = e_rsue
00425 #pragma weak e_rsue64_mp = e_rsue_mp
00426 #endif