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