Debian Codegraph @

libsrc/os/vms/osf.c

raw
1 /* @(#)osf.c 19.1 (ESO-DMD) 02/25/03 13:56:12 */
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 osf
32 .LANGUAGE C
33 .AUTHOR IPG-ESO Garching
34 .ENVIRONMENT VAX / VMS Version
35 .CATEGORY Host operating system interfaces. File management.
36 .COMMENTS
37 File management. These routines provide miscellaneous operations,
38 on files and directories. The routines use the physical file name
39 to access files.
40 The routines return always
41 a non-negative integer number on successful return.
42 Otherwise, a value of -1 is set to indicate an error
43 condition and the variable ``oserror'' contains the
44 symbolic error code.
45
46 .VERSION 0.0 25-Aug-1986 Definition J. D. Ponz
47 .VERSION 2.0 13-Oct-1988 F. Ochsenbein. Removed logical translations,
48 which do not deal only with files.
49 Translation of logical names now as oshenv.
50 Also, osfcreate FAILS if the file already exists!!
51 Use osdopen with write option to rewrite / create a file...
52 .VERSION 2.1 23-Aug-1990 F. Ochsenbein. Files are created with RAT = CR.
53 Take care of non-sequential files...
54 .VERSION 2.2 17-Oct-1990: Removed GNU-nonunderstandable code...
55 .VERSION 2.3 19-Feb-1992: osfinfo: filestatus modified. CG.
56 .VERSION 2.4 20-Aug-1992: osfinfo: filestatus modified. blocksize dropped.CG
57
58 020201 last modif
59
60 -----------------------------------------------*/
61
62 #define DEBUG 0 /* Set to 1 for Debugging Option */
63
64 #define SECTOR_SIZE 512
65
66 #include <osdefos.h>
67 #include <osfile.h>
68 #include <macrogen.h>
69 #include <atype.h>
70 #include <errno.h> /* System */
71 #include <iodef.h> /* System */
72 #include <stdlib.h> /* System */
73 # define fab$l_fna name
74 # define fab$b_fns lname
75 # define rab$l_ctx buffer
76 # define fab$l_ctx file_no
77 # define TheSector(n) (n-1)
78 # define FileSector(n) (n+1)
79 #include <rms.h> /* System */
80 #include <ssdef.h> /* System */
81 #include <devdef.h> /* System */
82
83 MID_EXTERN 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 fay, *fap;
90 static struct RAB *fp;
91 static struct XABFHC *fhp; /* File Header Characteristics */
92 static struct XABPRO *pro; /* Protections */
93 static struct XABRDT *rdt; /* Revision Date & Time */
94
95 static unsigned short zero = 0;
96 char *osmmget();
97
98 /*==========================================================================
99 * Protection Code Changes
100 *==========================================================================*/
101
102 /*==========================================================================*/
103 static int u_v (u)
104 /*++++++++
105 .PURPOSE Converts protection code from Unix to VMS.
106 .RETURNS Unix-like protection code.
107 .METHOD The VMS protection field is made of 4 hexadecimal digits,
108 edited as `WGOS'; each hexa digit contains bits `dewr' set to 1
109 to DENY access.
110 UNIX uses 3 octal digits `OGW', with bits `rwe' set to 1 to
111 PERMIT access.
112 ------------------------------------------------------------*/
113 int u; /* IN: UNIX protection code */
114 {
115 register int v, i, k;
116
117 /* Process World, Group, Owner */
118 for (i=3, k=u, v=0; --i>=0; k >>= 3, v <<= 4)
119 { v |= ( k & 2 ? 0 :10); /* WD permission */
120 v |= ( k & 4 ? 0 : 1); /* Read permission */
121 v |= ( k & 1 ? 0 : 4); /* Exec permission */
122 }
123
124 /* Add System = Owner */
125 v |= (v >> 12);
126
127 #if DEBUG
128 printf("\nConverted prot. code %3o (Unix) to %4X (VMS)\n", u, v);
129 #endif
130
131 return(v);
132 }
133
134 /*==========================================================================*/
135 static int v_u (v)
136 /*++++++++
137 .PURPOSE Converts protection code from VMS to Unix.
138 .RETURNS VMS protection code.
139 .METHOD The VMS protection field is made of 4 hexadecimal digits,
140 edited as `WGOS'; each hexa digit contains bits `dewr' set to 1
141 to DENY access.
142 UNIX uses 3 octal digits `OGW', with bits `rwe' set to 1 to
143 PERMIT access.
144 ------------------------------------------------------------*/
145 int v; /* IN: VMS protection code */
146 {
147 register int u, i, k;
148
149 k = v >> 4; /* Suppress system protections */
150
151 /* Process World, Group, Owner */
152 for (i=3, u=0; --i>=0; k >>= 4)
153 { u <<= 3;
154 u |= ( k & 2 ? 0 : 2); /* Write permission */
155 u |= ( k & 4 ? 0 : 1); /* Read permission */
156 u |= ( k & 1 ? 0 : 4); /* Exec permission */
157 }
158
159 #if DEBUG
160 printf("\nConverted prot. code %4X (VMS) to %3o (Unix)\n", v, u);
161 #endif
162
163 return(u);
164 }
165
166 /*======================================================================*/
167 static long vmstime(q)
168 /*+++++++++++++
169 .PURPOSE Converts the VAX time to standard
170 .RETURNS Time as seconds elapsed since Jan. 1, 1970
171 .REMARKS The VAX time is 64 bits; origin = Nov. 17, 1858; units = 100ns.
172 (40587 days before Jan 1, 1970)
173 --------------*/
174 unsigned long *q; /* IN: VAX time */
175 {
176 register double tt;
177 register long t;
178
179 tt = q[1] * 4294967296e0;
180 tt = tt + q[0];
181 tt = tt / 1.e7 - (40587.e0 * 86400.e0);
182
183 t = tt;
184
185 return(t);
186 }
187
188 /*======================================================================*/
189 static int finit (phname)
190 /*++++++++
191 .PURPOSE Initializes the RMS blocks
192 .RETURNS 0
193 ------------------------------------------------------------*/
194 char *phname; /* IN : physical filename */
195 {
196 struct RAB *vmsinit();
197
198 fp = vmsinit(phname);
199 fap = fp->rab$l_fab;
200 fap->fab$b_fac = FAB$M_BRO|FAB$M_PUT;
201 fhp = (struct XABFHC *)fap->fab$l_xab; /* File Header Characteristics */
202 pro = (struct XABPRO *)fhp->xab$l_nxt; /* Next block = protection */
203 rdt = (struct XABRDT *)pro->xab$l_nxt; /* Next block = date/time */
204
205 return(0);
206 }
207
208 /*==========================================================================
209 * osf Routines
210 *==========================================================================*/
211
212
213 /*==========================================================================*/
214 int osfcreate(phname, nobyt, protec)
215 /*++++++++
216 .PURPOSE Creates a NEW file (STMLF format by default).
217 Access is done by physical file name. Owner and group identifications
218 of the process creating the file define the owner and group
219 identification of the file.
220 The argument protec gives the file protection code, in Unix form.
221 .RETURNS 0 upon successful completion.
222 Else , -1 is returned and the system error code is set into
223 oserror. Note that trying to create a new file with the same
224 name as an existing file is an error.
225 .REMARKS The protection code may also include the NOATTR option.
226
227 The protection code is made of 3 octal digits `OGW' for Owner /
228 Group / World; each octal digit is made of 3 bits `rwe'
229 for read / write (delete) / execute protection set to 1 to
230 PERMIT access. As an example, 0755 allows read / execute
231 to everybody, but rewriting the file is only allowed to the owner.
232 The protection code of 0 converted to default 0755 protection.
233
234 To create non-STMLF files, use osfop before oscfreate.
235 ----------------------------------*/
236 char *phname; /* IN : physical filename */
237 long nobyt; /* IN : number of bytes to be allocated */
238 int protec; /* IN : protection code `OGW' (3 octal digits) \
239 and NOATTR option */
240 {
241 register int i;
242 MID_STATIC char c = '\n'; /* Character written in file*/
243
244 oserror = 0;
245 if (nobyt < 0) nobyt = 0;
246 if (protec == 0) protec = 0755;
247
248 finit(phname);
249 if (protec & NOATTR) ;
250 else fap->fab$b_rat = FAB$M_CR; /* Create file With RAT */
251 i = (nobyt + SECTOR_SIZE - 1) / SECTOR_SIZE;
252 /* if (fap->fab$b_org == FAB$C_SEQ)*/ fap->fab$l_alq = i;
253
254 pro->xab$w_pro = u_v(protec); /* Convert Protection Code */
255
256 vmserror = SYS$CREATE(fap); /* Create the file ... */
257 if (vmserror != RMS$_NORMAL) FINISH;
258
259 if (nobyt && (fap->fab$b_org == FAB$C_SEQ)) { /* Write if necessary */
260 vmserror = SYS$CONNECT(fp);
261 if (vmserror != RMS$_NORMAL) FINISH;
262
263 fp->rab$l_bkt = i; /* Last block ... */
264 fp->rab$w_rsz = nobyt - SECTOR_SIZE*(i-1);
265 fp->rab$l_rbf = osmmget(fp->rab$w_rsz);
266 LIB$MOVC5(&zero, 0, &zero, &fp->rab$w_rsz, fp->rab$l_rbf);
267 /* Fill with zeroes */
268 *(fp->rab$l_rbf + fp->rab$w_rsz -1) = c;
269 vmserror = SYS$WRITE(fp);
270 if (vmserror != RMS$_NORMAL) FINISH;
271 osmmfree(fp->rab$l_rbf);
272 }
273 vmserror = SYS$CLOSE(fap); /* Close the file ... */
274 if (vmserror != RMS$_NORMAL) FINISH;
275
276 FIN:
277 if (vmserror != RMS$_NORMAL) oserror = EIO;
278 return( oserror ? -1 : 0);
279 }
280
281 /*==========================================================================*/
282 int osfdelete(phname)
283 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
284 .PURPOSE Deletes file on disk by removing the directory entry corresponding
285 to the physical name. The file is assumed not to be open.
286 .RETURNS Value 0 on normal return / -1 if something wrong happened.
287 ``oserror'' is then set to the system error code.
288 .REMARKS
289 ------------------------------------------------------------*/
290 char *phname; /* IN : physical filename */
291 {
292 oserror = 0;
293
294 finit(phname);
295 vmserror = SYS$ERASE(fap);
296 if (vmserror != RMS$_NORMAL) oserror = ENOENT;
297
298 return(oserror ? -1 : 0);
299 }
300
301 /*==========================================================================*/
302 int osfcontrol(phname, function, value1, value2)
303 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
304 .PURPOSE Change file protection and/or owner. Access to the
305 file is by physical name. The argument function defines the
306 operation to be done, value1 is the protection code (!! in octal)
307 or the owner code and value2 is 0 or the actual group identification.
308 .RETURNS Value 0 on normal return. Value -1 in case of error (sys error
309 number is then returned to oserror).
310 .METHOD UIC on VMS is made of (group<<16 + owner)
311 .REMARKS
312 ------------------------------------------------------------*/
313 char *phname; /* IN : physical filename */
314 int function; /* IN : function code */
315 int value1; /* IN : protection or owner code */
316 int value2; /* IN : 0 or group code */
317 {
318 oserror = 0;
319
320 finit(phname);
321 vmserror = SYS$OPEN(fap); /* Open first the file ... */
322 if (vmserror != RMS$_NORMAL) FINISH;
323
324 switch (function)
325 { case CHMOD : /* change file protection */
326 pro->xab$w_pro = u_v(value1); /* Convert Protection Code */
327 vmserror = SYS$CLOSE(fap); /* and close it ... */
328 break;
329
330 case CHOWN : /* change owner and group of the file */
331 pro->xab$l_uic = value1 + (value2<<16); /* Convert Owner UIC */
332 vmserror = SYS$CLOSE(fap); /* and close it ... */
333 break;
334
335 default:
336 oserror = EINVAL;
337 break;
338 }
339
340 FIN:
341 if (vmserror != RMS$_NORMAL) oserror = EIO;
342 return(oserror ? -1 : 0);
343 }
344
345 int osfinfo(phname, status)
346 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
347 .PURPOSE Get file status information. Access to the file is by physical name.
348 .RETURNS Value 0 on normal return. (-1 if file does not exist).
349 .REMARKS
350 ------------------------------------------------------------*/
351 char *phname; /* IN : physical filename */
352 struct filestatus *status; /* OUT : output file status */
353 {
354 oserror = 0;
355
356 finit(phname);
357 fap->fab$b_fac = FAB$M_BRO|FAB$M_GET;
358 fap->fab$b_shr = FAB$M_SHRGET;
359 vmserror = SYS$OPEN(fap); /* Open first the file ... */
360 if (vmserror != RMS$_NORMAL) FINISH;
361 status->filesize = (fhp->xab$l_ebk-1)*fap->fab$w_bls + fhp->xab$w_ffb;
362 if (status->filesize < 0) status->filesize = 0;
363 status->date = vmstime(&rdt->xab$q_rdt); /* Convert the date/time */
364 status->owner = pro->xab$l_uic;
365 status->protection = v_u(pro->xab$w_pro); /* Convert Prot. Code */
366 /* status->blocksize = (fap->fab$w_bls ? fap->fab$w_bls : SECTOR_SIZE); */
367 vmserror = SYS$CLOSE(fap); /* and close it ... */
368
369 FIN:
370 if (vmserror != RMS$_NORMAL) oserror = ENOENT;
371
372 return(oserror ? -1 : 0);
373 }
374
375 /*==========================================================================*/
376 long osfdate(phname)
377 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
378 .PURPOSE Get the date of last modification of a file.
379 .RETURNS Date of last modification (-1 if file does not exist).
380 .REMARKS
381 ------------------------------------------------------------*/
382 char *phname; /* IN : physical filename */
383 {
384 long date;
385
386 oserror = 0;
387
388 finit(phname);
389 fap->fab$b_fac = FAB$M_BRO|FAB$M_GET;
390 fap->fab$b_shr = FAB$M_SHRGET;
391 vmserror = SYS$OPEN(fap); /* Open first the file ... */
392 if (vmserror != RMS$_NORMAL) FINISH;
393
394 date = vmstime(&rdt->xab$q_rdt); /* Convert the date/time */
395
396 vmserror = SYS$CLOSE(fap); /* and close it ... */
397
398 FIN:
399 if (vmserror != RMS$_NORMAL) oserror = ENOENT;
400
401 return(oserror ? -1 : date);
402 }
403
404 /*==========================================================================*/
405 long osfsize(phname)
406 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
407 .PURPOSE Get the size of a file.
408 .RETURNS Size of file in bytes (-1 if file does not exist).
409 .REMARKS
410 ------------------------------------------------------------*/
411 char *phname; /* IN : physical filename */
412 {
413 long size;
414
415 oserror = 0;
416
417 finit(phname);
418 fap->fab$b_fac = FAB$M_BRO|FAB$M_GET;
419 fap->fab$b_shr = FAB$M_SHRGET;
420 vmserror = SYS$OPEN(fap); /* Open first the file ... */
421 if (vmserror != RMS$_NORMAL) FINISH;
422
423 size = (fhp->xab$l_ebk-1)*fap->fab$w_bls + fhp->xab$w_ffb;
424 if (size < 0) size = 0;
425
426 vmserror = SYS$CLOSE(fap); /* and close it ... */
427
428 FIN:
429 if (vmserror != RMS$_NORMAL) oserror = ENOENT;
430
431 return(oserror ? -1 : size);
432 }
433
434 /*==========================================================================*/
435 int osfrename(oldname, newname)
436 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
437 .PURPOSE Renames a disk file by giving old and new physical names.
438 .RETURNS Value 0 on normal return. (-1 on error. Oserror is then set to the
439 actual error code).
440 .REMARKS
441 ------------------------------------------------------------*/
442 char *oldname; /* IN : old physical filename */
443 char *newname; /* IN : new physical filename */
444 {
445 oserror = 0;
446
447 finit(oldname);
448 fay = cc$rms_fab;
449 if (newname) /* Add name of file */
450 { fay.fab$b_fns = strlen(newname);/* sets length of file_name */
451 fay.fab$l_fna = newname;
452 }
453 vmserror = SYS$RENAME(fap, 0, 0, &fay);
454
455 FIN:
456 if (vmserror != RMS$_NORMAL) oserror = ENOENT;
457
458 return(oserror ? -1 : 0);
459 }
460
461 int osftranslate(logname, phname)
462 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
463 .PURPOSE Translate logical names into physical names. i.e. $KEYS ==> myfile.key
464 .METHOD Searches the system variable list for the logname.
465 .RETURNS length of physical name on normal return,
466 -1 when logname does not exist (phname empty).
467 .REMARKS System dependencies:
468 --UNIX: getenv(3).
469 ------------------------------------------------------------*/
470 char *logname; /* IN : logical name */
471 char *phname; /* OUT: translated physical filename */
472
473 {
474 register int nr;
475
476 char *getenv();
477 register char *t, *ptr, cc;
478
479
480
481 /* Do the logical translation */
482
483 if ((t = getenv(logname)) == NULL)
484 {
485 *phname = '\0';
486 return(-1);
487 }
488
489 ptr = phname;
490 for (nr=0; ; nr++) /* copy phys. filename */
491 {
492 cc = *t++;
493 if (cc == '\0')
494 {
495 *ptr = '\0';
496 return (nr);
497 }
498 *ptr++ = cc;
499 }
500 }
501
502 int osfphname(logname, phname)
503 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
504 .PURPOSE Translate logical names into physical names. i.e. $KEYS ==> myfile.key
505 .METHOD Searches the system variable list for the logname.
506 .RETURNS Value 0 on normal return, -1 when logname does not exist
507 (phname empty).
508 .REMARKS System dependencies:
509 --UNIX: getenv(3).
510 ------------------------------------------------------------*/
511 char *logname; /* IN : logical name */
512 char *phname; /* OUT: translated physical filename */
513 {
514 char *getenv();
515 register char *t1;
516
517 /*
518 ** Do the logical translation
519 */
520 if ((t1 = getenv(logname)) == NULL){
521 *phname = '\0';
522 return(-1);
523 }
524 strcpy(phname,t1);
525 return(0);
526 }
527
528