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