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
00043
00044
00045
00046
00047 #include <sys/types.h>
00048
00049 #include <string.h>
00050 #include <mutex.h>
00051 #include <stdio.h>
00052 #include <unistd.h>
00053 #include <stdlib.h>
00054 #include <cmplrs/fio.h>
00055 #include "fmt.h"
00056 #include "iomode.h"
00057 #include "idxio.h"
00058 #include "close.h"
00059 #include "err.h"
00060 #include "fio_direct_io.h"
00061 #include "util.h"
00062 #include "bcompat.h"
00063 #include "cmplrs/f_errno.h"
00064
00065 extern void __checktraps(void);
00066
00067
00068
00069 static ftnint
00070 f_clos_com (cllist *a, int lock) ;
00071
00072 ftnint
00073 f_clos (cllist *a)
00074 {
00075 return( f_clos_com( a, 0 ) );
00076 }
00077
00078 ftnint
00079 f_clos64_mp (cllist *a)
00080 {
00081 return( f_clos_com( a, 1 ) );
00082 }
00083
00084
00085
00086 static ftnint
00087 f_clos_com (cllist *a, int lock)
00088 {
00089 unit *ftnunit;
00090 char *cbuf, c, buf[256], tbuf[12];
00091 int n, istat;
00092
00093 if ((ftnunit = find_luno (a->cunit)) == NULL) {
00094 return 0;
00095 }
00096 while (lock && test_and_set( &ftnunit->lock_unit, 1L ))
00097 ;
00098 if (ftnunit->uconn <= 0) {
00099
00100 ftnunit->uconn = 0;
00101 ftnunit->lock_unit = 0;
00102 return(0);
00103 }
00104 ftnunit->uend = 0;
00105 if (cbuf = a->csta)
00106 switch (up_low (*cbuf++)) {
00107 case 'd':
00108 ftnunit->udisp = DELETE;
00109 break;
00110 case 'p':
00111 ftnunit->udisp = PRINT;
00112 goto checkdelete;
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 case 's':
00123 ftnunit->udisp = up_low (*cbuf) == 'a' ? KEEP : SUBMIT;
00124 checkdelete:
00125 while (c = (*cbuf++))
00126 if ((c == '/') && (c = (*cbuf)) && (up_low (c) == 'd'))
00127 ftnunit->udisp |= DELETE;
00128 break;
00129
00130 case 'k':
00131 if (ftnunit->uscrtch == 1)
00132 err(a->cerr, F_ERKEEPSCRATCH, "close");
00133 default:
00134 ftnunit->udisp = KEEP;
00135 }
00136 if (ftnunit->uscrtch == 1)
00137 ftnunit->udisp |= DELETE;
00138 if (ftnunit->uacc == KEYED) {
00139 n = idxclose(ftnunit, a->cerr);
00140 ftnunit->lock_unit = 0;
00141 return (n);
00142 }
00143
00144 #ifdef I90
00145
00146 if ( (ftnunit->f90sw == 1) && (ftnunit->f90nadv == 1) && (ftnunit->uwrt & WR_OP) ) {
00147 putc ('\n', ftnunit->ufd);
00148 ftnunit->f90nadv = 0;
00149 }
00150 #endif
00151
00152 if (ftnunit->ucc == CC_FORTRAN && ftnunit->ucchar)
00153 putc (ftnunit->ucchar, ftnunit->ufd);
00154
00155 if (ftnunit->ufd == stdin || ftnunit->ufd == stdout || ftnunit->ufd == stderr) {
00156
00157
00158
00159
00160 fflush(ftnunit->ufd);
00161 goto cont;
00162 }
00163 if (ftnunit->uwrt & WR_OP)
00164 (void) t_runc (ftnunit, a->cerr);
00165
00166
00167
00168 if ((ftnunit->uacc == DIRECT) && (ftnunit->ufmt == 0)) {
00169 while (lock && test_and_set( &io_lock, 1L ))
00170 ;
00171 if (ftnunit->uistty) {
00172 _fio_du_close ((int) ftnunit->ufd);
00173 } else if (((int)ftnunit->ufd) != _fio_du_close ((int) ftnunit->ufd)) {
00174 io_lock = 0;
00175 if (lock) ftnunit->lock_unit = 0;
00176 err (a->cerr, errno, "close");
00177 }
00178 io_lock = 0;
00179 } else {
00180 if (ftnunit->uistty) {
00181
00182
00183 while (lock && test_and_set( &io_lock, 1L ))
00184 ;
00185 istat = fclose (ftnunit->ufd);
00186 io_lock = 0;
00187 } else {
00188
00189 while (lock && test_and_set( &io_lock, 1L ))
00190 ;
00191 istat = fclose (ftnunit->ufd);
00192 io_lock = 0;
00193 if (istat) {
00194 if (lock) ftnunit->lock_unit = 0;
00195 err (a->cerr, errno, "close");
00196 }
00197 }
00198 }
00199
00200 if (ftnunit->ufnm) {
00201 if (ftnunit->udisp & SUBMIT) {
00202 (void) strcpy (tbuf, "tmp.FXXXXXX");
00203 (void) mktemp (tbuf);
00204 sprintf (buf, "cp %s %s", ftnunit->ufnm, tbuf);
00205 system (buf);
00206 sprintf (buf, "( chmod +x %s; %s; rm %s ) &",
00207 tbuf, tbuf, tbuf);
00208 system (buf);
00209 } else if (ftnunit->udisp & PRINT) {
00210 sprintf (buf, "lpr %s", ftnunit->ufnm);
00211 system (buf);
00212 }
00213 if (ftnunit->udisp & DELETE)
00214 (void) unlink (ftnunit->ufnm);
00215 free (ftnunit->ufnm);
00216 ftnunit->ufnm = NULL;
00217 }
00218 cont:
00219
00220
00221
00222
00223
00224
00225 if (ftnunit->f77syl) {
00226 free(ftnunit->f77syl);
00227 ftnunit->f77syl = NULL;
00228 }
00229 if (ftnunit->f77fio_buf) {
00230 free(ftnunit->f77fio_buf);
00231 ftnunit->f77fio_buf = NULL;
00232 ftnunit->f77fio_size = 0;
00233 }
00234 if (ftnunit->ukeys) {
00235 free(ftnunit->ukeys);
00236 ftnunit->ukeys = NULL;
00237 }
00238 ftnunit->ufd = NULL;
00239 ftnunit->uconn = 0;
00240 ftnunit->luno = 0;
00241 if (lock) ftnunit->lock_unit = 0;
00242
00243 return (0);
00244 }
00245
00246 #ifdef BUG_6084
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 #endif
00348
00349 void
00350 f_exit ()
00351 {
00352 int i;
00353 cllist xx;
00354
00355
00356
00357 __checktraps ();
00358 xx.cerr = 1;
00359 xx.csta = NULL;
00360 for (i = 0; i < mxunit; i++) {
00361 xx.cunit = f77units[i].luno;
00362 if (xx.cunit > 0)
00363
00364
00365
00366
00367 (void) f_clos64_mp (&xx);
00368 }
00369 }
00370
00371
00372
00373
00374 ftnint
00375 t_runc (unit *ftnunit, flag xerr) {
00376 extern void exit();
00377 #if 0
00378 char buf[128], nm[16];
00379 FILE *tmp;
00380 int n, m;
00381 int fd;
00382 #endif
00383
00384 #ifdef _BSD
00385 off_t ftell();
00386
00387 #endif
00388 #if defined (_SYSV) || defined(_SYSTYPE_SVR4)
00389 #ifndef sgi
00390 long ftell();
00391
00392 #endif
00393 #endif
00394
00395 ftnll loc, len;
00396
00397 if (ftnunit->uacc == DIRECT)
00398 return (0);
00399
00400 if (ftnunit->useek == 0 || ftnunit->ufnm == NULL)
00401 return (0);
00402
00403
00404 if (strncmp("/dev/null", ftnunit->ufnm, 9) == 0)
00405 return (0);
00406
00407 loc = FTELL (ftnunit->ufd);
00408 (void) fseek (ftnunit->ufd, 0L, 2);
00409 len = FTELL (ftnunit->ufd);
00410
00411 if (loc == len || loc < 0)
00412 return (0);
00413 if (TRUNCATE (ftnunit->ufnm, loc))
00414 err (xerr, 111, "endfile");
00415 (void) FSEEK (ftnunit->ufd, loc, 0);
00416 return 0;
00417 }
00418
00419 #pragma weak f_clos64 = f_clos