author | Claus Gittinger <cg@exept.de> |
Thu, 30 Oct 2008 17:25:55 +0100 | |
changeset 11294 | ecf21f4ab995 |
parent 11288 | c71a04b0e26c |
child 11302 | c8e8b9cde3d7 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
7826 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
FileStream subclass:#DirectoryStream |
1664 | 15 |
instanceVariableNames:'dirPointer readAheadEntry' |
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
16 |
classVariableNames:'' |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
17 |
poolDictionaries:'' |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
18 |
category:'Streams-External' |
1 | 19 |
! |
20 |
||
217 | 21 |
!DirectoryStream primitiveDefinitions! |
1 | 22 |
%{ |
437 | 23 |
|
1631 | 24 |
#define UNIX_LIKE |
2609 | 25 |
|
1631 | 26 |
#if defined(WIN32) |
27 |
# undef UNIX_LIKE |
|
28 |
#endif |
|
437 | 29 |
|
2896 | 30 |
#if defined(transputer) |
31 |
# undef UNIX_LIKE |
|
32 |
#endif |
|
33 |
||
1631 | 34 |
#ifdef UNIX_LIKE |
7955
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
35 |
# if defined(__GLIBC__) && (__GLIBC__ == 2) |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
36 |
# if defined(__GLIBC_MINOR__) && (__GLIBC_MINOR__ == 1) |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
37 |
/* there is a bug there, preventing 64bit readdir. */ |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
38 |
# undef __USE_FILE_OFFSET64 |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
39 |
# undef __USE_LARGEFILE64 |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
40 |
# endif |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
41 |
# endif |
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
42 |
|
1631 | 43 |
# include <stdio.h> |
44 |
# define _STDIO_H_INCLUDED_ |
|
45 |
||
46 |
# include <errno.h> |
|
47 |
# define _ERRNO_H_INCLUDED_ |
|
10 | 48 |
|
2896 | 49 |
# include <sys/types.h> |
50 |
# include <sys/stat.h> |
|
1631 | 51 |
|
2896 | 52 |
# ifdef HAS_OPENDIR |
53 |
# include <sys/types.h> |
|
54 |
# ifdef NEXT |
|
55 |
# include <sys/dir.h> |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
56 |
# define DIRENT_STRUCT struct direct |
2896 | 57 |
# else |
58 |
# include <dirent.h> |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
59 |
# define DIRENT_STRUCT struct dirent |
1 | 60 |
# endif |
61 |
# endif |
|
223 | 62 |
|
63 |
/* |
|
64 |
* on some systems errno is a macro ... check for it here |
|
65 |
*/ |
|
1631 | 66 |
# ifndef errno |
67 |
extern errno; |
|
68 |
# endif |
|
2896 | 69 |
|
1631 | 70 |
#endif /* UNIX_LIKE */ |
71 |
||
2896 | 72 |
|
1631 | 73 |
#ifdef WIN32 |
74 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8881
diff
changeset
|
75 |
# ifdef __i386__ |
1631 | 76 |
# define _X86_ |
77 |
# endif |
|
78 |
||
79 |
# undef INT |
|
80 |
# undef Array |
|
81 |
# undef Number |
|
82 |
# undef Method |
|
83 |
# undef Point |
|
84 |
# undef Context |
|
85 |
# undef Rectangle |
|
1989 | 86 |
# undef Block |
4298 | 87 |
# undef Time |
88 |
# undef Date |
|
10342 | 89 |
# undef Message |
90 |
# undef Process |
|
91 |
# undef Processor |
|
92 |
# undef INT |
|
1631 | 93 |
|
2685 | 94 |
/* # include <types.h> /* */ |
1989 | 95 |
# include <stdarg.h> /* */ |
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
96 |
# include <errno.h> |
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
97 |
# define _ERRNO_H_INCLUDED_ |
1989 | 98 |
# include <windef.h> /* */ |
99 |
# include <winbase.h> /* */ |
|
4298 | 100 |
/* # include <wingdi.h> /* */ |
101 |
/* # include <winuser.h> /* */ |
|
1631 | 102 |
|
103 |
# ifdef __DEF_Array |
|
104 |
# define Array __DEF_Array |
|
105 |
# endif |
|
106 |
# ifdef __DEF_Number |
|
107 |
# define Number __DEF_Number |
|
108 |
# endif |
|
109 |
# ifdef __DEF_Method |
|
110 |
# define Method __DEF_Method |
|
111 |
# endif |
|
112 |
# ifdef __DEF_Point |
|
113 |
# define Point __DEF_Point |
|
114 |
# endif |
|
1989 | 115 |
# ifdef __DEF_Block |
116 |
# define Block __DEF_Block |
|
117 |
# endif |
|
1631 | 118 |
# ifdef __DEF_Context |
119 |
# define Context __DEF_Context |
|
120 |
# endif |
|
4298 | 121 |
# ifdef __DEF_Date |
122 |
# define Date __DEF_Date |
|
123 |
# endif |
|
124 |
# ifdef __DEF_Time |
|
125 |
# define Time __DEF_Time |
|
126 |
# endif |
|
10342 | 127 |
# ifdef __DEF_Message |
128 |
# define Message __DEF_Message |
|
129 |
# endif |
|
130 |
# ifdef __DEF_Process |
|
131 |
# define Process __DEF_Process |
|
132 |
# endif |
|
133 |
# ifdef __DEF_Processor |
|
134 |
# define Processor __DEF_Processor |
|
135 |
# endif |
|
136 |
# define INT int |
|
1631 | 137 |
|
2999
b0e55440cf12
eliminate MKOBJ & MKCP (use MKEXTERNALADDRESS & externalAddressVal)
Claus Gittinger <cg@exept.de>
parents:
2982
diff
changeset
|
138 |
# define __HANDLEVal(o) (HANDLE)__externalAddressVal(o) |
1631 | 139 |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
140 |
# ifndef DO_WRAP_CALLS |
6266
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
141 |
# define STX_C_CALL0(__nm__, __f__) __f__((__a1__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
142 |
# define STX_C_CALL1(__nm__, __f__, __a1__) __f__((__a1__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
143 |
# define STX_C_CALL2(__nm__, __f__, __a1__, __a2__) __f__((__a1__), (__a2__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
144 |
# define STX_C_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __f__((__a1__), (__a2__), (__a3__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
145 |
# define STX_API_CALL0(__nm__, __f__) __f__((__a1__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
146 |
# define STX_API_CALL1(__nm__, __f__, __a1__) __f__((__a1__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
147 |
# define STX_API_CALL2(__nm__, __f__, __a1__, __a2__) __f__((__a1__), (__a2__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
148 |
# define STX_API_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __f__((__a1__), (__a2__), (__a3__)) |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
149 |
# else |
6266
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
150 |
# define STX_C_CALL0(__nm__, __f__) __STX_C_CALL0(__nm__,(void*)__f__) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
151 |
# define STX_C_CALL1(__nm__, __f__, __a1__) __STX_C_CALL1(__nm__,(void*)__f__, (void*)(__a1__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
152 |
# define STX_C_CALL2(__nm__, __f__, __a1__, __a2__) __STX_C_CALL2(__nm__,(void*)__f__, (void*)(__a1__), (void*)(__a2__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
153 |
# define STX_C_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __STX_C_CALL3(__nm__,(void*)__f__, (void*)(__a1__), (void*)(__a2__), (void*)(__a3__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
154 |
# define STX_API_CALL0(__nm__, __f__) __STX_API_CALL0(__nm__,(void*)__f__) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
155 |
# define STX_API_CALL1(__nm__, __f__, __a1__) __STX_API_CALL1(__nm__,(void*)__f__, (void*)(__a1__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
156 |
# define STX_API_CALL2(__nm__, __f__, __a1__, __a2__) __STX_API_CALL2(__nm__,(void*)__f__, (void*)(__a1__), (void*)(__a2__)) |
b12e2e6a84d7
pass name of called API/C function in wrap
Claus Gittinger <cg@exept.de>
parents:
6259
diff
changeset
|
157 |
# define STX_API_CALL3(__nm__, __f__, __a1__, __a2__, __a3__) __STX_API_CALL3(__nm__,(void*)__f__, (void*)(__a1__), (void*)(__a2__), (void*)(__a3__)) |
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
158 |
# endif |
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
159 |
|
1631 | 160 |
#endif /* WIN32 */ |
223 | 161 |
|
1 | 162 |
%} |
206 | 163 |
! ! |
1 | 164 |
|
438 | 165 |
!DirectoryStream primitiveFunctions! |
166 |
%{ |
|
2896 | 167 |
|
168 |
#ifndef HAS_OPENDIR |
|
7955
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
169 |
|
2896 | 170 |
# if defined(__VMS__) |
171 |
||
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
172 |
# define lib$find_file LIB$FIND_FILE |
438 | 173 |
|
174 |
/* |
|
175 |
** VMS readdir() routines. |
|
176 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
177 |
** This code has no copyright. |
|
178 |
*/ |
|
179 |
||
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
180 |
/* 12-NOV-1990 added d_namlen field and special case "." name -GJC@MITECH.COM |
438 | 181 |
*/ |
182 |
||
2896 | 183 |
# ifndef _STDIO_H_INCLUDED_ |
184 |
# include <stdio.h> |
|
185 |
# define _STDIO_H_INCLUDED_ |
|
186 |
# endif |
|
438 | 187 |
|
2896 | 188 |
# ifndef _CTYPE_H_INCLUDED_ |
189 |
# include <ctype.h> |
|
190 |
# define _CTYPE_H_INCLUDED_ |
|
191 |
# endif |
|
438 | 192 |
|
2896 | 193 |
# ifndef _ERRNO_H_INCLUDED_ |
194 |
# include <errno.h> |
|
195 |
# define _ERRNO_H_INCLUDED_ |
|
196 |
# endif |
|
438 | 197 |
|
2896 | 198 |
# ifndef _DESCRIP_H_INCLUDED_ |
199 |
# include <descrip.h> |
|
200 |
# define _DESCRIP_H_INCLUDED_ |
|
201 |
# endif |
|
438 | 202 |
|
2896 | 203 |
# ifndef _RMSDEF_H_INCLUDED_ |
204 |
# include <rmsdef.h> |
|
205 |
# define _RMSDEF_H_INCLUDED_ |
|
206 |
# endif |
|
438 | 207 |
|
208 |
/* |
|
209 |
* actually, the following has to go into dirent.h ... |
|
210 |
*/ |
|
2896 | 211 |
/* BEGIN included dirent.h |
438 | 212 |
* |
213 |
** Header file for VMS readdir() routines. |
|
214 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
215 |
** This code has no copyright. |
|
216 |
** |
|
217 |
** You must #include <descrip.h> before this file. |
|
218 |
*/ |
|
219 |
||
220 |
/* 12-NOV-1990 added d_namlen field -GJC@MITECH.COM */ |
|
221 |
||
222 |
/* Data structure returned by READDIR(). */ |
|
223 |
struct dirent { |
|
224 |
char d_name[100]; /* File name */ |
|
225 |
int d_namlen; |
|
2957 | 226 |
int vms_verscount; /* Number of versions */ |
227 |
int vms_versions[20]; /* Version numbers */ |
|
438 | 228 |
}; |
229 |
||
230 |
/* Handle returned by opendir(), used by the other routines. You |
|
231 |
* are not supposed to care what's inside this structure. */ |
|
232 |
typedef struct _dirdesc { |
|
233 |
long context; |
|
2957 | 234 |
int vms_wantversions; |
438 | 235 |
char *pattern; |
236 |
struct dirent entry; |
|
237 |
struct dsc$descriptor_s pat; |
|
238 |
} DIR; |
|
239 |
||
240 |
||
241 |
#define rewinddir(dirp) seekdir((dirp), 0L) |
|
242 |
||
243 |
||
244 |
extern DIR *opendir(); |
|
245 |
extern struct dirent *readdir(); |
|
246 |
extern long telldir(); |
|
247 |
extern void seekdir(); |
|
248 |
extern void closedir(); |
|
249 |
extern void vmsreaddirversions(); |
|
250 |
/* |
|
251 |
* END dirent.h |
|
252 |
*/ |
|
2896 | 253 |
#define _DIRENT_H_INCLUDED_ |
438 | 254 |
|
255 |
||
256 |
/* Number of elements in vms_versions array */ |
|
257 |
#define VERSIZE(e) (sizeof e->vms_versions / sizeof e->vms_versions[0]) |
|
258 |
||
259 |
/* Linked in later. */ |
|
260 |
extern char *strrchr(); |
|
261 |
extern char *strcpy(); |
|
262 |
/* Don't need this when all these programs are lumped together. RLD |
|
263 |
extern char *malloc(); |
|
264 |
*/ |
|
265 |
||
266 |
/* |
|
267 |
** Open a directory, return a handle for later use. |
|
268 |
*/ |
|
269 |
DIR * |
|
270 |
opendir(name) |
|
271 |
char *name; |
|
272 |
{ |
|
273 |
DIR *dd; |
|
274 |
||
275 |
/* Get memory for the handle, and the pattern. */ |
|
276 |
if ((dd = (DIR *)malloc(sizeof *dd)) == NULL) { |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
277 |
__threadErrno = ENOMEM; |
438 | 278 |
return NULL; |
279 |
} |
|
280 |
||
281 |
if (strcmp(".",name) == 0) name = ""; |
|
282 |
||
283 |
dd->pattern = malloc((unsigned int)(strlen(name) + sizeof "*.*" + 1)); |
|
284 |
if (dd->pattern == NULL) { |
|
285 |
free((char *)dd); |
|
5120
13143fbcbdf8
introduced __threadErrno (for native threads)
Claus Gittinger <cg@exept.de>
parents:
4526
diff
changeset
|
286 |
__threadErrno = ENOMEM; |
438 | 287 |
return NULL; |
288 |
} |
|
289 |
||
290 |
/* Fill in the fields; mainly playing with the descriptor. */ |
|
291 |
(void)sprintf(dd->pattern, "%s*.*", name); |
|
292 |
dd->context = 0; |
|
293 |
dd->vms_wantversions = 0; |
|
294 |
dd->pat.dsc$a_pointer = dd->pattern; |
|
295 |
dd->pat.dsc$w_length = strlen(dd->pattern); |
|
296 |
dd->pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
297 |
dd->pat.dsc$b_class = DSC$K_CLASS_S; |
|
298 |
||
299 |
return dd; |
|
300 |
} |
|
301 |
||
302 |
/* |
|
303 |
** Set the flag to indicate we want versions or not. |
|
304 |
*/ |
|
305 |
void |
|
306 |
vmsreaddirversions(dd, flag) |
|
307 |
DIR *dd; |
|
308 |
int flag; |
|
309 |
{ |
|
310 |
dd->vms_wantversions = flag; |
|
311 |
} |
|
312 |
||
313 |
/* |
|
314 |
** Free up an opened directory. |
|
315 |
*/ |
|
316 |
void |
|
317 |
closedir(dd) |
|
318 |
DIR *dd; |
|
319 |
{ |
|
320 |
free(dd->pattern); |
|
321 |
free((char *)dd); |
|
322 |
} |
|
323 |
||
324 |
/* |
|
325 |
** Collect all the version numbers for the current file. |
|
326 |
*/ |
|
327 |
static void |
|
328 |
collectversions(dd) |
|
329 |
DIR *dd; |
|
330 |
{ |
|
331 |
struct dsc$descriptor_s pat; |
|
332 |
struct dsc$descriptor_s res; |
|
333 |
struct dirent *e; |
|
334 |
char *p; |
|
335 |
char buff[sizeof dd->entry.d_name]; |
|
336 |
int i; |
|
337 |
char *text; |
|
338 |
long context; |
|
339 |
||
340 |
/* Convenient shorthand. */ |
|
341 |
e = &dd->entry; |
|
342 |
||
343 |
/* Add the version wildcard, ignoring the "*.*" put on before */ |
|
344 |
i = strlen(dd->pattern); |
|
345 |
text = malloc((unsigned int)(i + strlen(e->d_name)+ 2 + 1)); |
|
346 |
if (text == NULL) |
|
347 |
return; |
|
348 |
(void)strcpy(text, dd->pattern); |
|
349 |
(void)sprintf(&text[i - 3], "%s;*", e->d_name); |
|
350 |
||
351 |
/* Set up the pattern descriptor. */ |
|
352 |
pat.dsc$a_pointer = text; |
|
353 |
pat.dsc$w_length = strlen(text); |
|
354 |
pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
355 |
pat.dsc$b_class = DSC$K_CLASS_S; |
|
356 |
||
357 |
/* Set up result descriptor. */ |
|
358 |
res.dsc$a_pointer = buff; |
|
359 |
res.dsc$w_length = sizeof buff - 2; |
|
360 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
361 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
362 |
||
363 |
/* Read files, collecting versions. */ |
|
364 |
for (context = 0; e->vms_verscount < VERSIZE(e); e->vms_verscount++) { |
|
365 |
if (lib$find_file(&pat, &res, &context) == RMS$_NMF || context == 0) |
|
366 |
break; |
|
367 |
buff[sizeof buff - 1] = '\0'; |
|
368 |
if (p = strchr(buff, ';')) |
|
369 |
e->vms_versions[e->vms_verscount] = atoi(p + 1); |
|
370 |
else |
|
371 |
e->vms_versions[e->vms_verscount] = -1; |
|
372 |
} |
|
373 |
||
374 |
free(text); |
|
375 |
} |
|
376 |
||
377 |
/* |
|
378 |
** Read the next entry from the directory. |
|
379 |
*/ |
|
380 |
struct dirent * |
|
381 |
readdir(dd) |
|
2957 | 382 |
DIR *dd; |
438 | 383 |
{ |
2957 | 384 |
struct dsc$descriptor_s res; |
385 |
char *p; |
|
2982 | 386 |
char buff[sizeof dd->entry.d_name + 10]; |
2957 | 387 |
int i; |
438 | 388 |
|
389 |
/* Set up result descriptor, and get next file. */ |
|
390 |
res.dsc$a_pointer = buff; |
|
391 |
res.dsc$w_length = sizeof buff - 2; |
|
392 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
393 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
394 |
if (lib$find_file(&dd->pat, &res, &dd->context) == RMS$_NMF |
|
395 |
|| dd->context == 0L) |
|
396 |
/* None left... */ |
|
397 |
return NULL; |
|
398 |
||
399 |
/* Force the buffer to end with a NUL. */ |
|
400 |
buff[sizeof buff - 1] = '\0'; |
|
401 |
for (p = buff; !isspace(*p); p++) |
|
402 |
; |
|
403 |
*p = '\0'; |
|
404 |
||
405 |
/* Skip any directory component and just copy the name. */ |
|
406 |
if (p = strchr(buff, ']')) |
|
407 |
(void)strcpy(dd->entry.d_name, p + 1); |
|
408 |
else |
|
409 |
(void)strcpy(dd->entry.d_name, buff); |
|
410 |
||
411 |
/* Clobber the version. */ |
|
412 |
if (p = strchr(dd->entry.d_name, ';')) |
|
413 |
*p = '\0'; |
|
414 |
||
2982 | 415 |
/* claus: empty dirs seems to leave *.* in the buffer ... */ |
416 |
if (strcmp(dd->entry.d_name, "*.*") == 0) { |
|
417 |
return NULL; |
|
418 |
} |
|
419 |
||
438 | 420 |
dd->entry.d_namlen = strlen(dd->entry.d_name); |
421 |
||
422 |
dd->entry.vms_verscount = 0; |
|
423 |
if (dd->vms_wantversions) |
|
424 |
collectversions(dd); |
|
425 |
return &dd->entry; |
|
426 |
} |
|
427 |
||
428 |
/* |
|
429 |
** Return something that can be used in a seekdir later. |
|
430 |
*/ |
|
431 |
long |
|
432 |
telldir(dd) |
|
2957 | 433 |
DIR *dd; |
438 | 434 |
{ |
435 |
return dd->context; |
|
436 |
} |
|
437 |
||
438 |
/* |
|
439 |
** Return to a spot where we used to be. |
|
440 |
*/ |
|
441 |
void |
|
442 |
seekdir(dd, pos) |
|
2957 | 443 |
DIR *dd; |
444 |
long pos; |
|
438 | 445 |
{ |
446 |
dd->context = pos; |
|
447 |
} |
|
448 |
||
2896 | 449 |
# define HAS_OPENDIR |
450 |
||
451 |
# endif /* __VMS__ */ |
|
452 |
#endif /* not HAS_OPENDIR */ |
|
438 | 453 |
%} |
454 |
! ! |
|
455 |
||
613 | 456 |
!DirectoryStream class methodsFor:'documentation'! |
457 |
||
458 |
copyright |
|
459 |
" |
|
460 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
461 |
All Rights Reserved |
|
1 | 462 |
|
613 | 463 |
This software is furnished under a license and may be used |
464 |
only in accordance with the terms of that license and with the |
|
465 |
inclusion of the above copyright notice. This software may not |
|
466 |
be provided or otherwise made available to, or used by, any |
|
467 |
other person. No title to or ownership of the software is |
|
468 |
hereby transferred. |
|
469 |
" |
|
470 |
! |
|
360 | 471 |
|
613 | 472 |
documentation |
473 |
" |
|
474 |
Instances of DirectoryStream allow reading a file-directory, |
|
475 |
as if it was a stream of filenames. |
|
476 |
Basically, its an interface to opendir, readdir and closedir. |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
477 |
Notice: |
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
478 |
DirectoryStream is an ST/X special; |
2957 | 479 |
for portability, we recommend the use of Filename protocol. |
1295 | 480 |
|
481 |
[author:] |
|
1631 | 482 |
Claus Gittinger |
2957 | 483 |
|
484 |
[see also:] |
|
485 |
Filename |
|
613 | 486 |
" |
1 | 487 |
! ! |
488 |
||
489 |
!DirectoryStream class methodsFor:'instance creation'! |
|
490 |
||
491 |
directoryNamed:dirName |
|
7826 | 492 |
"return a DirectoryStream for directory named dirName, aString |
493 |
Raises an openError, if the directory does not exists." |
|
1 | 494 |
|
495 |
|newStream| |
|
496 |
||
8775 | 497 |
newStream := (self basicNew) text; pathName:dirName. |
1 | 498 |
newStream openForReading isNil ifTrue:[^nil]. |
499 |
^ newStream |
|
7826 | 500 |
|
501 |
" |
|
8881
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
502 |
self directoryNamed:'adadasd' |
53e94478133f
STORE macro fix (gcc3.4 bug)
Claus Gittinger <cg@exept.de>
parents:
8775
diff
changeset
|
503 |
self directoryNamed:'.' |
7826 | 504 |
" |
1 | 505 |
! ! |
506 |
||
5245 | 507 |
!DirectoryStream methodsFor:'access-reading'! |
1 | 508 |
|
509 |
nextLine |
|
510 |
"return the next filename as a string" |
|
511 |
||
512 |
|prevEntry nextEntry| |
|
513 |
%{ |
|
77 | 514 |
#ifdef HAS_OPENDIR |
1 | 515 |
DIR *d; |
7955
f3a24f9588ee
64bit readdir bug workaround
Claus Gittinger <cg@exept.de>
parents:
7826
diff
changeset
|
516 |
DIRENT_STRUCT *dp; |
92 | 517 |
OBJ dirP; |
1 | 518 |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
519 |
if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) { |
11288 | 520 |
__INST(lastErrorNumber) = nil; |
521 |
d = (DIR *)__FILEVal(dirP); |
|
92 | 522 |
|
11288 | 523 |
__BEGIN_INTERRUPTABLE__ |
524 |
do { |
|
525 |
do { |
|
526 |
__threadErrno = 0; |
|
527 |
dp = readdir(d); |
|
528 |
/* |
|
529 |
* for compatibility with ST-80, |
|
530 |
* skip entries for '.' and '..'. |
|
531 |
* If wanted, these must be added synthetically. |
|
532 |
*/ |
|
533 |
} while (dp && ((strcmp(dp->d_name, ".")==0) || (strcmp(dp->d_name, "..")==0))); |
|
534 |
} while ((dp == NULL) && (__threadErrno == EINTR)); |
|
535 |
__END_INTERRUPTABLE__ |
|
92 | 536 |
|
11288 | 537 |
if (dp != NULL) { |
538 |
nextEntry = __MKSTRING((char *)(dp->d_name)); |
|
539 |
} else { |
|
540 |
if (__threadErrno) { |
|
541 |
__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno); |
|
542 |
} else { |
|
543 |
__INST(hitEOF) = true; |
|
544 |
} |
|
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
545 |
} |
1 | 546 |
} |
1631 | 547 |
#else /* no HAS_OPENDIR */ |
548 |
# ifdef WIN32 |
|
549 |
HANDLE d; |
|
11288 | 550 |
WIN32_FIND_DATAW data; |
551 |
OBJ __MKU16STRING(); |
|
1631 | 552 |
OBJ dirP; |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
553 |
int rslt; |
1631 | 554 |
|
555 |
if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) { |
|
11288 | 556 |
__INST(lastErrorNumber) = nil; |
557 |
d = __HANDLEVal(dirP); |
|
1631 | 558 |
|
11288 | 559 |
do { |
560 |
__threadErrno = 0; |
|
561 |
rslt = STX_API_CALL2( "FindNextFileW", FindNextFileW, d, &data ); |
|
562 |
} while ((rslt < 0) && (__threadErrno == EINTR)); |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
563 |
|
11288 | 564 |
if (rslt > 0) { |
565 |
nextEntry = __MKU16STRING( data.cFileName ); |
|
566 |
} else { |
|
567 |
__INST(hitEOF) = true; |
|
568 |
} |
|
1631 | 569 |
} |
570 |
# endif /* WIN32 */ |
|
571 |
#endif /* HAS_OPENDIR */ |
|
155 | 572 |
%}. |
573 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1664 | 574 |
prevEntry := readAheadEntry. |
575 |
readAheadEntry := nextEntry. |
|
11288 | 576 |
readAheadEntry notNil ifTrue:[ |
577 |
readAheadEntry := readAheadEntry asSingleByteStringIfPossible. |
|
578 |
]. |
|
8614 | 579 |
prevEntry isNil ifTrue:[^ self pastEndRead]. |
1 | 580 |
^ prevEntry |
581 |
! ! |
|
582 |
||
613 | 583 |
!DirectoryStream methodsFor:'closing'! |
584 |
||
585 |
close |
|
586 |
"close the stream - tell operating system" |
|
587 |
||
588 |
dirPointer notNil ifTrue:[ |
|
589 |
Lobby unregister:self. |
|
590 |
self closeFile. |
|
591 |
dirPointer := nil |
|
592 |
] |
|
593 |
! ! |
|
594 |
||
8519 | 595 |
!DirectoryStream protectedMethodsFor:'instance release'! |
613 | 596 |
|
597 |
closeFile |
|
598 |
"low level close of a directoryStream" |
|
599 |
%{ |
|
600 |
#ifdef HAS_OPENDIR |
|
601 |
OBJ dp; |
|
602 |
||
1133 | 603 |
if ((dp = __INST(dirPointer)) != nil) { |
604 |
__INST(dirPointer) = nil; |
|
613 | 605 |
closedir( (DIR *)(__FILEVal(dp)) ); |
606 |
} |
|
1631 | 607 |
#else |
608 |
# ifdef WIN32 |
|
609 |
OBJ dp; |
|
610 |
||
611 |
if ((dp = __INST(dirPointer)) != nil) { |
|
612 |
__INST(dirPointer) = nil; |
|
613 |
FindClose( __HANDLEVal(dp) ); |
|
614 |
} |
|
615 |
# endif |
|
613 | 616 |
#endif |
617 |
%} |
|
618 |
! ! |
|
619 |
||
1 | 620 |
!DirectoryStream methodsFor:'private'! |
621 |
||
622 |
openForReading |
|
623 |
"open the file for readonly" |
|
624 |
||
1631 | 625 |
|ok entry| |
1 | 626 |
|
627 |
mode := #readonly. |
|
628 |
%{ |
|
77 | 629 |
#ifdef HAS_OPENDIR |
1 | 630 |
DIR *d; |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
631 |
OBJ path, dp; |
1 | 632 |
|
155 | 633 |
ok = false; |
1133 | 634 |
if (__INST(dirPointer) == nil) { |
11294 | 635 |
path = __INST(pathName); |
636 |
if (__isString(path)) { |
|
637 |
__BEGIN_INTERRUPTABLE__ |
|
638 |
__threadErrno = 0; |
|
639 |
do { |
|
640 |
path = __INST(pathName); |
|
641 |
d = opendir((char *) __stringVal(path)); |
|
642 |
} while ((d == NULL) && (__threadErrno == EINTR)); |
|
643 |
__END_INTERRUPTABLE__ |
|
92 | 644 |
|
11294 | 645 |
if (d == NULL) { |
646 |
__INST(lastErrorNumber) = __mkSmallInteger(__threadErrno); |
|
647 |
} else { |
|
648 |
dp = __MKEXTERNALADDRESS(d); __INST(dirPointer) = dp; __STORE(self, dp); |
|
649 |
ok = true; |
|
650 |
} |
|
651 |
} |
|
1 | 652 |
} |
1631 | 653 |
#else |
654 |
# ifdef WIN32 |
|
11294 | 655 |
# ifndef MAXPATHLEN |
656 |
# define MAXPATHLEN 1024 |
|
657 |
# endif |
|
1631 | 658 |
HANDLE d; |
659 |
OBJ path, dp; |
|
11294 | 660 |
union { |
661 |
char pattern[MAXPATHLEN]; |
|
662 |
wchar_t wpattern[MAXPATHLEN]; |
|
663 |
} uP; |
|
664 |
union { |
|
665 |
WIN32_FIND_DATAA data; |
|
666 |
WIN32_FIND_DATAW wdata; |
|
667 |
} uD; |
|
1631 | 668 |
|
669 |
ok = false; |
|
670 |
if (__INST(dirPointer) == nil) { |
|
11294 | 671 |
path = __INST(pathName); |
672 |
if (__isString(path)) { |
|
673 |
int l = __stringSize(path); |
|
3164
4f37ae9c8961
use strncpy to avoid buffer-overruns
Claus Gittinger <cg@exept.de>
parents:
2999
diff
changeset
|
674 |
|
11294 | 675 |
if (l < (MAXPATHLEN-4)) { |
676 |
strncpy(uP.pattern, __stringVal(path), l); |
|
677 |
strcpy(uP.pattern+l, "\\*"); |
|
678 |
||
679 |
do { |
|
680 |
__threadErrno = 0; |
|
681 |
d = STX_API_CALL2( "FindFirstFile", FindFirstFile, uP.pattern, &uD.data ); |
|
682 |
} while ((d < 0) && (__threadErrno == EINTR)); |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
683 |
|
11294 | 684 |
if (d == INVALID_HANDLE_VALUE) { |
685 |
__INST(lastErrorNumber) = __mkSmallInteger(GetLastError()); |
|
686 |
} else { |
|
687 |
dp = __MKEXTERNALADDRESS(d); __INST(dirPointer) = dp; __STORE(self, dp); |
|
688 |
entry = __MKSTRING( uD.data.cFileName ); |
|
689 |
ok = true; |
|
690 |
} |
|
691 |
} |
|
692 |
} |
|
693 |
else if (__isUnicode16String(path)) { |
|
694 |
int l = __unicode16StringSize(path); |
|
695 |
int i; |
|
6259
164d7efc1eb2
api call stuff (more calls wrapped; care for EINTR)
Claus Gittinger <cg@exept.de>
parents:
5245
diff
changeset
|
696 |
|
11294 | 697 |
if (l < (MAXPATHLEN-4)) { |
698 |
for (i=0; i<l; i++) { |
|
699 |
uP.wpattern[i] = __unicode16StringVal(path)[i]; |
|
700 |
} |
|
701 |
uP.wpattern[i++] = '\\'; |
|
702 |
uP.wpattern[i++] = '*'; |
|
703 |
uP.wpattern[i] = 0; |
|
704 |
||
705 |
do { |
|
706 |
__threadErrno = 0; |
|
707 |
d = STX_API_CALL2( "FindFirstFileW", FindFirstFileW, uP.wpattern, &uD.wdata ); |
|
708 |
} while ((d < 0) && (__threadErrno == EINTR)); |
|
709 |
||
710 |
if (d == INVALID_HANDLE_VALUE) { |
|
711 |
__INST(lastErrorNumber) = __mkSmallInteger(GetLastError()); |
|
712 |
} else { |
|
713 |
dp = __MKEXTERNALADDRESS(d); __INST(dirPointer) = dp; __STORE(self, dp); |
|
714 |
entry = __MKU16STRING( uD.wdata.cFileName ); |
|
715 |
ok = true; |
|
716 |
} |
|
717 |
} |
|
718 |
} |
|
1631 | 719 |
} |
720 |
# endif |
|
1 | 721 |
#endif |
155 | 722 |
%}. |
723 |
ok isNil ifTrue:[ |
|
11294 | 724 |
" |
725 |
opendir not avalable - use slower pipe |
|
726 |
" |
|
727 |
^ PipeStream readingFrom:('cd ' , pathName , '; ls -a') |
|
1 | 728 |
]. |
155 | 729 |
|
730 |
(ok == true) ifTrue:[ |
|
11294 | 731 |
Lobby register:self. |
732 |
entry isNil ifTrue:[ |
|
733 |
self nextLine. "read 1st entry into readAheadEntry buffer" |
|
734 |
] ifFalse:[ |
|
735 |
readAheadEntry := entry. |
|
736 |
]. |
|
737 |
^ self |
|
1 | 738 |
]. |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4298
diff
changeset
|
739 |
dirPointer notNil ifTrue:[^ self errorAlreadyOpen]. |
155 | 740 |
lastErrorNumber notNil ifTrue:[^ self openError]. |
1 | 741 |
^ nil |
742 |
! |
|
743 |
||
744 |
reOpen |
|
8604 | 745 |
"USERS WILL NEVER INVOKE THIS METHOD |
746 |
sent after snapin to reopen streams." |
|
38 | 747 |
|
2 | 748 |
dirPointer := nil. |
749 |
super reOpen |
|
1 | 750 |
! ! |
751 |
||
752 |
!DirectoryStream methodsFor:'testing'! |
|
753 |
||
754 |
atEnd |
|
755 |
"return true, if position is at end" |
|
756 |
||
1664 | 757 |
^ readAheadEntry == nil |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
758 |
! |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
759 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
760 |
isEmpty |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
761 |
"test for if the unread portion of the directory stream is empty. |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
762 |
This query changes the readPointer of the DirectoryStream" |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
763 |
|
2957 | 764 |
|entry| |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
765 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
766 |
[self atEnd] whileFalse:[ |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
767 |
entry := self nextLine. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
768 |
entry asFilename isImplicit ifFalse:[ |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
769 |
^ false. |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
770 |
] |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
771 |
]. |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
772 |
^ true |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
773 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
774 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
775 |
" |
3816
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
776 |
(DirectoryStream directoryNamed:'/') isEmpty |
af3f917c7759
use wrapCalls for FindFirstFile & FindNextFile (win32 - dir-reading)
Claus Gittinger <cg@exept.de>
parents:
3164
diff
changeset
|
777 |
(DirectoryStream directoryNamed:'/var/tmp') isEmpty |
2945
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
778 |
" |
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
779 |
|
1a4f2e0d9f1b
Add #isEmpty, to check for an empty directory.
Stefan Vogel <sv@exept.de>
parents:
2896
diff
changeset
|
780 |
"Modified: 18.9.1997 / 18:05:31 / stefan" |
1 | 781 |
! ! |
782 |
||
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
783 |
!DirectoryStream class methodsFor:'documentation'! |
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
784 |
|
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
785 |
version |
11294 | 786 |
^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.65 2008-10-30 16:25:55 cg Exp $' |
1281
b4b3abffdf32
Support optional signal generation on end of stream.
Stefan Vogel <sv@exept.de>
parents:
1133
diff
changeset
|
787 |
! ! |