Fix a bug where a variable could be used without being initialized in W32.
[make.git] / vmsfunctions.c
blob3acf54c4d798b61fdef6ca9abf123124d07ec7e4
1 /* vmsfunctions.c */
3 #include "make.h"
4 #include "debug.h"
5 #include "job.h"
7 #ifdef __DECC
8 #include <starlet.h>
9 #endif
10 #include <descrip.h>
11 #include <rms.h>
12 #include <iodef.h>
13 #include <atrdef.h>
14 #include <fibdef.h>
15 #include "vmsdir.h"
17 #ifdef HAVE_VMSDIR_H
19 DIR *
20 opendir (char *dspec)
22 struct DIR *dir = (struct DIR *)xmalloc (sizeof (struct DIR));
23 struct NAM *dnam = (struct NAM *)xmalloc (sizeof (struct NAM));
24 struct FAB *dfab = &dir->fab;
25 char *searchspec = (char *)xmalloc (MAXNAMLEN + 1);
27 memset (dir, 0, sizeof *dir);
29 *dfab = cc$rms_fab;
30 *dnam = cc$rms_nam;
31 sprintf (searchspec, "%s*.*;", dspec);
33 dfab->fab$l_fna = searchspec;
34 dfab->fab$b_fns = strlen (searchspec);
35 dfab->fab$l_nam = dnam;
37 *dnam = cc$rms_nam;
38 dnam->nam$l_esa = searchspec;
39 dnam->nam$b_ess = MAXNAMLEN;
41 if (! (sys$parse (dfab) & 1))
43 free (dir);
44 free (dnam);
45 free (searchspec);
46 return (NULL);
49 return dir;
52 #define uppercasify(str) \
53 do \
54 { \
55 char *tmp; \
56 for (tmp = (str); *tmp != '\0'; tmp++) \
57 if (islower ((unsigned char)*tmp)) \
58 *tmp = toupper ((unsigned char)*tmp); \
59 } \
60 while (0)
62 struct direct *
63 readdir (DIR *dir)
65 struct FAB *dfab = &dir->fab;
66 struct NAM *dnam = (struct NAM *)(dfab->fab$l_nam);
67 struct direct *dentry = &dir->dir;
68 int i;
70 memset (dentry, 0, sizeof *dentry);
72 dnam->nam$l_rsa = dir->d_result;
73 dnam->nam$b_rss = MAXNAMLEN;
75 DB (DB_VERBOSE, ("."));
77 if (!((i = sys$search (dfab)) & 1))
79 DB (DB_VERBOSE, (_("sys$search failed with %d\n"), i));
80 return (NULL);
83 dentry->d_off = 0;
84 if (dnam->nam$w_fid == 0)
85 dentry->d_fileno = 1;
86 else
87 dentry->d_fileno = dnam->nam$w_fid[0] + (dnam->nam$w_fid[1] << 16);
89 dentry->d_reclen = sizeof (struct direct);
90 dentry->d_namlen = dnam->nam$b_name + dnam->nam$b_type;
91 strncpy (dentry->d_name, dnam->nam$l_name, dentry->d_namlen);
92 dentry->d_name[dentry->d_namlen] = '\0';
94 #ifdef HAVE_CASE_INSENSITIVE_FS
95 uppercasify (dentry->d_name);
96 #endif
98 return (dentry);
102 closedir (DIR *dir)
104 if (dir != NULL)
106 struct FAB *dfab = &dir->fab;
107 struct NAM *dnam = (struct NAM *)(dfab->fab$l_nam);
108 if (dnam != NULL)
109 free (dnam->nam$l_esa);
110 free (dnam);
111 free (dir);
114 return 0;
116 #endif /* compiled for OpenVMS prior to V7.x */
118 char *
119 getwd (char *cwd)
121 static char buf[512];
123 if (cwd)
124 return (getcwd (cwd, 512));
125 else
126 return (getcwd (buf, 512));
130 vms_stat (char *name, struct stat *buf)
132 int status;
133 int i;
135 static struct FAB Fab;
136 static struct NAM Nam;
137 static struct fibdef Fib; /* short fib */
138 static struct dsc$descriptor FibDesc =
139 { sizeof (Fib), DSC$K_DTYPE_Z, DSC$K_CLASS_S, (char *) &Fib };
140 static struct dsc$descriptor_s DevDesc =
141 { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, &Nam.nam$t_dvi[1] };
142 static char EName[NAM$C_MAXRSS];
143 static char RName[NAM$C_MAXRSS];
144 static struct dsc$descriptor_s FileName =
145 { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0 };
146 static struct dsc$descriptor_s string =
147 { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0 };
148 static unsigned long Rdate[2];
149 static unsigned long Cdate[2];
150 static struct atrdef Atr[] =
152 #if defined(VAX)
153 /* Revision date */
154 { sizeof (Rdate), ATR$C_REVDATE, (unsigned int) &Rdate[0] },
155 /* Creation date */
156 { sizeof (Cdate), ATR$C_CREDATE, (unsigned int) &Cdate[0] },
157 #else
158 /* Revision date */
159 { sizeof (Rdate), ATR$C_REVDATE, &Rdate[0] },
160 /* Creation date */
161 { sizeof (Cdate), ATR$C_CREDATE, &Cdate[0]},
162 #endif
163 { 0, 0, 0 }
165 static short int DevChan;
166 static short int iosb[4];
168 name = vmsify (name, 0);
170 /* initialize RMS structures, we need a NAM to retrieve the FID */
171 Fab = cc$rms_fab;
172 Fab.fab$l_fna = name; /* name of file */
173 Fab.fab$b_fns = strlen (name);
174 Fab.fab$l_nam = &Nam; /* FAB has an associated NAM */
176 Nam = cc$rms_nam;
177 Nam.nam$l_esa = EName; /* expanded filename */
178 Nam.nam$b_ess = sizeof (EName);
179 Nam.nam$l_rsa = RName; /* resultant filename */
180 Nam.nam$b_rss = sizeof (RName);
182 /* do $PARSE and $SEARCH here */
183 status = sys$parse (&Fab);
184 if (!(status & 1))
185 return -1;
187 DevDesc.dsc$w_length = Nam.nam$t_dvi[0];
188 status = sys$assign (&DevDesc, &DevChan, 0, 0);
189 if (!(status & 1))
190 return -1;
192 FileName.dsc$a_pointer = Nam.nam$l_name;
193 FileName.dsc$w_length = Nam.nam$b_name + Nam.nam$b_type + Nam.nam$b_ver;
195 /* Initialize the FIB */
196 for (i = 0; i < 3; i++)
198 #ifndef __VAXC
199 Fib.fib$w_fid[i] = Nam.nam$w_fid[i];
200 Fib.fib$w_did[i] = Nam.nam$w_did[i];
201 #else
202 Fib.fib$r_fid_overlay.fib$w_fid[i] = Nam.nam$w_fid[i];
203 Fib.fib$r_did_overlay.fib$w_did[i] = Nam.nam$w_did[i];
204 #endif
207 status = sys$qiow (0, DevChan, IO$_ACCESS, &iosb, 0, 0,
208 &FibDesc, &FileName, 0, 0, &Atr, 0);
209 sys$dassgn (DevChan);
210 if (!(status & 1))
211 return -1;
212 status = iosb[0];
213 if (!(status & 1))
214 return -1;
216 status = stat (name, buf);
217 if (status)
218 return -1;
220 buf->st_mtime = ((Rdate[0] >> 24) & 0xff) + ((Rdate[1] << 8) & 0xffffff00);
221 buf->st_ctime = ((Cdate[0] >> 24) & 0xff) + ((Cdate[1] << 8) & 0xffffff00);
223 return 0;
226 char *
227 cvt_time (unsigned long tval)
229 static long int date[2];
230 static char str[27];
231 static struct dsc$descriptor date_str =
232 { 26, DSC$K_DTYPE_T, DSC$K_CLASS_S, str };
234 date[0] = (tval & 0xff) << 24;
235 date[1] = ((tval >> 8) & 0xffffff);
237 if ((date[0] == 0) && (date[1] == 0))
238 return ("never");
240 sys$asctim (0, &date_str, date, 0);
241 str[26] = '\0';
243 return (str);
247 strcmpi (const char *s1, const char *s2)
249 while (*s1 != '\0' && toupper(*s1) == toupper(*s2))
251 s1++;
252 s2++;
255 return toupper(*(unsigned char *) s1) - toupper(*(unsigned char *) s2);