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 <cmplrs/f_errno.h>
00042 #include <unistd.h>
00043 #include <fcntl.h>
00044 #include "iomode.h"
00045 #include "util.h"
00046 #include "fio_direct_io.h"
00047 #include "bcompat.h"
00048
00049 static FILE *
00050 get_ufd(ftnint luno, int *direct_unformatted)
00051 {
00052 XINT64 loc;
00053 unit *u = find_luno(luno);
00054 FILE *fstat;
00055
00056 if (!u) {
00057 errno = F_ERUNIT;
00058 return(NULL);
00059 }
00060 if (u->uacc == DIRECT && !u->ufmt) {
00061 *direct_unformatted = 1;
00062 _fio_du_flush((int) u->ufd);
00063 } else {
00064 *direct_unformatted = 0;
00065 fflush(u->ufd);
00066 if (!(u->uwrt & RW_FILE) && !u->ureadonly) {
00067
00068
00069
00070
00071 loc = FTELL (u->ufd);
00072
00073 while (test_and_set( &io_lock, 1L ))
00074 ;
00075 fstat = freopen (u->ufnm, "r+", u->ufd);
00076 if (fstat) {
00077 FSEEK (u->ufd, loc, SEEK_SET);
00078 u->uwrt |= RW_FILE;
00079 io_lock = 0;
00080 }
00081 else {
00082
00083
00084
00085
00086 if (u->uwrt & WR_OP) {
00087
00088 fstat = freopen (u->ufnm, "a", u->ufd);
00089 io_lock = 0;
00090 if (fstat == 0) {
00091 return(NULL);
00092 }
00093 } else {
00094
00095 fstat = freopen (u->ufnm, "r", u->ufd);
00096 io_lock = 0;
00097 if (fstat == 0)
00098 return(NULL);
00099 }
00100 FSEEK (u->ufd, loc, SEEK_SET);
00101 }
00102 }
00103 }
00104 return(u->ufd);
00105 }
00106
00107
00108 static __int32_t
00109 get_fd(ftnint luno)
00110 {
00111 FILE *ufd;
00112 int direct_unformatted;
00113
00114 if ((ufd = get_ufd(luno, &direct_unformatted)) == 0 && !direct_unformatted)
00115 return(-errno);
00116 if (direct_unformatted)
00117 return((int) ufd);
00118 else
00119 return(fileno(ufd));
00120 }
00121
00122
00123 extern __int32_t
00124 fcntl_( __int32_t *fd, __int32_t *locktype, struct flock *fl)
00125 {
00126 if (*locktype == F_DUPFD || *locktype ==F_SETFL ) {
00127 if (fcntl( *fd, *locktype, * (int *) fl ))
00128 return(errno);
00129 }
00130 else if (fcntl( *fd, *locktype, fl ))
00131 return(errno);
00132 return(0);
00133 }
00134
00135
00136 extern __int32_t
00137 lockf_( __int32_t *luno, __int32_t *func, size_t *size)
00138
00139
00140
00141
00142
00143 {
00144 int fd;
00145
00146 if ((fd = get_fd(*luno)) < 0)
00147 return( -fd );
00148
00149 if (lockf( fd, *func, (long) *size ))
00150 return(errno);
00151 return(0);
00152 }
00153
00154 #ifdef SIZEOF_LUNO_IS_64
00155 extern __int32_t
00156 lockf64_( XINT *luno, __int32_t *func, size_t *size)
00157
00158
00159
00160
00161
00162 {
00163 int fd;
00164
00165 if ((fd = get_fd(*luno)) < 0)
00166 return( -fd );
00167
00168 if (lockf( fd, *func, (long) *size ))
00169 return(errno);
00170 return(0);
00171 }
00172 #endif
00173
00174 extern int
00175 setbuf_( __int32_t *luno, char *buf )
00176 {
00177 FILE *ufd;
00178 int direct_unformatted;
00179
00180 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00181 return( errno );
00182
00183 if (direct_unformatted) return (0);
00184 setbuf( ufd, buf );
00185 return(0);
00186 }
00187
00188 #ifdef SIZEOF_LUNO_IS_64
00189 extern int
00190 setbuf64_( XINT *luno, char *buf )
00191 {
00192 FILE *ufd;
00193 int direct_unformatted;
00194
00195 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00196 return( errno );
00197
00198 if (direct_unformatted) return (0);
00199 setbuf( ufd, buf );
00200 return(0);
00201 }
00202 #endif
00203
00204 extern int
00205 setvbuf_( __int32_t *luno, char *buf, int *type, size_t *size)
00206 {
00207 FILE *ufd;
00208 int direct_unformatted;
00209
00210 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00211 return( errno );
00212
00213 if (direct_unformatted)
00214 return (0);
00215 if (setvbuf( ufd, buf, *type, *size ))
00216 return(errno);
00217 return(0);
00218 }
00219
00220 #ifdef SIZEOF_LUNO_IS_64
00221 extern int
00222 setvbuf64_( XINT *luno, char *buf, int *type, size_t *size)
00223 {
00224 FILE *ufd;
00225 int direct_unformatted;
00226
00227 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00228 return( errno );
00229
00230 if (direct_unformatted)
00231 return (0);
00232 if (setvbuf( ufd, buf, *type, *size ))
00233 return(errno);
00234 return(0);
00235 }
00236 #endif
00237
00238 extern int
00239 setbuffer_( __int32_t *luno, char *buf, int *size )
00240 {
00241 FILE *ufd;
00242 int direct_unformatted;
00243
00244 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00245 return( errno );
00246
00247 if (direct_unformatted) return (0);
00248 if (setbuffer( ufd, buf, *size ))
00249 return(errno);
00250 return(0);
00251 }
00252
00253 #ifdef SIZEOF_LUNO_IS_64
00254 extern int
00255 setbuffer64_( XINT *luno, char *buf, int *size )
00256 {
00257 FILE *ufd;
00258 int direct_unformatted;
00259
00260 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00261 return( errno );
00262
00263 if (direct_unformatted) return (0);
00264 if (setbuffer( ufd, buf, *size ))
00265 return(errno);
00266 return(0);
00267 }
00268 #endif
00269
00270 extern int
00271 setlinebuf_( __int32_t *luno )
00272 {
00273 FILE *ufd;
00274 int direct_unformatted;
00275
00276 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00277 return( errno );
00278
00279 if (direct_unformatted) return (0);
00280 if (setlinebuf( ufd ))
00281 return(errno);
00282 return(0);
00283 }
00284
00285 #ifdef SIZEOF_LUNO_IS_64
00286 extern int
00287 setlinebuf64_( XINT *luno )
00288 {
00289 FILE *ufd;
00290 int direct_unformatted;
00291
00292 if ((ufd = get_ufd(*luno, &direct_unformatted)) == 0 && !direct_unformatted)
00293 return( errno );
00294
00295 if (direct_unformatted) return (0);
00296 if (setlinebuf( ufd ))
00297 return(errno);
00298 return(0);
00299 }
00300 #endif
00301
00302 extern __int32_t
00303 maplun_( int *luno )
00304 {
00305
00306
00307
00308
00309
00310
00311
00312
00313 return(get_fd(*luno));
00314 }
00315
00316 #ifdef SIZEOF_LUNO_IS_64
00317 extern __int32_t
00318 maplun64_( XINT *luno )
00319 {
00320
00321
00322
00323
00324
00325
00326
00327
00328 return(get_fd(*luno));
00329 }
00330 #endif