1/* vmsfunctions.c */
2
3#include "make.h"
4#include "debug.h"
5
6#ifdef __DECC
7#include <starlet.h>
8#endif
9#include <descrip.h>
10#include <rms.h>
11#include <iodef.h>
12#include <atrdef.h>
13#include <fibdef.h>
14#include "vmsdir.h"
15
16#ifdef HAVE_VMSDIR_H
17
18DIR *
19opendir (dspec)
20     char *dspec;
21{
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);
26
27  memset (dir, 0, sizeof *dir);
28
29  *dfab = cc$rms_fab;
30  *dnam = cc$rms_nam;
31  sprintf (searchspec, "%s*.*;", dspec);
32
33  dfab->fab$l_fna = searchspec;
34  dfab->fab$b_fns = strlen (searchspec);
35  dfab->fab$l_nam = dnam;
36
37  *dnam = cc$rms_nam;
38  dnam->nam$l_esa = searchspec;
39  dnam->nam$b_ess = MAXNAMLEN;
40
41  if (! (sys$parse (dfab) & 1))
42    {
43      free (dir);
44      free (dnam);
45      free (searchspec);
46      return (NULL);
47    }
48
49  return dir;
50}
51
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)
61
62struct direct *
63readdir (dir)
64     DIR * dir;
65{
66  struct FAB *dfab = &dir->fab;
67  struct NAM *dnam = (struct NAM *)(dfab->fab$l_nam);
68  struct direct *dentry = &dir->dir;
69  int i;
70
71  memset (dentry, 0, sizeof *dentry);
72
73  dnam->nam$l_rsa = dir->d_result;
74  dnam->nam$b_rss = MAXNAMLEN;
75
76  DB (DB_VERBOSE, ("."));
77
78  if (!((i = sys$search (dfab)) & 1))
79    {
80      DB (DB_VERBOSE, (_("sys$search failed with %d\n"), i));
81      return (NULL);
82    }
83
84  dentry->d_off = 0;
85  if (dnam->nam$w_fid == 0)
86    dentry->d_fileno = 1;
87  else
88    dentry->d_fileno = dnam->nam$w_fid[0] + (dnam->nam$w_fid[1] << 16);
89
90  dentry->d_reclen = sizeof (struct direct);
91  dentry->d_namlen = dnam->nam$b_name + dnam->nam$b_type;
92  strncpy (dentry->d_name, dnam->nam$l_name, dentry->d_namlen);
93  dentry->d_name[dentry->d_namlen] = '\0';
94  uppercasify (dentry->d_name);
95
96  return (dentry);
97}
98
99int
100closedir (dir)
101     DIR *dir;
102{
103  if (dir != NULL)
104    {
105      struct FAB *dfab = &dir->fab;
106      struct NAM *dnam = (struct NAM *)(dfab->fab$l_nam);
107      if (dnam != NULL)
108	free (dnam->nam$l_esa);
109      free (dnam);
110      free (dir);
111    }
112
113  return 0;
114}
115#endif /* compiled for OpenVMS prior to V7.x */
116
117char *
118getwd (cwd)
119     char *cwd;
120{
121  static char buf[512];
122
123  if (cwd)
124    return (getcwd (cwd, 512));
125  else
126    return (getcwd (buf, 512));
127}
128
129int
130vms_stat (name, buf)
131     char *name;
132     struct stat *buf;
133{
134  int status;
135  int i;
136
137  static struct FAB Fab;
138  static struct NAM Nam;
139  static struct fibdef Fib;	/* short fib */
140  static struct dsc$descriptor FibDesc =
141  { sizeof (Fib), DSC$K_DTYPE_Z, DSC$K_CLASS_S, (char *) &Fib };
142  static struct dsc$descriptor_s DevDesc =
143  { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, &Nam.nam$t_dvi[1] };
144  static char EName[NAM$C_MAXRSS];
145  static char RName[NAM$C_MAXRSS];
146  static struct dsc$descriptor_s FileName =
147  { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0 };
148  static struct dsc$descriptor_s string =
149  { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0 };
150  static unsigned long Rdate[2];
151  static unsigned long Cdate[2];
152  static struct atrdef Atr[] =
153  {
154#if defined(VAX)
155    /* Revision date */
156    { sizeof (Rdate), ATR$C_REVDATE, (unsigned int) &Rdate[0] },
157    /* Creation date */
158    { sizeof (Cdate), ATR$C_CREDATE, (unsigned int) &Cdate[0] },
159#else
160    /* Revision date */
161    { sizeof (Rdate), ATR$C_REVDATE, &Rdate[0] },
162    /* Creation date */
163    { sizeof (Cdate), ATR$C_CREDATE, &Cdate[0]},
164#endif
165    { 0, 0, 0 }
166  };
167  static short int DevChan;
168  static short int iosb[4];
169
170  name = vmsify (name, 0);
171
172  /* initialize RMS structures, we need a NAM to retrieve the FID */
173  Fab = cc$rms_fab;
174  Fab.fab$l_fna = name;		/* name of file */
175  Fab.fab$b_fns = strlen (name);
176  Fab.fab$l_nam = &Nam;		/* FAB has an associated NAM */
177
178  Nam = cc$rms_nam;
179  Nam.nam$l_esa = EName;	/* expanded filename */
180  Nam.nam$b_ess = sizeof (EName);
181  Nam.nam$l_rsa = RName;	/* resultant filename */
182  Nam.nam$b_rss = sizeof (RName);
183
184  /* do $PARSE and $SEARCH here */
185  status = sys$parse (&Fab);
186  if (!(status & 1))
187    return -1;
188
189  DevDesc.dsc$w_length = Nam.nam$t_dvi[0];
190  status = sys$assign (&DevDesc, &DevChan, 0, 0);
191  if (!(status & 1))
192    return -1;
193
194  FileName.dsc$a_pointer = Nam.nam$l_name;
195  FileName.dsc$w_length = Nam.nam$b_name + Nam.nam$b_type + Nam.nam$b_ver;
196
197  /* Initialize the FIB */
198  for (i = 0; i < 3; i++)
199    {
200#ifndef __VAXC
201      Fib.fib$w_fid[i] = Nam.nam$w_fid[i];
202      Fib.fib$w_did[i] = Nam.nam$w_did[i];
203#else
204      Fib.fib$r_fid_overlay.fib$w_fid[i] = Nam.nam$w_fid[i];
205      Fib.fib$r_did_overlay.fib$w_did[i] = Nam.nam$w_did[i];
206#endif
207    }
208
209  status = sys$qiow (0, DevChan, IO$_ACCESS, &iosb, 0, 0,
210		     &FibDesc, &FileName, 0, 0, &Atr, 0);
211  sys$dassgn (DevChan);
212  if (!(status & 1))
213    return -1;
214  status = iosb[0];
215  if (!(status & 1))
216    return -1;
217
218  status = stat (name, buf);
219  if (status)
220    return -1;
221
222  buf->st_mtime = ((Rdate[0] >> 24) & 0xff) + ((Rdate[1] << 8) & 0xffffff00);
223  buf->st_ctime = ((Cdate[0] >> 24) & 0xff) + ((Cdate[1] << 8) & 0xffffff00);
224
225  return 0;
226}
227
228char *
229cvt_time (tval)
230     unsigned long tval;
231{
232  static long int date[2];
233  static char str[27];
234  static struct dsc$descriptor date_str =
235  { 26, DSC$K_DTYPE_T, DSC$K_CLASS_S, str };
236
237  date[0] = (tval & 0xff) << 24;
238  date[1] = ((tval >> 8) & 0xffffff);
239
240  if ((date[0] == 0) && (date[1] == 0))
241    return ("never");
242
243  sys$asctim (0, &date_str, date, 0);
244  str[26] = '\0';
245
246  return (str);
247}
248
249int
250strcmpi (s1, s2)
251    const char *s1;
252    const char *s2;
253{
254  while (*s1 != '\0' && toupper(*s1) == toupper(*s2))
255    {
256      s1++;
257      s2++;
258    }
259
260  return toupper(*(unsigned char *) s1) - toupper(*(unsigned char *) s2);
261}
262