Debian Codegraph @

libsrc/os/vms/osa.c

raw
1 /* @(#)osa.c 19.1 (ES0-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 Massachusetss Ave, Cambridge,
18 MA 02139, USA.
19
20 Corresponding 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 osa
32 .LANGUAGE C
33 .AUTHOR IPG-ESO Garching
34 .CATEGORY Host operating system interfaces. Record i/o on disk files.
35 .ENVIRONMENT VAX / VMS
36 .COMMENTS Handling of RECORD io's.
37 All files are referenced
38 by the physical name in the open function. A file identifier
39 (number)
40 is returned by open functions to be used in later i/o operations.
41 \begin{TeX}
42
43 The routines return always a non-negative integer number on
44 successful return.
45 Otherwise, a value of -1 is set to indicate an error
46 condition or the End of File; the variable ``oserror''
47 contains the numeric error code ($0$ for EOF),
48 which can be converted to a character pointer via osmsg routine.
49
50 Access to permanent (terminal) files ia made via QIO's.
51 \end{TeX}
52
53 .VERSION 0.0 25-Aug-1986 Definition J. D. Ponz
54 .VERSION 1.6 22-Oct-1987 Written for VMS. Merge with UNIX.
55 Tested on Ultrix. (F. Ochsenbein)
56 .VERSION 2.0 13-Oct-1988 Compatibility.
57 .VERSION 2.1 24-Aug-1990 Small simplifications. (F. Ochsenbein)
58 ------------------------------------------------------------*/
59
60 #define DEBUG 0
61
62 #include <osdefos.h>
63 #include <osfile.h>
64 #include <macrogen.h>
65 #include <atype.h>
66 #include <errno.h> /* System */
67 #include <iodef.h> /* System */
68 # define fab$l_fna name
69 # define fab$b_fns lname
70 # define rab$l_ctx last_op /* 1 if $FIND */
71 # define fab$l_ctx file_no
72 #include <rms.h> /* System */
73 #include <ssdef.h> /* System */
74 #include <devdef.h> /* System */
75
76 MID_EXTERN vmserror; /* VMS-specific error code */
77 MID_EXTERN int oserror;
78 MID_EXTERN char *oserrmsg;
79
80 #define FINISH goto FIN
81
82 /* Variables shared in local subroutines */
83
84 static struct FAB *fap;
85 static struct XABFHC *fhp;
86 static struct RAB *fp;
87 static int TheFile = 0;
88
89 static unsigned short old_rfa[3]; /* record's file address */
90 static unsigned short chan;
91
92 #define pos_to_rfa(pos) fp->rab$w_rfa[2] = pos % fap->fab$w_bls,\
93 *(long *)(fp->rab$w_rfa) = 1 + pos / fap->fab$w_bls
94 struct RAB *vmsrab();
95 long vmsize();
96
97
98 /****************************
99 * General Utility Routines *
100 ****************************/
101
102 /*==========================================================================*/
103 static int getfile(fid)
104 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105 .PURPOSE Retrieves the file pointer.
106 .RETURNS Number of file; the static fp (file pointer) is also defined.
107 ------------------------------------------------------------*/
108 int fid; /* IN : file identifier */
109 {
110 if (!(fp = vmsrab(fid))) FINISH;
111
112 fap = fp->rab$l_fab;
113 fhp = (struct XABFHC *)(fap->fab$l_xab);
114 TheFile = fap->file_no;
115
116 if (fp->rab$l_ctx == 0xffffffff)
117 chan = fp->rab$w_isi;
118 else chan = 0;
119
120 FIN:
121 return ( oserror ? -1 : fid );
122 }
123
124 /*==========================================================================
125 Routines dealing with VMS record-oriented i/o
126 *==========================================================================*/
127
128 /*==========================================================================*/
129 static int vmsget(pbuf, nochar)
130 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131 .PURPOSE Interface to SYS$GET
132 .RETURNS Number of characters of the record / -1 for error
133 ------------------------------------------------------------*/
134 char *pbuf; /* IN : pointer to i/o buffer */
135 long nochar; /* IN : number of i/o characters */
136 {
137 register int len;
138 short ba[3];
139
140 fp->rab$l_ubf = pbuf, fp->rab$w_usz = nochar;
141 ba[0] = fp->rab$w_rfa[0], ba[1] = fp->rab$w_rfa[1], ba[2] = fp->rab$w_rfa[2];
142
143 vmserror = SYS$GET(fp);
144 fp->last_op &= ~1; /* Last operation is NOT a find */
145
146 switch(vmserror) {
147 case RMS$_NORMAL: len = fp->rab$w_rsz; /* Length of read block */
148 break;
149 case RMS$_EOF: len = -1; fp->rab$b_rac = RAB$C_SEQ;
150 fp->rab$w_rfa[0] = ba[0],
151 fp->rab$w_rfa[1] = ba[1],
152 fp->rab$w_rfa[2] = ba[2];
153 break;
154 case RMS$_RTB: len = fp->rab$l_stv; /* Actual Record Length */
155 break;
156 default: len = -1; oserror = EIO; FINISH;
157 }
158
159 if (fp->rab$b_rac == RAB$C_RFA) /* Set RFA to NEXT record */
160 { fp->rab$b_rac = RAB$C_SEQ; /* Random access: Must modify RFA */
161 if (fap->fab$b_fac & FAB$M_PUT)
162 if (SYS$FIND(fp) &1)
163 fp->rab$b_rac = RAB$C_RFA,
164 fp->last_op |= 1;
165 }
166
167 FIN:
168 return(len); /* Length of read block */
169 }
170
171 /*==========================================================================*/
172 static int toeof(pos)
173 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
174 .PURPOSE Positions the file to END.
175 .RETURNS Upon successful completion, the file size
176 measured in characters, or -1 on error.
177 .REMARKS The position is the one of the NEXT record.
178 .METHOD SYS$FIND on sequential, then SYS$FIND on random access.
179 ------------------------------------------------------------*/
180 long pos; /* IN: The EOF position */
181 {
182 if ((fap->fab$b_fac & FAB$M_PUT) && ((fp->rab$l_rop & RAB$M_EOF) == 0))
183 { SYS$FLUSH(fp);
184 SYS$DISCONNECT(fp);
185 fp->rab$l_rop |= RAB$M_EOF;
186 SYS$CONNECT(fp);
187 fp->rab$b_rac = RAB$C_SEQ;
188 }
189 else fp->rab$b_rac = RAB$C_RFA;
190 pos_to_rfa(pos);
191 return(0);
192 }
193
194 /*==========================================================================*/
195 static long vmstell()
196 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
197 .PURPOSE Computes the current byte position in the file.
198 .RETURNS Upon successful completion, the resulting pointer position
199 measured in characters, or -1 on error.
200 .REMARKS The position is the one of the NEXT record.
201 .METHOD SYS$FIND on sequential, then SYS$FIND on random access.
202 ------------------------------------------------------------*/
203 {
204 long pos;
205
206 if (*(long *)(fp->rab$w_rfa) == 0) return(0);
207
208 if ((fp->rab$b_rac == RAB$C_SEQ) && !(fp->last_op & 1))
209 { vmserror = SYS$FIND(fp); /* Tells position of NEXT record */
210 if (vmserror&1) /* Randomly resets the pointer */
211 fp->last_op |= 1;
212 }
213 else vmserror = RMS$_NORMAL;
214
215 switch(vmserror)
216 { case RMS$_EOF: pos = vmsize(TheFile);
217 pos_to_rfa(pos);
218 break;
219 case RMS$_NORMAL: pos = (*(long *)(fp->rab$w_rfa))-1;
220 pos = pos * fap->fab$w_bls + fp->rab$w_rfa[2];
221 break;
222 default: oserror = ESPIPE;
223 break;
224 }
225
226 FIN:
227 return(oserror ? -1 : pos);
228 }
229
230 /*==========================================================================*/
231 static int vmsput(pbuf, nochar)
232 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
233 .PURPOSE Interface to SYS$PUT
234 .RETURNS Number of characters actually written.
235 -1 is returned in case of error (error in oserror).
236 .REMARKS Use SYS$UPDATE service to write in the middle of a file...
237 ------------------------------------------------------------*/
238 char *pbuf; /* IN : pointer to i/o buffer */
239 long nochar; /* IN : number of i/o characters */
240 {
241 register int len;
242
243 #if DEBUG
244 printf("rfm is %d, rat is %d\n", fap->fab$b_rfm, fap->fab$b_rat);
245 #endif
246
247 len = nochar;
248
249 if (fp->rab$b_rac == RAB$C_RFA) /* Put within the file */
250 { switch(vmserror = SYS$FIND(fp))
251 { case RMS$_EOF: toeof(vmsize(TheFile));
252 goto Append;
253 case RMS$_NORMAL:
254 break;
255 default: FINISH;
256 }
257 fp->rab$l_rbf = pbuf, fp->rab$w_rsz = nochar;
258 vmserror = SYS$UPDATE(fp);
259 if (vmserror&1) /* This updates the RFA to NEXT record */
260 { fp->rab$b_rac = RAB$C_SEQ;
261 if (SYS$FIND(fp) &1)
262 fp->rab$b_rac = RAB$C_RFA,
263 fp->last_op |= 1;
264 }
265 FINISH;
266 }
267
268 Append:
269 fp->rab$l_rbf = pbuf, fp->rab$w_rsz = nochar;
270 vmserror = SYS$PUT(fp);
271
272 FIN:
273 if (!(vmserror&1)) oserror = EIO;
274 return(oserror ? -1 : len);
275 }
276
277 /*==========================================================================*/
278 static long vmsseek(address, mode)
279 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
280 .PURPOSE moves read/write pointer to text file. The address defines
281 the pointer position in characters relative to:
282 start of the file, current position or end of the file as
283 defined by the variable mode (FILE_START, FILE_CURRENT, FILE_END).
284 .RETURNS Upon successful completion, the resulting pointer position
285 measured in characters is returned or -1 on error.
286 .REMARKS use of File Record Address
287 ------------------------------------------------------------*/
288 long address; /* IN : offset to seek to */
289 int mode; /* IN : mode of addressing */
290 {
291 register long pos;
292
293 switch (mode)
294 { case FILE_CURRENT : /* seek to file_current + address */
295 pos = vmstell();
296 if (address == 0) FINISH;
297 pos += address; /* Absolute address, continue */
298 break;
299 case FILE_START : /* seek to file_start + address */
300 pos = address;
301 break;
302 case FILE_END : /* seek to file_end + address */
303 pos = vmsize(TheFile) + address;
304 break;
305 default:
306 oserror = EINVAL;
307 break;
308 }
309
310 if (oserror) FINISH;
311
312 /* Convert now pos to RFA (Record File Address), and find this
313 * record ... */
314
315 pos_to_rfa(pos);
316 fp->rab$b_rac = RAB$C_RFA; /* First seek randomly... */
317 vmserror = SYS$FIND(fp);
318 if (vmserror & 1) fp->last_op |= 1; /* Last operation = $FIND */
319
320 FIN:
321 fp->rab$l_rop &= ~RAB$M_EOF; /* We're not any more at EOF */
322 /* Note: RMS apparently provides an EOF message when
323 * the RFA is bad... Let's assume that we go to EOF.
324 */
325 switch(vmserror)
326 { case RMS$_EOF: pos = vmsize(TheFile);
327 toeof(pos);
328 break;
329 default: oserror = ESPIPE;
330 break;
331 case RMS$_NORMAL:
332 case 0: break;
333 }
334
335 return( ( oserror ? -1 : pos));
336 }
337
338
339
340 /************************
341 * OSA Routines *
342 ************************/
343
344
345 /*==========================================================================*/
346 int osaopen(phname, mode)
347 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
348 .PURPOSE Opens disk file for read or write,
349 and positions at file beginning (at end for APPEND mode).
350 The variable `mode'
351 defines the way of opening the file as READ_ONLY,
352 WRITE_ONLY, READ_WRITE and APPEND. (see midas/osparms.h).
353 .RETURNS Upon successful completion a file descriptor is returned. -1 is
354 returned on error.
355 .REMARKS File is opened with buffering. The mode is a combination of
356 the following options (defined is osparms):
357 \begin{TeX}
358 \begin{itemize}
359 \item {\em READ WRITE READ\_WRITE APPEND}
360 \item {\em NOATTR} (record without attributes, i.e. without linefeed)
361 \end{itemize}
362 The routine {\em osfop}, if called before {\em osaopen},
363 allows to create non STMLF--format files.
364 \end{TeX}
365 ------------------------------------------------------------*/
366 char *phname; /* IN : physical filename */
367 int mode; /* IN : open mode (see remarks) */
368 {
369 register int i;
370
371 i = vmsopen(phname, mode); /* Call system-dependent part */
372
373 if ((i > 0) && ((mode&3) == READ_WRITE))
374 { getfile(i);
375 if (SYS$FIND(fp) &1)
376 fp->rab$b_rac = RAB$C_RFA,
377 fp->last_op |= 1;
378 }
379
380 return( i);
381 }
382
383 /*==========================================================================*/
384 int osaclose(fid)
385 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
386 .PURPOSE Closes a disk file.
387 The argument fid is the file identification obtained from osdopen.
388 .RETURNS 0 (success) / -1 (failure)
389 .REMARKS
390 ------------------------------------------------------------*/
391 int fid; /* IN : file identifier */
392 {
393
394 oserror = 0;
395
396 vmsclose(fid);
397
398 return(oserror ? -1 : 0);
399 }
400
401 /*==========================================================================*/
402 int osawait(fid)
403 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
404 .PURPOSE Force the physical writing on disk of modified buffers.
405 The argument fid is the file identification obtained from osdopen.
406 .RETURNS 0 (success) / -1 (failure)
407 .REMARKS
408 ------------------------------------------------------------*/
409 int fid; /* IN : file identifier */
410 {
411 if (getfile(fid) < 0) FINISH;
412 if (chan) FINISH;
413
414 vmserror = SYS$FLUSH(fp);
415 if (!(vmserror&1)) oserror = EIO;
416
417 FIN:
418 return(oserror ? -1 : 0);
419 }
420
421 /*==========================================================================*/
422 int osaread(fid, pbuf, nochar)
423 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
424 .PURPOSE Synchronous RECORD read from disk file.
425 The argument fid is the file identification obtained from osdopen.
426 The routine reads 1 record from the current file
427 position and stores up to nochar characters into the buffer
428 pointed by pbuf. If the buffer is too small, the remainder of the
429 record is IGNORED, BUT the function returns the actual record size.
430 The end-of-record character is never returned, but replaced
431 with the end-of-string character. The buffer returned
432 is always null terminated.
433 .RETURNS --- Number of characters actually read, excluding the end-of-string
434 terminator (e.g. 0 for an empty record). This number may be
435 larger than (nobuf-1) in case of truncation.
436 --- -1 if an error is encountered or if the end of file is
437 reached. In case of error, oserror is set to the relevant error
438 number; oserror is set to 0 in case of EOF.
439 .REMARKS
440 ------------------------------------------------------------*/
441 int fid; /* IN : file identifier */
442 char *pbuf; /* OUT : pointer to i/o buffer */
443 unsigned int nochar; /* IN : size of buffer */
444 {
445 register int len;
446
447 len = 0;
448 if (nochar < 2) FINISH;
449
450 if (getfile(fid) < 0) FINISH;
451
452 if (chan) len = vmsrlb(chan, pbuf, nochar-1);
453 else len = vmsget(pbuf, nochar-1);
454
455 if (len < 0) FINISH;
456 *(pbuf + MIN(len,nochar-1)) = EOS;
457
458 FIN:
459 return(oserror ? -1 : len);
460 }
461
462 /*==========================================================================*/
463 int osawrite(fid, pbuf, nochar)
464 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
465 .PURPOSE Synchronous write to a file a string of characters followed
466 by a newline (end of record).
467 The argument fid is the file identification obtained from osdopen.
468 .RETURNS Number of characters actually written
469 -1 for error (oserror provides an explanation)
470 .REMARKS
471 ------------------------------------------------------------*/
472 int fid; /* IN : file identifier */
473 char *pbuf; /* IN : EOS-terminated string to write */
474 unsigned int nochar; /* IN : Number of bytes to write */
475 {
476 register char *p;
477 register int i;
478
479 if (getfile(fid) < 0) FINISH;
480
481 i = nochar;
482 if (i == 0) FINISH;
483
484 p = pbuf + i - 1; /* Suppress last linefeed */
485 if (chan) { /* Output via QIO */
486 i = vmsqio(chan, IO$_WRITELBLK, pbuf, i, 0);
487 if (*p == '\n') i += vmsqio(chan, IO$_WRITELBLK, "\r", 1, 0);
488 }
489 else {
490 if (*(p) == '\n') i--;
491 i = vmsput(pbuf, i);
492 }
493
494 FIN:
495 return(oserror ? -1 : i);
496 }
497
498 /*==========================================================================*/
499 long osaseek(fid, address, mode)
500 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
501 .PURPOSE Moves read/write pointer to a specified byte position.
502 Bytes are numbered from 0 at the file beginning.
503 The address is a displacement in BYTES from
504 start of the file, current position or end of the file as
505 defined by the variable mode (FILE_START, FILE_CURRENT, FILE_END).
506 The argument fid is the file identification obtained from osdopen.
507 .RETURNS Upon successful completion, the resulting sector number;
508 -1 on error (oserror explains the error).
509 .REMARKS
510 ------------------------------------------------------------*/
511 int fid; /* IN : file identifier */
512 long address; /* IN : Move in bytes */
513 int mode; /* IN : mode of addressing */
514 {
515 register int i;
516 register long pos;
517
518 if (getfile(fid) < 0) FINISH;
519
520 if (chan) oserror = ESPIPE;
521 else pos = vmsseek(address, mode);
522
523 FIN:
524 return(oserror ? -1 : pos);
525 }
526
527 /*==========================================================================*/
528 long osasize(fid)
529 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
530 .PURPOSE Tells the size of an opened file without moving the file pointer.
531 .RETURNS Upon successful completion, the file size;
532 -1 on error (oserror explains the error).
533 .REMARKS
534 ------------------------------------------------------------*/
535 int fid; /* IN : file identifier */
536 {
537 return(vmsize(fid));
538 }
539
540 /*==========================================================================*/
541 int osaunix(fid)
542 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
543 .PURPOSE Tells if the opened file is Unix-compatible
544 .RETURNS 0 if non-unix, 1 if Unix, -1 on error (oserror explains the error).
545 .REMARKS VMS only
546 ------------------------------------------------------------*/
547 int fid; /* IN : file identifier */
548 {
549 register int isunix;
550
551 if (getfile(fid) < 0) return(-1);
552
553 return(fap->fab$b_rfm == FAB$C_STMLF ? 1 : 0);
554 }