* vmsutil.c (vms_file_stats_name): Fix incorrect use of st_mtime
[external/binutils.git] / bfd / vmsutil.c
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
20 #include "ansidecl.h"
21 #include "vmsutil.h"
22
23 #ifdef VMS
24 #define __NEW_STARLET 1
25 #include <vms/starlet.h>
26 #include <vms/rms.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>
32 #include <errno.h>
33 #include <vms/descrip.h>
34 #include <string.h>
35 #include <unixlib.h>
36
37 #define MAXPATH 256
38
39 /* Descrip.h doesn't have everything...  */
40 typedef struct fibdef * __fibdef_ptr32 __attribute__ (( mode (SI) ));
41
42 struct dsc$descriptor_fib
43 {
44   unsigned int   fib$l_len;
45   __fibdef_ptr32 fib$l_addr;
46 };
47
48 /* I/O Status Block.  */
49 struct IOSB
50 {
51   unsigned short status, count;
52   unsigned int devdep;
53 };
54
55 static char *tryfile;
56
57 /* Variable length string.  */
58 struct vstring
59 {
60   short length;
61   char string[NAM$C_MAXRSS+1];
62 };
63
64 static char filename_buff [MAXPATH];
65 static char vms_filespec [MAXPATH];
66
67 /* Callback function for filespec style conversion.  */
68
69 static int
70 translate_unix (char *name, int type ATTRIBUTE_UNUSED)
71 {
72   strncpy (filename_buff, name, MAXPATH);
73   filename_buff [MAXPATH - 1] = (char) 0;
74   return 0;
75 }
76
77 /* Wrapper for DECC function that converts a Unix filespec
78    to VMS style filespec.  */
79
80 static char *
81 to_vms_file_spec (char *filespec)
82 {
83   strncpy (vms_filespec, "", MAXPATH);
84   decc$to_vms (filespec, translate_unix, 1, 1);
85   strncpy (vms_filespec, filename_buff, MAXPATH);
86
87   vms_filespec [MAXPATH - 1] = (char) 0;
88
89   return vms_filespec;
90 }
91
92 #else
93 #include <sys/stat.h>
94 #include <time.h>
95 #define VMS_EPOCH_OFFSET 35067168000000000LL
96 #define VMS_GRANULARITY_FACTOR 10000000
97 #endif
98
99 /* Return VMS file date, size, format, version given a name.  */
100
101 int
102 vms_file_stats_name (const char *filename,
103                      long long *cdt,
104                      long *siz,
105                      char *rfo,
106                      int *ver)
107 {
108 #ifdef VMS
109   struct FAB fab;
110   struct NAM nam;
111
112   unsigned long long create;
113   FAT recattr;
114   char ascnamebuff [256];
115
116   ATRDEF atrlst[]
117     = {
118       { ATR$S_CREDATE,  ATR$C_CREDATE,  &create },
119       { ATR$S_RECATTR,  ATR$C_RECATTR,  &recattr },
120       { ATR$S_ASCNAME,  ATR$C_ASCNAME,  &ascnamebuff },
121       { 0, 0, 0}
122     };
123
124   FIBDEF fib;
125   struct dsc$descriptor_fib fibdsc = {sizeof (fib), (void *) &fib};
126
127   struct IOSB iosb;
128
129   long status;
130   unsigned short chan;
131
132   struct vstring file;
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};
141
142   if (strcmp (filename, "<internal>") == 0
143       || strcmp (filename, "<built-in>") == 0)
144     {
145       if (cdt)
146         *cdt = 0;
147
148       if (siz)
149         *siz = 0;
150
151       if (rfo)
152         *rfo = 0;
153
154       if (ver)
155         *ver = 0;
156
157       return 0;
158     }
159
160   tryfile = to_vms_file_spec ((char *) filename);
161
162   /* Allocate and initialize a FAB and NAM structures.  */
163   fab = cc$rms_fab;
164   nam = cc$rms_nam;
165
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;
173
174   /* Validate filespec syntax and device existence.  */
175   status = SYS$PARSE (&fab, 0, 0);
176   if ((status & 1) != 1)
177     return 1;
178
179   file.string[nam.nam$b_esl] = 0;
180
181   /* Find matching filespec.  */
182   status = SYS$SEARCH (&fab, 0, 0);
183   if ((status & 1) != 1)
184     return 1;
185
186   file.string[nam.nam$b_esl] = 0;
187   result.string[result.length=nam.nam$b_rsl] = 0;
188
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;
192   chan = 0;
193   status = SYS$ASSIGN (&devicedsc, &chan, 0, 0, 0);
194   if ((status & 1) != 1)
195     return 1;
196
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];
202   fib.fib$l_acctl = 0;
203   fib.fib$l_wcc = 0;
204   strcpy (file.string, (strrchr (result.string, ']') + 1));
205   filedsc.dsc$w_length = strlen (file.string);
206   result.string[result.length = 0] = 0;
207
208   /* Open and close the file to fill in the attributes.  */
209   status
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)
213     return 1;
214   if ((iosb.status & 1) != 1)
215     return 1;
216
217   result.string[result.length] = 0;
218   status = SYS$QIOW (0, chan, IO$_DEACCESS, &iosb, 0, 0, &fibdsc, 0, 0, 0,
219                      &atrlst, 0);
220   if ((status & 1) != 1)
221     return 1;
222   if ((iosb.status & 1) != 1)
223     return 1;
224
225   /* Deassign the channel and exit.  */
226   status = SYS$DASSGN (chan);
227   if ((status & 1) != 1)
228     return 1;
229
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);
236
237   return 0;
238 #else
239   struct stat buff;
240
241   if ((stat (filename, &buff)) != 0)
242      return 1;
243
244   if (cdt)
245     {
246       *cdt = (long long) (buff.st_mtime * VMS_GRANULARITY_FACTOR)
247                          + VMS_EPOCH_OFFSET;
248     }
249
250   if (siz)
251     *siz = buff.st_size;
252
253   if (rfo)
254     *rfo = 2; /* Stream LF format.  */
255
256   if (ver)
257     *ver = 0;
258
259   return 0;
260 #endif
261 }
262