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 <cmplrs/fio.h>
00040 #include <mutex.h>
00041 #include "fmt.h"
00042 #include "iomode.h"
00043 #include "vmsflags.h"
00044 #include "err.h"
00045 #include "close.h"
00046 #include "unistd.h"
00047 #include "util.h"
00048 #include "bcompat.h"
00049
00050 #define ASSOCV 12
00051
00052 #define get_alist64(p_dst, p_src) ((p_dst)->aerr = (p_src)->aerr, (p_dst)->aunit = (p_src)->aunit)
00053
00054 static int
00055 f_rew_com (alist *a, int lock)
00056 {
00057 unit *ftnunit;
00058
00059 if ((ftnunit = find_luno (a->aunit)) == NULL)
00060 return(0);
00061 while (lock && test_and_set( &ftnunit->lock_unit, 1L ))
00062 ;
00063
00064 if (ftnunit->uacc == KEYED)
00065 errret(a->aerr, 164, "rewind");
00066
00067 if (ftnunit->uconn <= 0) {
00068 ftnunit->lock_unit = 0;
00069 return (0);
00070 }
00071
00072 if (!ftnunit->useek && !ftnunit->uistty)
00073 errret(a->aerr, 106, "rewind");
00074 ftnunit->uend = 0;
00075
00076
00077 if (ftnunit->uassocv)
00078 set_var (ftnunit->uassocv, ftnunit->umask, ASSOCV, (ftnll) 1);
00079
00080
00081
00082 if ((ftnunit->uacc == DIRECT) && (ftnunit->ufmt == 0)) {
00083 if (-1 == lseek ((int) ftnunit->ufd, 0, SEEK_SET)) {
00084 errret(a->aerr, 106, "rewind");
00085 }
00086
00087 _fio_set_seek((int) ftnunit->ufd, (ftnll) 0, 0);
00088 ftnunit->uirec = 0;
00089 ftnunit->lock_unit = 0;
00090 return (1);
00091 }
00092
00093 #ifdef I90
00094
00095 ftnunit->f77recpos = 0;
00096 ftnunit->f77recend = 0;
00097 #endif
00098
00099 if (f77vms_flag_[VMS_EF]) {
00100
00101 char buf[513];
00102 XINT64 y, x;
00103 int i, n;
00104 char ch;
00105
00106
00107
00108
00109
00110 if (ftnunit->uwrt & WR_OP) {
00111 #ifdef I90
00112
00113 if (ftnunit->f90sw == 1 && ftnunit->f90nadv == 1 ) {
00114 putc ('\n', ftnunit->ufd);
00115 ftnunit->f90nadv = 0;
00116 }
00117 #endif
00118 (void) t_runc (ftnunit, a->aerr);
00119
00120 if (f77nowreading(ftnunit))
00121 errret(a->aerr, 106, "rewind");
00122 }
00123 if (ftnunit->ufmt != 1) {
00124 if (ftell (ftnunit->ufd) == 0) {
00125 ftnunit->lock_unit = 0;
00126 return (0);
00127 }
00128 if (fseek (ftnunit->ufd, (long) (-sizeof (int)), 1) < 0)
00129 errret(a->aerr, 106, "rewind");
00130 for (i = 0;; i++) {
00131 (void) fread ((char *) &n, sizeof (int), 1, ftnunit->ufd);
00132 if (n != 1 || i == 0) {
00133 if (fseek (ftnunit->ufd, (long) (-n - 3 * sizeof (int)), 1)) {
00134 rewind (ftnunit->ufd);
00135 ftnunit->lock_unit = 0;
00136 return (0);
00137 }
00138 } else {
00139 if (fseek (ftnunit->ufd, -(sizeof (int) + 1), 1)) {
00140 rewind (ftnunit->ufd);
00141 ftnunit->lock_unit = 0;
00142 return (0);
00143 }
00144 (void) fread ((char *) &ch, 1, 1, ftnunit->ufd);
00145 if (ch == '\032') {
00146 fseek (ftnunit->ufd, sizeof (int), 1);
00147 ftnunit->lock_unit = 0;
00148 return (0);
00149 }
00150 fseek (ftnunit->ufd, -(2 * sizeof (int) + 1), 1);
00151 }
00152 }
00153 }
00154 y = x = FTELL (ftnunit->ufd) - 2;
00155
00156 if (y < 0) {
00157 (void) fseek(ftnunit->ufd, 0L, 0);
00158 ftnunit->lock_unit = 0;
00159 return(0);
00160 }
00161 ch = '\0';
00162 for (;;) {
00163 if (x < sizeof (buf) - 1)
00164 x = 0;
00165 else
00166 x -= sizeof (buf) - 1;
00167 (void) FSEEK (ftnunit->ufd, x, 0);
00168 n = (int) fread (buf, 1, (int) (y - x), ftnunit->ufd);
00169 buf[n] = ch;
00170 for (i = n - 1; i >= 1; i--) {
00171 if (buf[i] != '\032' || buf[i + 1] != '\n')
00172 continue;
00173 (void) fseek (ftnunit->ufd, (long) (i + 2 - n), 1);
00174 ftnunit->lock_unit = 0;
00175 return (0);
00176 }
00177 if (x == 0) {
00178 (void) fseek (ftnunit->ufd, 0L, 0);
00179 ftnunit->lock_unit = 0;
00180 return (0);
00181 }
00182 y = x;
00183 ch = buf[0];
00184 }
00185 }
00186
00187
00188
00189
00190 if (ftnunit->uwrt & WR_OP) {
00191 #ifdef I90
00192
00193 if (ftnunit->f90sw == 1 && ftnunit->f90nadv == 1 ) {
00194 putc ('\n', ftnunit->ufd);
00195 ftnunit->f90nadv = 0;
00196 }
00197 #endif
00198 (void) t_runc (ftnunit, a->aerr);
00199
00200 if (f77nowreading(ftnunit))
00201 errret(a->aerr, 106, "backspace");
00202 }
00203 rewind (ftnunit->ufd);
00204 ftnunit->lock_unit = 0;
00205 return (0);
00206 }
00207
00208 int
00209 f_rew (alist *a)
00210 {
00211 return( f_rew_com( a, 0 ) );
00212 }
00213
00214 int
00215 f_rew64_mp (alist *a)
00216 {
00217 return( f_rew_com( a, 1 ) );
00218 }
00219
00220 #pragma weak f_rew64 = f_rew