1 /* vmsutil.c -- Utilities for VMS.
2 Copyright 2009 Free Software Foundation, Inc.
4 Written by Douglas B Rupp <rupp@gnat.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 #define __NEW_STARLET 1
25 #include <vms/starlet.h>
27 #include <vms/atrdef.h>
28 #include <vms/fibdef.h>
29 #include <vms/stsdef.h>
30 #include <vms/iodef.h>
31 #include <vms/fatdef.h>
33 #include <vms/descrip.h>
39 /* Descrip.h doesn't have everything... */
40 typedef struct fibdef
* __fibdef_ptr32
__attribute__ (( mode (SI
) ));
42 struct dsc$descriptor_fib
44 unsigned int fib$l_len
;
45 __fibdef_ptr32 fib$l_addr
;
48 /* I/O Status Block. */
51 unsigned short status
, count
;
57 /* Variable length string. */
61 char string
[NAM$C_MAXRSS
+1];
64 static char filename_buff
[MAXPATH
];
65 static char vms_filespec
[MAXPATH
];
67 /* Callback function for filespec style conversion. */
70 translate_unix (char *name
, int type ATTRIBUTE_UNUSED
)
72 strncpy (filename_buff
, name
, MAXPATH
);
73 filename_buff
[MAXPATH
- 1] = (char) 0;
77 /* Wrapper for DECC function that converts a Unix filespec
78 to VMS style filespec. */
81 to_vms_file_spec (char *filespec
)
83 strncpy (vms_filespec
, "", MAXPATH
);
84 decc$
to_vms (filespec
, translate_unix
, 1, 1);
85 strncpy (vms_filespec
, filename_buff
, MAXPATH
);
87 vms_filespec
[MAXPATH
- 1] = (char) 0;
95 #define VMS_EPOCH_OFFSET 35067168000000000LL
96 #define VMS_GRANULARITY_FACTOR 10000000
99 /* Return VMS file date, size, format, version given a name. */
102 vms_file_stats_name (const char *filename
,
112 unsigned long long create
;
114 char ascnamebuff
[256];
118 { ATR$S_CREDATE
, ATR$C_CREDATE
, &create
},
119 { ATR$S_RECATTR
, ATR$C_RECATTR
, &recattr
},
120 { ATR$S_ASCNAME
, ATR$C_ASCNAME
, &ascnamebuff
},
125 struct dsc$descriptor_fib fibdsc
= {sizeof (fib
), (void *) &fib
};
133 struct dsc$descriptor_s filedsc
134 = {NAM$C_MAXRSS
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, (void *) file
.string
};
135 struct vstring device
;
136 struct dsc$descriptor_s devicedsc
137 = {NAM$C_MAXRSS
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, (void *) device
.string
};
138 struct vstring result
;
139 struct dsc$descriptor_s resultdsc
140 = {NAM$C_MAXRSS
, DSC$K_DTYPE_VT
, DSC$K_CLASS_VS
, (void *) result
.string
};
142 if (strcmp (filename
, "<internal>") == 0
143 || strcmp (filename
, "<built-in>") == 0)
160 tryfile
= to_vms_file_spec ((char *) filename
);
162 /* Allocate and initialize a FAB and NAM structures. */
166 nam
.nam$l_esa
= file
.string
;
167 nam
.nam$b_ess
= NAM$C_MAXRSS
;
168 nam
.nam$l_rsa
= result
.string
;
169 nam
.nam$b_rss
= NAM$C_MAXRSS
;
170 fab
.fab$l_fna
= tryfile
;
171 fab
.fab$b_fns
= strlen (tryfile
);
172 fab
.fab$l_nam
= &nam
;
174 /* Validate filespec syntax and device existence. */
175 status
= SYS$
PARSE (&fab
, 0, 0);
176 if ((status
& 1) != 1)
179 file
.string
[nam
.nam$b_esl
] = 0;
181 /* Find matching filespec. */
182 status
= SYS$
SEARCH (&fab
, 0, 0);
183 if ((status
& 1) != 1)
186 file
.string
[nam
.nam$b_esl
] = 0;
187 result
.string
[result
.length
=nam
.nam$b_rsl
] = 0;
189 /* Get the device name and assign an IO channel. */
190 strncpy (device
.string
, nam
.nam$l_dev
, nam
.nam$b_dev
);
191 devicedsc
.dsc$w_length
= nam
.nam$b_dev
;
193 status
= SYS$
ASSIGN (&devicedsc
, &chan
, 0, 0, 0);
194 if ((status
& 1) != 1)
197 /* Initialize the FIB and fill in the directory id field. */
198 memset (&fib
, 0, sizeof (fib
));
199 fib
.fib$w_did
[0] = nam
.nam$w_did
[0];
200 fib
.fib$w_did
[1] = nam
.nam$w_did
[1];
201 fib
.fib$w_did
[2] = nam
.nam$w_did
[2];
204 strcpy (file
.string
, (strrchr (result
.string
, ']') + 1));
205 filedsc
.dsc$w_length
= strlen (file
.string
);
206 result
.string
[result
.length
= 0] = 0;
208 /* Open and close the file to fill in the attributes. */
210 = SYS$
QIOW (0, chan
, IO$_ACCESS
|IO$M_ACCESS
, &iosb
, 0, 0,
211 &fibdsc
, &filedsc
, &result
.length
, &resultdsc
, &atrlst
, 0);
212 if ((status
& 1) != 1)
214 if ((iosb
.status
& 1) != 1)
217 result
.string
[result
.length
] = 0;
218 status
= SYS$
QIOW (0, chan
, IO$_DEACCESS
, &iosb
, 0, 0, &fibdsc
, 0, 0, 0,
220 if ((status
& 1) != 1)
222 if ((iosb
.status
& 1) != 1)
225 /* Deassign the channel and exit. */
226 status
= SYS$
DASSGN (chan
);
227 if ((status
& 1) != 1)
230 if (cdt
) *cdt
= create
;
231 if (siz
) *siz
= (512 * 65536 * recattr
.fat$w_efblkh
) +
232 (512 * (recattr
.fat$w_efblkl
- 1)) +
233 recattr
.fat$w_ffbyte
;
234 if (rfo
) *rfo
= recattr
.fat$v_rtype
;
235 if (ver
) *ver
= strtol (strrchr (ascnamebuff
, ';')+1, 0, 10);
242 if ((stat (filename
, &buff
)) != 0)
247 ts
= localtime (&buff
.st_mtime
);
248 *cdt
= (long long) ((buff
.st_mtim
.tv_sec
* VMS_GRANULARITY_FACTOR
)
249 + (buff
.st_mtim
.tv_nsec
/ 100))
257 *rfo
= 2; /* Stream LF format. */