2009-07-07 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / bfd / vmsutil.c
CommitLineData
01e1a5bc
NC
1/* vmsutil.c -- Utilities for VMS.
2 Copyright 2009 Free Software Foundation, Inc.
3
4 Written by Douglas B Rupp <rupp@gnat.com>
5
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.
10
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.
15
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. */
19
efde2f2c 20#include "sysdep.h"
01e1a5bc
NC
21#include "vmsutil.h"
22
efde2f2c
NC
23/* The purspose of the two alternate versions below is to have one that
24 works for native VMS and one that works on an NFS mounted filesystem
25 (Unix Server/VMS client). The main issue being to generate the special
26 VMS file timestamps for the debug info. */
27
01e1a5bc
NC
28#ifdef VMS
29#define __NEW_STARLET 1
30#include <vms/starlet.h>
31#include <vms/rms.h>
32#include <vms/atrdef.h>
33#include <vms/fibdef.h>
34#include <vms/stsdef.h>
35#include <vms/iodef.h>
36#include <vms/fatdef.h>
37#include <errno.h>
38#include <vms/descrip.h>
39#include <string.h>
40#include <unixlib.h>
41
42#define MAXPATH 256
43
44/* Descrip.h doesn't have everything... */
45typedef struct fibdef * __fibdef_ptr32 __attribute__ (( mode (SI) ));
46
47struct dsc$descriptor_fib
48{
49 unsigned int fib$l_len;
50 __fibdef_ptr32 fib$l_addr;
51};
52
53/* I/O Status Block. */
54struct IOSB
55{
56 unsigned short status, count;
57 unsigned int devdep;
58};
59
60static char *tryfile;
61
62/* Variable length string. */
63struct vstring
64{
65 short length;
66 char string[NAM$C_MAXRSS+1];
67};
68
69static char filename_buff [MAXPATH];
70static char vms_filespec [MAXPATH];
71
72/* Callback function for filespec style conversion. */
73
74static int
75translate_unix (char *name, int type ATTRIBUTE_UNUSED)
76{
77 strncpy (filename_buff, name, MAXPATH);
78 filename_buff [MAXPATH - 1] = (char) 0;
79 return 0;
80}
81
82/* Wrapper for DECC function that converts a Unix filespec
83 to VMS style filespec. */
84
85static char *
86to_vms_file_spec (char *filespec)
87{
88 strncpy (vms_filespec, "", MAXPATH);
89 decc$to_vms (filespec, translate_unix, 1, 1);
90 strncpy (vms_filespec, filename_buff, MAXPATH);
91
92 vms_filespec [MAXPATH - 1] = (char) 0;
93
94 return vms_filespec;
95}
96
efde2f2c
NC
97#else /* not VMS */
98
99#define _BSD_SOURCE 1
01e1a5bc
NC
100#include <sys/stat.h>
101#include <time.h>
efde2f2c
NC
102
103#define VMS_EPOCH_OFFSET 35067168000000000LL
104#define VMS_GRANULARITY_FACTOR 10000000
105
106#endif /* VMS */
01e1a5bc
NC
107
108/* Return VMS file date, size, format, version given a name. */
109
110int
111vms_file_stats_name (const char *filename,
112 long long *cdt,
113 long *siz,
114 char *rfo,
115 int *ver)
116{
117#ifdef VMS
118 struct FAB fab;
119 struct NAM nam;
120
121 unsigned long long create;
122 FAT recattr;
123 char ascnamebuff [256];
124
125 ATRDEF atrlst[]
126 = {
127 { ATR$S_CREDATE, ATR$C_CREDATE, &create },
128 { ATR$S_RECATTR, ATR$C_RECATTR, &recattr },
129 { ATR$S_ASCNAME, ATR$C_ASCNAME, &ascnamebuff },
130 { 0, 0, 0}
131 };
132
133 FIBDEF fib;
134 struct dsc$descriptor_fib fibdsc = {sizeof (fib), (void *) &fib};
135
136 struct IOSB iosb;
137
138 long status;
139 unsigned short chan;
140
141 struct vstring file;
142 struct dsc$descriptor_s filedsc
143 = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) file.string};
144 struct vstring device;
145 struct dsc$descriptor_s devicedsc
146 = {NAM$C_MAXRSS, DSC$K_DTYPE_T, DSC$K_CLASS_S, (void *) device.string};
147 struct vstring result;
148 struct dsc$descriptor_s resultdsc
149 = {NAM$C_MAXRSS, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, (void *) result.string};
150
151 if (strcmp (filename, "<internal>") == 0
152 || strcmp (filename, "<built-in>") == 0)
153 {
154 if (cdt)
155 *cdt = 0;
156
157 if (siz)
158 *siz = 0;
159
160 if (rfo)
161 *rfo = 0;
162
163 if (ver)
164 *ver = 0;
165
166 return 0;
167 }
168
169 tryfile = to_vms_file_spec ((char *) filename);
170
171 /* Allocate and initialize a FAB and NAM structures. */
172 fab = cc$rms_fab;
173 nam = cc$rms_nam;
174
175 nam.nam$l_esa = file.string;
176 nam.nam$b_ess = NAM$C_MAXRSS;
177 nam.nam$l_rsa = result.string;
178 nam.nam$b_rss = NAM$C_MAXRSS;
179 fab.fab$l_fna = tryfile;
180 fab.fab$b_fns = strlen (tryfile);
181 fab.fab$l_nam = &nam;
182
183 /* Validate filespec syntax and device existence. */
184 status = SYS$PARSE (&fab, 0, 0);
185 if ((status & 1) != 1)
186 return 1;
187
188 file.string[nam.nam$b_esl] = 0;
189
190 /* Find matching filespec. */
191 status = SYS$SEARCH (&fab, 0, 0);
192 if ((status & 1) != 1)
193 return 1;
194
195 file.string[nam.nam$b_esl] = 0;
196 result.string[result.length=nam.nam$b_rsl] = 0;
197
198 /* Get the device name and assign an IO channel. */
199 strncpy (device.string, nam.nam$l_dev, nam.nam$b_dev);
200 devicedsc.dsc$w_length = nam.nam$b_dev;
201 chan = 0;
202 status = SYS$ASSIGN (&devicedsc, &chan, 0, 0, 0);
203 if ((status & 1) != 1)
204 return 1;
205
206 /* Initialize the FIB and fill in the directory id field. */
207 memset (&fib, 0, sizeof (fib));
208 fib.fib$w_did[0] = nam.nam$w_did[0];
209 fib.fib$w_did[1] = nam.nam$w_did[1];
210 fib.fib$w_did[2] = nam.nam$w_did[2];
211 fib.fib$l_acctl = 0;
212 fib.fib$l_wcc = 0;
213 strcpy (file.string, (strrchr (result.string, ']') + 1));
214 filedsc.dsc$w_length = strlen (file.string);
215 result.string[result.length = 0] = 0;
216
217 /* Open and close the file to fill in the attributes. */
218 status
219 = SYS$QIOW (0, chan, IO$_ACCESS|IO$M_ACCESS, &iosb, 0, 0,
220 &fibdsc, &filedsc, &result.length, &resultdsc, &atrlst, 0);
221 if ((status & 1) != 1)
222 return 1;
223 if ((iosb.status & 1) != 1)
224 return 1;
225
226 result.string[result.length] = 0;
227 status = SYS$QIOW (0, chan, IO$_DEACCESS, &iosb, 0, 0, &fibdsc, 0, 0, 0,
228 &atrlst, 0);
229 if ((status & 1) != 1)
230 return 1;
231 if ((iosb.status & 1) != 1)
232 return 1;
233
234 /* Deassign the channel and exit. */
235 status = SYS$DASSGN (chan);
236 if ((status & 1) != 1)
237 return 1;
238
239 if (cdt) *cdt = create;
240 if (siz) *siz = (512 * 65536 * recattr.fat$w_efblkh) +
241 (512 * (recattr.fat$w_efblkl - 1)) +
242 recattr.fat$w_ffbyte;
243 if (rfo) *rfo = recattr.fat$v_rtype;
efde2f2c
NC
244 if (ver) *ver = strtol (strrchr (ascnamebuff, ';') + 1, 0, 10);
245#else /* not VMS */
01e1a5bc 246
01e1a5bc 247 struct stat buff;
efde2f2c
NC
248 struct tm *ts;
249 long long gmtoff, secs, nsecs;
01e1a5bc
NC
250
251 if ((stat (filename, &buff)) != 0)
252 return 1;
253
254 if (cdt)
255 {
efde2f2c
NC
256 ts = localtime (& buff.st_mtime);
257
258#ifdef HAVE_TM_GMTOFF
259 gmtoff = ts->tm_gmtoff;
260#else
261 {
262 extern long timezone;
263
264 if (ts->tm_isdst == 1)
265 gmtoff = - (timezone - 3600);
266 else
267 gmtoff = - timezone;
268 }
269#endif
270
271#ifdef HAVE_ST_MTIM_TV_SEC
272 secs = buff.st_mtim.tv_sec;
273#else
274 secs = buff.st_mtime;
275#endif
276
277#ifdef HAVE_ST_MTIM_TV_NSEC
278 nsecs = buff.st_mtim.tv_nsec;
279#else
280 nsecs = 0;
281#endif
282
283 /* VMS timestamps are stored in local time to 100 nsec accuracy, but by
284 experiment I found timestamps truncated to (at least) microseconds
285 on an NFS mounted filesystem, hence the adjustment below. DBR. */
286 *cdt = ((secs + gmtoff) * VMS_GRANULARITY_FACTOR)
287 + (nsecs / 1000 * 10) + VMS_EPOCH_OFFSET;
01e1a5bc
NC
288 }
289
290 if (siz)
291 *siz = buff.st_size;
292
293 if (rfo)
294 *rfo = 2; /* Stream LF format. */
295
efde2f2c
NC
296 /* Returning a file version of 0 is never correct for debug info, version 1
297 will be correct if file editing is done only on the Unix side. If editing
298 is done on the VMS side, then its TBD. */
01e1a5bc 299 if (ver)
efde2f2c
NC
300 *ver = 1;
301#endif /* VMS */
01e1a5bc
NC
302
303 return 0;
01e1a5bc
NC
304}
305
This page took 0.087431 seconds and 4 git commands to generate.