forwarding a radium compilation fix.
[AROS-Contrib.git] / arospdf / goo / vms_directory.c
blob92d94932099754ff47a95a8399c6492f02935b52
1 /*
2 * DIRECTORY.C - VMS emulation routines for UNIX Directory
3 * callable routines
5 * Author: Patrick L. Mahan
6 * Location: TGV, Inc
7 * Date: 19-November-1991
9 * Purpose: Provides emulation of the BSD directory routines
10 * which are used by some of the X11 R4 release
11 * software.
13 * Side effects: This is only a partial emulation. Not all of
14 * the required information is passed to the user.
16 * Modification History
18 * Date | Who | Version | History
19 * ------------+-----------+---------------+----------------------------
20 * 19-Nov-1991 | PLM | 1.0 | First Write
21 * 20-Apr-1992 | PLM | 1.1 | Added validation check for
22 * | | | for the directory
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <rmsdef.h>
29 #include <descrip.h>
30 #include <lib$routines.h>
31 #include "vms_dirent.h"
33 #define NOWILD 0x00000001
34 #define MULTIPLE 0x00000002
36 static unsigned long context = 0;
38 static struct dsc$descriptor_s *create_descriptor ( name )
39 char *name;
41 struct dsc$descriptor_s *retdescrip;
43 retdescrip = (struct dsc$descriptor_s *)calloc(1, sizeof(struct dsc$descriptor_s));
45 if (retdescrip == NULL) return ((struct dsc$descriptor_s *)NULL);
47 retdescrip->dsc$b_dtype = DSC$K_DTYPE_T;
48 retdescrip->dsc$b_class = DSC$K_CLASS_S;
49 retdescrip->dsc$w_length = strlen(name);
50 retdescrip->dsc$a_pointer = name;
52 return (retdescrip);
55 static int Check_Directory( dirname )
56 char *dirname;
58 static char *tmpdir, *cp;
59 FILE *tfp;
60 int status;
62 status = 1;
64 tmpdir = calloc(strlen(dirname)+15,sizeof(char));
66 strcpy(tmpdir, dirname);
68 cp = strrchr(tmpdir, '.');
70 if (cp != NULL) {
71 *cp = ']';
72 cp = strrchr(tmpdir, ']');
73 *cp = '.';
74 strcat(tmpdir, "dir");
76 else {
77 char *tmp1;
78 tmp1 = calloc(strlen(dirname)+1,sizeof(char));
79 cp = strchr(tmpdir, '[');
80 cp++;
81 strcpy(tmp1, cp);
82 cp = strrchr(tmp1, ']');
83 *cp = '\0';
84 cp = strchr(tmpdir, '[');
85 cp++;
86 *cp = '\0';
87 strcat(tmpdir, "000000]");
88 strcat(tmpdir, tmp1);
89 strcat(tmpdir, ".dir");
92 tfp = fopen(tmpdir, "r");
94 if (tfp == NULL) status = 0;
96 fclose(tfp);
98 return (status);
101 DIR *opendir( dirname )
102 char *dirname;
104 DIR *retdir;
105 struct dsc$descriptor_s filedescriptor;
106 char *filepathname;
108 retdir = (DIR *) calloc(1, sizeof(DIR));
110 if (retdir == NULL) return ((DIR *)NULL);
112 if (!Check_Directory(dirname)) return ((DIR *)NULL);
114 filepathname = (char *)calloc(256, sizeof(char));
116 strcpy(filepathname, dirname);
117 strcat(filepathname, "*.*.*");
119 retdir->dd_fd = (unsigned long) create_descriptor(filepathname);
120 retdir->dd_loc = 0;
121 retdir->dd_size = strlen(filepathname);
122 retdir->dd_bsize = 0;
123 retdir->dd_off = 0;
124 retdir->dd_buf = filepathname;
126 return (retdir);
129 struct dirent *readdir( dirp )
130 DIR *dirp;
132 static struct dirent *retdirent;
133 struct dsc$descriptor_s retfilenamedesc;
134 struct dsc$descriptor_s searchpathdesc = *((struct dsc$descriptor_s *)dirp->dd_fd);
135 char retfilename[256];
136 char *sp;
137 unsigned long istatus;
138 unsigned long rms_status;
139 unsigned long flags;
141 retdirent = (struct dirent *)NULL;
143 flags = MULTIPLE;
145 retfilenamedesc.dsc$b_dtype = DSC$K_DTYPE_T;
146 retfilenamedesc.dsc$b_class = DSC$K_CLASS_S;
147 retfilenamedesc.dsc$w_length = 255;
148 retfilenamedesc.dsc$a_pointer= retfilename;
150 istatus = lib$find_file (&searchpathdesc,
151 &retfilenamedesc,
152 &dirp->dd_loc,
153 0, 0,
154 &rms_status,
155 &flags);
157 if (!(istatus & 1) && (istatus != RMS$_NMF) && (istatus != RMS$_FNF))
159 lib$signal (istatus);
160 return (retdirent);
162 else if ((istatus == RMS$_NMF) || (istatus == RMS$_FNF))
163 return (retdirent);
165 retfilename[retfilenamedesc.dsc$w_length] = '\0';
167 sp = strchr(retfilename, ' ');
168 if (sp != NULL) *sp = '\0';
170 sp = strrchr(retfilename, ']');
171 if (sp != NULL)
172 sp++;
173 else
174 sp = retfilename;
176 retdirent = (struct dirent *)calloc(1, sizeof(struct dirent));
178 strcpy(retdirent->d_name, sp);
179 retdirent->d_namlen = strlen(sp);
180 retdirent->d_fileno = 0;
181 retdirent->d_off = 0;
182 retdirent->d_reclen = DIRSIZ(retdirent);
184 return (retdirent);
187 long telldir( dirp )
188 DIR *dirp;
190 return(0);
193 void seekdir( dirp, loc )
194 DIR *dirp;
195 int loc;
197 return;
200 void rewinddir( dirp )
201 DIR *dirp;
203 lib$find_file_end (&dirp->dd_loc);
206 void closedir( dirp )
207 DIR *dirp;
209 lib$find_file_end (&dirp->dd_loc);
211 cfree ((void *) dirp->dd_fd);
212 cfree (dirp->dd_buf);
213 cfree (dirp);