| 1 |
/* @(#)osd.c 19.1 (ESO-DMD) 02/25/03 13:56:11 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*+++++++++++ |
| 30 |
.TYPE Module |
| 31 |
.NAME osd.c |
| 32 |
.LANGUAGE C |
| 33 |
.AUTHOR IPG-ESO Garching |
| 34 |
.CATEGORY Host operating system interfaces. i/o on disk files. |
| 35 |
.ENVIRONMENT VAX / VMS |
| 36 |
.COMMENTS Handling of disk io's. |
| 37 |
All files are referenced by the physical name in the open function. |
| 38 |
A file identifier (number) |
| 39 |
is returned by open functions to be used in later i/o operations. |
| 40 |
\begin{TeX} |
| 41 |
This module includes {\em osd} routines which perform |
| 42 |
access to disk files. Possible seeking at byte address. |
| 43 |
The buffering is done within this routine, and a |
| 44 |
large blocking {\em LARGE\_BUFFER} option is also available. |
| 45 |
Note that only {\em STMLF} format files are |
| 46 |
unix-compatible! For other format, use preferably the {\em osa} routines... |
| 47 |
|
| 48 |
The routines return always |
| 49 |
a non-negative integer number on successful return. |
| 50 |
Otherwise, a value of -1 is set to indicate an error |
| 51 |
condition or the End of File; the variable ``oserror'' |
| 52 |
contains the numeric error code ($0$ for EOF), |
| 53 |
which can be converted to a character pointer via {\em osmsg routine}. |
| 54 |
|
| 55 |
\end{TeX} |
| 56 |
|
| 57 |
.VERSION 0.0 25-Aug-1986 Definition J. D. Ponz |
| 58 |
.VERSION 2.0 13-Oct-1988 VMS version (F. Ochsenbein) |
| 59 |
.VERSION 2.1 10-Jan-1989 Coorected bug in osdopen (propagate NOATTR option) |
| 60 |
.VERSION 2.2 24-Aug-1990 Corrected bug in osdwrite (very large blocks). |
| 61 |
Allow non-sequential organisation. |
| 62 |
.VERSION 2.3 19-Nov-1990 Force multiple of 8 bytes for buffers. |
| 63 |
.VERSION 2.4 23-Apr-1991 Be sure to free buffer memory at osdclose... |
| 64 |
------------------------------------------------------------*/ |
| 65 |
|
| 66 |
#define IO_MAX (90*512) /* Set to 16 FITS blocks */ |
| 67 |
|
| 68 |
#define DEBUG 0 |
| 69 |
#include <osdefos.h> |
| 70 |
#include <osfile.h> |
| 71 |
#include <macrogen.h> |
| 72 |
#include <atype.h> |
| 73 |
#include <errno.h> /* System */ |
| 74 |
#include <iodef.h> /* System */ |
| 75 |
# define fab$l_fna name |
| 76 |
# define fab$b_fns lname |
| 77 |
# define rab$l_ctx buffer /* Used as address of Buffer */ |
| 78 |
# define fab$l_ctx file_no |
| 79 |
#include <rms.h> /* System */ |
| 80 |
#include <ssdef.h> /* System */ |
| 81 |
#include <devdef.h> /* System */ |
| 82 |
|
| 83 |
MID_EXTERN int vmserror; /* VMS-specific error code */ |
| 84 |
MID_EXTERN int oserror; |
| 85 |
MID_EXTERN char *oserrmsg; |
| 86 |
|
| 87 |
#define FINISH goto FIN |
| 88 |
|
| 89 |
static struct FAB *fap; |
| 90 |
static struct XABFHC *fhp; |
| 91 |
static struct RAB *fp; |
| 92 |
static int TheFile = 0; |
| 93 |
|
| 94 |
static unsigned short chan; /* Set by getfile, tells QIO's to be used */ |
| 95 |
static short move_len; |
| 96 |
|
| 97 |
struct RAB *vmsrab(); |
| 98 |
long vmsize(); |
| 99 |
|
| 100 |
struct Buffer { |
| 101 |
int size, offset, bytes, block; |
| 102 |
unsigned long file_size; |
| 103 |
unsigned char buffer_modified; |
| 104 |
unsigned char flush; |
| 105 |
unsigned char spare[2]; |
| 106 |
char *text; |
| 107 |
}; |
| 108 |
static struct Buffer *b; |
| 109 |
static unsigned long full_size; |
| 110 |
static char gbuf = 1; /* In getfile(), ask to allocate buffer */ |
| 111 |
|
| 112 |
/*==========================================================================*/ |
| 113 |
static int vmsread(pbuf, nochar) |
| 114 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 115 |
.PURPOSE Interface to SYS$READ |
| 116 |
.RETURNS Number of characters actually read / -1 if an error is encountered / |
| 117 |
0 if the end of file is reached. |
| 118 |
In case of error, oserror is set to the relevant error number. |
| 119 |
.REMARKS Works only on Disk Files ! The virtual sector to read is |
| 120 |
b->block. Also, nochar is assumed to be POSITIVE ... |
| 121 |
------------------------------------------------------------*/ |
| 122 |
char *pbuf; /* OUT : pointer to i/o buffer */ |
| 123 |
int nochar; /* IN : size of i/o buffer */ |
| 124 |
{ |
| 125 |
register int len; |
| 126 |
|
| 127 |
if (nochar <= 0) { oserror = EINVAL; FINISH; } |
| 128 |
fp->rab$l_bkt = b->block + 1; |
| 129 |
fp->rab$l_ubf = pbuf; |
| 130 |
fp->rab$w_usz = nochar; |
| 131 |
|
| 132 |
vmserror = SYS$READ(fp); |
| 133 |
|
| 134 |
len = fp->rab$w_rsz; /* Length of read block */ |
| 135 |
switch(vmserror) |
| 136 |
{ case RMS$_NORMAL: /* Compute Next Block */ |
| 137 |
break; |
| 138 |
case RMS$_EOF: len = 0; break; |
| 139 |
default: oserror = EIO; break; |
| 140 |
} |
| 141 |
|
| 142 |
FIN: |
| 143 |
#if DEBUG |
| 144 |
printf("vmsread file %d rab$l_bkt #%d: bytes=%d on %d\n", |
| 145 |
TheFile, fp->rab$l_bkt-1, len, nochar); |
| 146 |
#endif |
| 147 |
return(oserror ? -1 : len); |
| 148 |
} |
| 149 |
|
| 150 |
/*==========================================================================*/ |
| 151 |
static int vmswrite(pbuf, nochar) |
| 152 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 153 |
.PURPOSE Interface to SYS$PUT. |
| 154 |
.RETURNS Number of characters actually written / -1 if an error is encountered. |
| 155 |
In case of error, oserror is set to the relevant error number. |
| 156 |
.REMARKS Works only on Disk Files ! The virtual sector to read is |
| 157 |
b->block. Also, nochar is assumed to be POSITIVE ... |
| 158 |
------------------------------------------------------------*/ |
| 159 |
char *pbuf; /* IN : pointer to i/o buffer */ |
| 160 |
int nochar; /* IN : size of i/o buffer */ |
| 161 |
{ |
| 162 |
register int len; |
| 163 |
|
| 164 |
fp->rab$l_bkt = b->block + 1; |
| 165 |
fp->rab$l_rbf = pbuf; |
| 166 |
fp->rab$w_rsz = nochar; |
| 167 |
|
| 168 |
vmserror = SYS$WRITE(fp); |
| 169 |
|
| 170 |
if (!(vmserror&1)) oserror = EIO; |
| 171 |
else len = nochar; |
| 172 |
|
| 173 |
FIN: |
| 174 |
#if DEBUG |
| 175 |
printf("vmswritten file %d: bytes=%d on %d - Block is now %d\n", |
| 176 |
TheFile, len, nochar, fp->rab$l_bkt); |
| 177 |
#endif |
| 178 |
return(oserror ? -1 : len); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
/**************************** |
| 183 |
* General Utility Routines * |
| 184 |
****************************/ |
| 185 |
|
| 186 |
/*==========================================================================*/ |
| 187 |
static int osbuffer(size) |
| 188 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 189 |
.PURPOSE Open a buffer for file fid. |
| 190 |
.RETURNS size of buffer (success) / -1 (failure) |
| 191 |
------------------------------------------------------------*/ |
| 192 |
int size; /* IN : size of buffer (bytes) */ |
| 193 |
{ |
| 194 |
|
| 195 |
b = (struct Buffer *)osmmget(size + sizeof(struct Buffer)); |
| 196 |
if (oserror) FINISH; |
| 197 |
|
| 198 |
fp->buffer = (unsigned int) b; /* casted by tm 05.09.89 */ |
| 199 |
b->text = (char *) ( b + 1); |
| 200 |
b->size = size; |
| 201 |
b->offset = 0; |
| 202 |
b->bytes = 0; |
| 203 |
b->block = 0; /* Nothing was read */ |
| 204 |
b->buffer_modified = 0; |
| 205 |
b->flush = 0; |
| 206 |
b->file_size = full_size; |
| 207 |
|
| 208 |
FIN: |
| 209 |
return (oserror ? -1 : size); |
| 210 |
} |
| 211 |
|
| 212 |
/*==========================================================================*/ |
| 213 |
static int getfile(fid) |
| 214 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 215 |
.PURPOSE Retrieves the file pointer. |
| 216 |
.RETURNS File number (fid); the static fp (file pointer) is also set up. |
| 217 |
------------------------------------------------------------*/ |
| 218 |
int fid; /* IN : file identifier */ |
| 219 |
{ |
| 220 |
if (!(fp = vmsrab(fid))) FINISH; |
| 221 |
|
| 222 |
b = NULL_PTR(struct Buffer); |
| 223 |
fap = fp->rab$l_fab; |
| 224 |
fhp = (struct XABFHC *)(fap->fab$l_xab); |
| 225 |
TheFile = fap->file_no; |
| 226 |
|
| 227 |
if (fp->rab$l_ctx == 0xffffffff) { /* Indicator for QIO usage */ |
| 228 |
chan = fp->rab$w_isi; |
| 229 |
} |
| 230 |
else { |
| 231 |
chan = 0, b = (struct Buffer *)fp->buffer; |
| 232 |
if(gbuf && (!b)) /* Allocate Buffer if Necessary */ |
| 233 |
{ full_size = (fhp->xab$l_ebk > 0 ? |
| 234 |
(fhp->xab$l_ebk-1)*fap->fab$w_bls + fhp->xab$w_ffb : |
| 235 |
0); |
| 236 |
osbuffer(fap->fab$w_bls); |
| 237 |
b->block = 0; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
FIN: |
| 242 |
return (oserror ? -1 : fid); |
| 243 |
} |
| 244 |
|
| 245 |
/*==========================================================================*/ |
| 246 |
static int oswb() |
| 247 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 248 |
.PURPOSE Write the Buffer to disk (called by osd routines) |
| 249 |
.RETURNS Bytes written / -1 for error (oserror provides an explanation) |
| 250 |
------------------------------------------------------------*/ |
| 251 |
{ |
| 252 |
register int len; |
| 253 |
unsigned long fsize; |
| 254 |
|
| 255 |
#if DEBUG |
| 256 |
printf("....Entering oswb: block=%d, size=%d, bytes=%d, offset=%d, file_size=%d, flag=%d\n", |
| 257 |
b->block, b->size, b->bytes, b->offset, b->file_size, b->buffer_modified); |
| 258 |
#endif |
| 259 |
len = 0; |
| 260 |
|
| 261 |
if (b->buffer_modified) /* Write Block */ |
| 262 |
{ fsize = b->block*fap->fab$w_bls + b->bytes; |
| 263 |
if (b->file_size < fsize) b->file_size = fsize; |
| 264 |
if ((len = vmswrite(b->text, b->bytes)) < 0) FINISH; |
| 265 |
b->buffer_modified = 0; |
| 266 |
#if DEBUG |
| 267 |
printf("....Exiting oswb: block=%d, size=%d, bytes=%d, offset=%d, file_size=%d, flag=%d\n", |
| 268 |
b->block, b->size, b->bytes, b->offset, b->file_size, b->buffer_modified); |
| 269 |
#endif |
| 270 |
} |
| 271 |
|
| 272 |
FIN: |
| 273 |
return (oserror ? -1 : len); |
| 274 |
} |
| 275 |
|
| 276 |
/*==========================================================================*/ |
| 277 |
static int osrb() |
| 278 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 279 |
.PURPOSE Read the Next Buffer from disk (called by osd routines) |
| 280 |
.RETURNS Length of read block, 0 for EOF, -1 for errors. |
| 281 |
.REMARKS The current is rewritten on disk, if it was modified. |
| 282 |
------------------------------------------------------------*/ |
| 283 |
{ |
| 284 |
register int i; |
| 285 |
|
| 286 |
/* Read now one Buffer */ |
| 287 |
|
| 288 |
#if DEBUG |
| 289 |
printf("....Entering osrb: block=%d, size=%d, bytes=%d, offset=%d, file_size=%d, flag=%d\n", |
| 290 |
b->block, b->size, b->bytes, b->offset, b->file_size, b->buffer_modified); |
| 291 |
#endif |
| 292 |
if (b->buffer_modified) |
| 293 |
if (oswb() < 0) FINISH; |
| 294 |
if ((b->block * fap->fab$w_bls + b->bytes) >= b->file_size) |
| 295 |
{ i= 0; FINISH; } /* EOF */ |
| 296 |
b->block += (b->bytes / fap->fab$w_bls); |
| 297 |
b->bytes = 0; |
| 298 |
b->offset = 0; |
| 299 |
i = vmsread(b->text, b->size); |
| 300 |
if (i < 0) FINISH; |
| 301 |
b->bytes = i; |
| 302 |
#if DEBUG |
| 303 |
printf("....Exiting osrb: block=%d, size=%d, bytes=%d, offset=%d, file_size=%d, flag=%d\n", |
| 304 |
b->block, b->size, b->bytes, b->offset, b->file_size, b->buffer_modified); |
| 305 |
#endif |
| 306 |
|
| 307 |
FIN: |
| 308 |
return (oserror ? -1 : i); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/************************ |
| 313 |
* OSD Routines * |
| 314 |
************************/ |
| 315 |
|
| 316 |
|
| 317 |
/*==========================================================================*/ |
| 318 |
int osdopen(phname, mode) |
| 319 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 320 |
.PURPOSE Opens disk file for read or write, and positions at |
| 321 |
file beginning (at end for APPEND mode). |
| 322 |
The variable `mode' defines the way of opening the file. |
| 323 |
.RETURNS Upon successful completion a file descriptor is returned. -1 is |
| 324 |
returned on error. |
| 325 |
.REMARKS The mode is a combination of the following options |
| 326 |
\begin{TeX} |
| 327 |
(defined in {\tt osfile.h}): |
| 328 |
\begin{itemize} |
| 329 |
\item {\em READ WRITE READ\_WRITE APPEND} |
| 330 |
\item {\em NOATTR} on VMS only (record without attributes, i.e. without linefeed) |
| 331 |
\item {\em LARGE\_BUFFER} for better efficiency when randomly accessing |
| 332 |
large sections files. |
| 333 |
\end{itemize} |
| 334 |
The routine {\em osfop}, if called before {\em osdopen}, |
| 335 |
allows to use non STMLF record formats. |
| 336 |
Note that e.g. variable-length record are not interpreted: |
| 337 |
the file is seen as a stream !! |
| 338 |
\end{TeX} |
| 339 |
------------------------------------------------------------*/ |
| 340 |
char *phname; /* IN : physical filename */ |
| 341 |
int mode; /* IN : open mode (see remarks) */ |
| 342 |
{ |
| 343 |
register int i, bs, bsize; |
| 344 |
|
| 345 |
if ((i = vmsopen(phname, mode)) < 0) FINISH; |
| 346 |
|
| 347 |
/* Check Special File Organisation */ |
| 348 |
fp = vmsrab(i); |
| 349 |
if (fp->rab$l_ctx == 0xffffffff) FINISH; /* Not a Disk */ |
| 350 |
fap = fp->rab$l_fab; |
| 351 |
/* Take care of non-sequential files. |
| 352 |
Close / Reopen to use QIO's only. */ |
| 353 |
if (fap->fab$b_org != FAB$C_SEQ) { |
| 354 |
bs = vmsopen ((char *)-1, 0); |
| 355 |
if (bs < 0) FINISH; |
| 356 |
fp->rab$l_bkt = 1; /* Virtual Block */ |
| 357 |
fp->rab$l_ctx = 0xffffffff; /* Use Qio */ |
| 358 |
fp->rab$w_isi = bs; |
| 359 |
fap->fab$l_fop = FAB$M_UFO; |
| 360 |
FINISH; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
bs = gbuf; gbuf = 0; /* Don't allocate buffers */ |
| 365 |
getfile(i); |
| 366 |
gbuf = bs; |
| 367 |
|
| 368 |
/* Determine the size of buffers */ |
| 369 |
|
| 370 |
bs = (mode&LARGE_BUFFER ? IO_MAX/fap->fab$w_bls : 1); |
| 371 |
full_size = vmsize(TheFile); |
| 372 |
bsize = fap->fab$w_bls * bs; |
| 373 |
if ((mode&3) == 0) { |
| 374 |
bsize = MIN(bsize, full_size); |
| 375 |
bsize = ((bsize+7)>>3)<<3; /* Take multiple of 8 bytes */ |
| 376 |
} |
| 377 |
if (osbuffer(bsize) < 0) FINISH; |
| 378 |
|
| 379 |
switch(mode&3) { |
| 380 |
case APPEND: |
| 381 |
b->block = b->file_size/fap->fab$w_bls; |
| 382 |
if (osrb() < 0) FINISH; |
| 383 |
b->offset = b->bytes; |
| 384 |
break; |
| 385 |
case READ_WRITE: |
| 386 |
if (osrb() < 0) FINISH; |
| 387 |
break; |
| 388 |
case WRITE: |
| 389 |
b->block = 0; |
| 390 |
break; |
| 391 |
} |
| 392 |
|
| 393 |
FIN: |
| 394 |
return (oserror ? -1 : i); |
| 395 |
} |
| 396 |
|
| 397 |
/*==========================================================================*/ |
| 398 |
int osdclose(fid) |
| 399 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 400 |
.PURPOSE Closes a disk file (previously opened by osdopen) |
| 401 |
.RETURNS 0 (success) / -1 (failure) |
| 402 |
------------------------------------------------------------*/ |
| 403 |
int fid; /* IN : file identifier */ |
| 404 |
{ |
| 405 |
if (getfile(fid) < 0) FINISH; |
| 406 |
|
| 407 |
if (chan == 0) |
| 408 |
if (oswb() < 0) FINISH; /* Write last buffer */ |
| 409 |
|
| 410 |
osmmfree(fp->buffer); /* Free Buffer */ |
| 411 |
vmsclose(TheFile); |
| 412 |
|
| 413 |
FIN: |
| 414 |
return (oserror ? -1 : 0); |
| 415 |
} |
| 416 |
|
| 417 |
/*==========================================================================*/ |
| 418 |
int osdwait(fid) |
| 419 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 420 |
.PURPOSE Force the physical writing on disk of modified buffers. |
| 421 |
The argument fid is the file identification obtained from osdopen. |
| 422 |
.RETURNS 0 (success) / -1 (failure) |
| 423 |
.REMARKS |
| 424 |
------------------------------------------------------------*/ |
| 425 |
int fid; /* IN : file identifier */ |
| 426 |
{ |
| 427 |
if (getfile(fid) < 0) FINISH; |
| 428 |
|
| 429 |
if (chan) FINISH; |
| 430 |
|
| 431 |
if (oswb() < 0) FINISH; |
| 432 |
|
| 433 |
FIN: |
| 434 |
return (oserror ? -1 : 0); |
| 435 |
} |
| 436 |
|
| 437 |
/*==========================================================================*/ |
| 438 |
int osdread(fid, pbuf, nochar) |
| 439 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 440 |
.PURPOSE Synchronous read from a file. |
| 441 |
The argument fid is the file identification obtained from osdopen. |
| 442 |
The routine reads nochar bytes starting at at the current |
| 443 |
byte position. |
| 444 |
.RETURNS --- Number of characters actually read. |
| 445 |
--- -1 if an error is encountered or if the end of file is |
| 446 |
reached. In case of error, oserror is set to the relevant error |
| 447 |
number; oserror is set to 0 in case of EOF. |
| 448 |
.REMARKS It's a straight read, i.e. reading variable-length record files |
| 449 |
provide stange results... |
| 450 |
------------------------------------------------------------*/ |
| 451 |
int fid; /* IN : file identifier */ |
| 452 |
char *pbuf; /* IN : pointer to i/o buffer */ |
| 453 |
unsigned int nochar; /* IN : buffer size */ |
| 454 |
{ |
| 455 |
register int i, len; |
| 456 |
register char *p, *pe; |
| 457 |
|
| 458 |
if (getfile(fid) < 0) FINISH; |
| 459 |
|
| 460 |
p = pbuf, pe = p + nochar; |
| 461 |
if (nochar == 0) FINISH; /* Read 0 bytes ... */ |
| 462 |
|
| 463 |
if (chan) { |
| 464 |
if (fap->fab$l_fop & FAB$M_UFO) { |
| 465 |
len = vmsqio (chan, IO$_READVBLK, pbuf, nochar, fp->rab$l_bkt); |
| 466 |
if (len <= 0) FINISH; |
| 467 |
fp->rab$l_bkt += (len + 511)>>9; |
| 468 |
} |
| 469 |
else len = vmsrlb(chan, pbuf, nochar); |
| 470 |
p += len; |
| 471 |
FINISH; |
| 472 |
} |
| 473 |
|
| 474 |
/* Get first Data existing in Buffer */ |
| 475 |
if ((b->block*fap->fab$w_bls + b->offset) >= b->file_size) |
| 476 |
FINISH; /* Beyond EOF */ |
| 477 |
|
| 478 |
if (b->bytes > b->offset) |
| 479 |
{ len = MIN(nochar, b->bytes - b->offset); |
| 480 |
move_len = len, LIB$MOVC3(&move_len, b->text + b->offset, p); |
| 481 |
b->offset += len, p += len; |
| 482 |
if (len == nochar) FINISH; |
| 483 |
} |
| 484 |
|
| 485 |
/* Read Now Directly into user's buffer */ |
| 486 |
if (b->buffer_modified) if (oswb() < 0) FINISH; |
| 487 |
if ((b->block*fap->fab$w_bls + b->offset) >= b->file_size) |
| 488 |
FINISH; /* Beyond EOF */ |
| 489 |
b->block += (b->offset / fap->fab$w_bls); |
| 490 |
b->bytes = 0, b->offset = 0; |
| 491 |
|
| 492 |
while( (pe - p) >= b->size) |
| 493 |
{ len = MIN((pe-p),IO_MAX) / b->size; |
| 494 |
len *= b->size; |
| 495 |
i = vmsread(p, len); |
| 496 |
if (i <= 0) FINISH; |
| 497 |
b->block += (i / fap->fab$w_bls); |
| 498 |
p += i; |
| 499 |
if (i < len) /* EOF Encountered */ |
| 500 |
{ b->offset = i % fap->fab$w_bls; |
| 501 |
FINISH; |
| 502 |
} |
| 503 |
} |
| 504 |
if (p == pe) FINISH; /* All is read */ |
| 505 |
|
| 506 |
/* Read now one Buffer */ |
| 507 |
if (osrb() <= 0) FINISH; |
| 508 |
|
| 509 |
/* Get remaining bytes from new buffer */ |
| 510 |
len = MIN(pe-p, b->bytes); /* b->offset is zero... */ |
| 511 |
move_len = len, LIB$MOVC3(&move_len, b->text, p); |
| 512 |
b->offset += len, p += len; |
| 513 |
|
| 514 |
FIN: |
| 515 |
if (oserror) len = -1; |
| 516 |
else len = p - pbuf; |
| 517 |
return(len == 0 ? -1 : len); |
| 518 |
} |
| 519 |
|
| 520 |
/*==========================================================================*/ |
| 521 |
int osdputs(fid, pbuf) |
| 522 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 523 |
.PURPOSE Synchronous write to a file a string of characters followed |
| 524 |
by a newline (end of record). |
| 525 |
The argument fid is the file identification obtained from osdopen. |
| 526 |
.RETURNS Number of characters actually written / -1 for error |
| 527 |
(oserror provides an explanation) |
| 528 |
.REMARKS |
| 529 |
------------------------------------------------------------*/ |
| 530 |
int fid; /* IN : file identifier */ |
| 531 |
char *pbuf; /* IN : EOS-terminated string to write */ |
| 532 |
{ |
| 533 |
register char *p; |
| 534 |
register int i; |
| 535 |
|
| 536 |
i = (int) strlen(pbuf); |
| 537 |
p = pbuf + i++; |
| 538 |
*p = '\n'; |
| 539 |
|
| 540 |
i = osdwrite(fid, pbuf, i); |
| 541 |
*p = EOS; |
| 542 |
|
| 543 |
FIN: |
| 544 |
return (oserror ? -1 : i); |
| 545 |
} |
| 546 |
|
| 547 |
/*==========================================================================*/ |
| 548 |
int osdwrite(fid, pbuf, nochar) |
| 549 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 550 |
.PURPOSE Synchronous write to a disk file. |
| 551 |
The argument fid is the file identification obtained from osdopen. |
| 552 |
The routine writes nochar bytes from the buffer pointed |
| 553 |
by pbuf to file, at the current byte position. |
| 554 |
.RETURNS Number of characters actually written. |
| 555 |
-1 for error (oserror provides an explanation) |
| 556 |
------------------------------------------------------------*/ |
| 557 |
int fid; /* IN : file identifier */ |
| 558 |
char *pbuf; /* IN : pointer to i/o buffer */ |
| 559 |
unsigned int nochar; /* IN : number of i/o characters */ |
| 560 |
{ |
| 561 |
register int len; |
| 562 |
char *p, *pe, *p1; |
| 563 |
unsigned long fsize; |
| 564 |
|
| 565 |
if (nochar == 0) FINISH; |
| 566 |
if (getfile(fid) < 0) FINISH; |
| 567 |
|
| 568 |
p = pbuf, pe = pbuf+nochar, len = 0; |
| 569 |
|
| 570 |
/* Take care first which system service to use */ |
| 571 |
if (chan) { /* Output via QIO */ |
| 572 |
if (fap->fab$l_fop & FAB$M_UFO) { |
| 573 |
len = vmsqio (chan, IO$_WRITEVBLK, pbuf, nochar, fp->rab$l_bkt); |
| 574 |
if (len <= 0) FINISH; |
| 575 |
fp->rab$l_bkt += (len + 511)>>9; |
| 576 |
} |
| 577 |
else if (fid > 2) len = vmsqio(chan, IO$_WRITELBLK, pbuf, len); |
| 578 |
else while (p < pe) { /* Map \n to \r\n */ |
| 579 |
p1 = p + 1; |
| 580 |
if (p1 < pe) p1 += oscloc(p1, pe-p1, '\n'); |
| 581 |
if (p1 < pe) |
| 582 |
*p1 = '\r', |
| 583 |
len += vmsqio(chan, IO$_WRITELBLK, p, (p1-p)+1), |
| 584 |
*p1 = '\n'; |
| 585 |
else p1 = pe, |
| 586 |
len += vmsqio(chan, IO$_WRITELBLK, p, (p1-p)); |
| 587 |
p = p1; |
| 588 |
} |
| 589 |
FINISH; |
| 590 |
} |
| 591 |
|
| 592 |
/* Buffered File: move data first into Buffer */ |
| 593 |
len = MIN((pe-p), b->size - b->offset); |
| 594 |
if (len) |
| 595 |
{ move_len = len, LIB$MOVC3(&move_len, p, b->text + b->offset); |
| 596 |
p += len, b->offset += len; |
| 597 |
} |
| 598 |
|
| 599 |
if (b->bytes < b->offset) b->bytes = b->offset; |
| 600 |
if (p > pbuf) b->buffer_modified = 1; |
| 601 |
if (pe == p) goto FLUSH; /* All is written */ |
| 602 |
|
| 603 |
/* ... write the filled buffer ... */ |
| 604 |
if (b->offset == b->size) |
| 605 |
if (oswb() < 0) FINISH; |
| 606 |
|
| 607 |
b->block += (b->bytes / fap->fab$w_bls); |
| 608 |
b->bytes = 0, b->offset = 0; |
| 609 |
|
| 610 |
/* ... Write Directly to File ... */ |
| 611 |
for (; (pe-p) >= b->size; p += len) |
| 612 |
{ len = MIN((pe-p), IO_MAX) / b->size; |
| 613 |
len *= b->size; |
| 614 |
len = vmswrite(p, len); |
| 615 |
if (len < 0) FINISH; |
| 616 |
b->block += (len / fap->fab$w_bls); |
| 617 |
} |
| 618 |
|
| 619 |
if (pe == p) goto FLUSH; /* All is written */ |
| 620 |
|
| 621 |
/* ... copy rest to empty Buffer ... */ |
| 622 |
if (osrb() < 0) FINISH; |
| 623 |
b->offset = pe - p; |
| 624 |
move_len = b->offset, LIB$MOVC3(&move_len, p, b->text); |
| 625 |
p += b->offset; |
| 626 |
if (b->bytes < b->offset) b->bytes = b->offset; |
| 627 |
if (b->offset) b->buffer_modified = 1; |
| 628 |
|
| 629 |
FLUSH: /* Rewrite buffer to file */ |
| 630 |
fsize = b->block*fap->fab$w_bls + b->bytes; |
| 631 |
if (b->file_size < fsize) b->file_size = fsize; |
| 632 |
len = p - pbuf; |
| 633 |
if (b->flush) osdwait(fid); |
| 634 |
|
| 635 |
FIN: |
| 636 |
return (oserror ? -1 : len); |
| 637 |
} |
| 638 |
|
| 639 |
/*==========================================================================*/ |
| 640 |
long osdseek(fid, address, mode) |
| 641 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 642 |
.PURPOSE Moves read/write pointer to a specified byte position. |
| 643 |
Bytes are numbered from 0 at the file beginning. |
| 644 |
The address is a displacement in BYTES from |
| 645 |
start of the file, current position or end of the file as |
| 646 |
defined by the variable mode (FILE_START, FILE_CURRENT, FILE_END). |
| 647 |
The argument fid is the file identification obtained from osdopen. |
| 648 |
.RETURNS Upon successful completion, the new byte position; |
| 649 |
-1 on error (oserror explains the error). |
| 650 |
------------------------------------------------------------*/ |
| 651 |
int fid; /* IN : file identifier */ |
| 652 |
long address; /* IN : Move in Bytes */ |
| 653 |
int mode; /* IN : mode of addressing */ |
| 654 |
{ |
| 655 |
long pos, sector, posf; |
| 656 |
|
| 657 |
if (getfile(fid) < 0) FINISH; |
| 658 |
|
| 659 |
if (chan) { oserror = ESPIPE; FINISH;} |
| 660 |
|
| 661 |
/* Compute present position for Buffered File */ |
| 662 |
|
| 663 |
switch (mode) |
| 664 |
{ case FILE_CURRENT : /* seek to file_current + address */ |
| 665 |
pos = b->block*fap->fab$w_bls + b->offset; |
| 666 |
if (address == 0) FINISH; |
| 667 |
pos += address; /* Absolute address, continue */ |
| 668 |
break; |
| 669 |
case FILE_START : /* seek to file_start + address */ |
| 670 |
pos = address; |
| 671 |
break; |
| 672 |
case FILE_END : /* seek to file_end + address */ |
| 673 |
pos = b->file_size + address; |
| 674 |
break; |
| 675 |
default: |
| 676 |
oserror = EINVAL; |
| 677 |
break; |
| 678 |
} |
| 679 |
|
| 680 |
if (oserror) FINISH; |
| 681 |
if (pos < 0) { oserror = ESPIPE; FINISH;} |
| 682 |
sector = pos / fap->fab$w_bls; |
| 683 |
posf = pos - b->block * fap->fab$w_bls; /* Offset in Buffer */ |
| 684 |
|
| 685 |
/* Test block is loaded */ |
| 686 |
if (( posf >= 0) && (posf <= b->bytes)) |
| 687 |
{ b->offset = posf; |
| 688 |
FINISH; |
| 689 |
} |
| 690 |
|
| 691 |
/* ... Must set position to a new sector ... */ |
| 692 |
if (b->buffer_modified) |
| 693 |
if (oswb() < 0) FINISH; /* Rewrite first the Sector... */ |
| 694 |
b->offset = 0, b->bytes = 0; |
| 695 |
b->block = sector; /* Position to sector ... */ |
| 696 |
posf = pos - sector*fap->fab$w_bls; /* Fraction of sector */ |
| 697 |
if (posf == 0) FINISH; /* ... if at boundary, it's OK */ |
| 698 |
if (osrb() < 0) FINISH; /* ... read the correct sector */ |
| 699 |
b->offset = posf; |
| 700 |
|
| 701 |
FIN: |
| 702 |
return (oserror ? -1 : pos); |
| 703 |
} |
| 704 |
|
| 705 |
/*==========================================================================*/ |
| 706 |
long osdsize(fid) |
| 707 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 708 |
.PURPOSE Tells the size of an opened file without moving the file pointer. |
| 709 |
.RETURNS Upon successful completion, the file size; |
| 710 |
-1 on error (oserror explains the error). |
| 711 |
.REMARKS |
| 712 |
------------------------------------------------------------*/ |
| 713 |
int fid; /* IN : file identifier */ |
| 714 |
{ |
| 715 |
if (getfile(fid) < 0) FINISH; |
| 716 |
|
| 717 |
if (chan) { oserror = ESPIPE; FINISH;} |
| 718 |
|
| 719 |
FIN: |
| 720 |
return (oserror ? -1 : b->file_size); |
| 721 |
} |
| 722 |
|
| 723 |
/*==========================================================================*/ |
| 724 |
int osdunix(fid) |
| 725 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 726 |
.PURPOSE Tells if the opened file is Unix-compatible |
| 727 |
.RETURNS 0 if non-unix, 1 if Unix, -1 on error (oserror explains the error). |
| 728 |
.REMARKS VMS only |
| 729 |
------------------------------------------------------------*/ |
| 730 |
int fid; /* IN : file identifier */ |
| 731 |
{ |
| 732 |
register int isunix; |
| 733 |
|
| 734 |
if (getfile(fid) < 0) return(-1); |
| 735 |
|
| 736 |
return ((fap->fab$b_rfm == FAB$C_STMLF) && (fap->fab$b_org = FAB$C_SEQ)); |
| 737 |
} |