author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 02:21:27 +0100 | |
changeset 607 | a9a526c51233 |
parent 528 | a083413dfbe8 |
child 613 | 0af19c3594fc |
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 |
" |
|
12 |
||
13 |
FileStream subclass:#DirectoryStream |
|
14 |
instanceVariableNames:'dirPointer readAhead' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Streams-External' |
|
18 |
! |
|
19 |
||
88 | 20 |
!DirectoryStream class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
155 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
36 |
version |
|
528
a083413dfbe8
converted version methods from comment-only to returning-a-string
Claus Gittinger <cg@exept.de>
parents:
477
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.26 1995-11-11 14:28:26 cg Exp $' |
88 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Instances of DirectoryStream allow reading a file-directory, |
|
43 |
as if it was a stream of filenames. |
|
44 |
Basically, its an interface to opendir, readdir and closedir. |
|
45 |
" |
|
46 |
! ! |
|
1 | 47 |
|
217 | 48 |
!DirectoryStream primitiveDefinitions! |
206 | 49 |
|
1 | 50 |
%{ |
437 | 51 |
|
1 | 52 |
#include <stdio.h> |
437 | 53 |
#define _STDIO_H_INCLUDED_ |
54 |
||
10 | 55 |
#include <errno.h> |
437 | 56 |
#define _ERRNO_H_INCLUDED_ |
10 | 57 |
|
1 | 58 |
#ifndef transputer |
59 |
# include <sys/types.h> |
|
60 |
# include <sys/stat.h> |
|
437 | 61 |
|
77 | 62 |
# ifdef HAS_OPENDIR |
1 | 63 |
# include <sys/types.h> |
64 |
# ifdef NEXT |
|
65 |
# include <sys/dir.h> |
|
66 |
# else |
|
438 | 67 |
# ifndef VMS |
68 |
# include <dirent.h> |
|
69 |
# endif /* not VMS */ |
|
1 | 70 |
# endif |
71 |
# endif |
|
72 |
#endif |
|
223 | 73 |
|
74 |
/* |
|
75 |
* on some systems errno is a macro ... check for it here |
|
76 |
*/ |
|
77 |
#ifndef errno |
|
78 |
extern errno; |
|
79 |
#endif |
|
80 |
||
1 | 81 |
%} |
206 | 82 |
! ! |
1 | 83 |
|
438 | 84 |
!DirectoryStream primitiveFunctions! |
85 |
||
86 |
%{ |
|
87 |
#ifdef VMS |
|
88 |
||
89 |
/* |
|
90 |
** VMS readdir() routines. |
|
91 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
92 |
** This code has no copyright. |
|
93 |
*/ |
|
94 |
||
95 |
/* 12-NOV-1990 added d_namlen field and special case "." name -GJC@MITECH.COM |
|
96 |
*/ |
|
97 |
||
98 |
#ifndef _STDIO_H_INCLUDED_ |
|
99 |
# include <stdio.h> |
|
100 |
# define _STDIO_H_INCLUDED_ |
|
101 |
#endif |
|
102 |
||
103 |
#ifndef _CTYPE_H_INCLUDED_ |
|
104 |
# include <ctype.h> |
|
105 |
# define _CTYPE_H_INCLUDED_ |
|
106 |
#endif |
|
107 |
||
108 |
#ifndef _ERRNO_H_INCLUDED_ |
|
109 |
# include <errno.h> |
|
110 |
# define _ERRNO_H_INCLUDED_ |
|
111 |
#endif |
|
112 |
||
113 |
#ifndef _DESCRIP_H_INCLUDED_ |
|
114 |
# include <descrip.h> |
|
115 |
# define _DESCRIP_H_INCLUDED_ |
|
116 |
#endif |
|
117 |
||
118 |
#ifndef _RMSDEF_H_INCLUDED_ |
|
119 |
# include <rmsdef.h> |
|
120 |
# define _RMSDEF_H_INCLUDED_ |
|
121 |
#endif |
|
122 |
||
123 |
/* |
|
124 |
* actually, the following has to go into dirent.h ... |
|
125 |
*/ |
|
126 |
/* BEGIN dirent.h |
|
127 |
* |
|
128 |
** Header file for VMS readdir() routines. |
|
129 |
** Written by Rich $alz, <rsalz@bbn.com> in August, 1990. |
|
130 |
** This code has no copyright. |
|
131 |
** |
|
132 |
** You must #include <descrip.h> before this file. |
|
133 |
*/ |
|
134 |
||
135 |
/* 12-NOV-1990 added d_namlen field -GJC@MITECH.COM */ |
|
136 |
||
137 |
/* Data structure returned by READDIR(). */ |
|
138 |
struct dirent { |
|
139 |
char d_name[100]; /* File name */ |
|
140 |
int d_namlen; |
|
141 |
int vms_verscount; /* Number of versions */ |
|
142 |
int vms_versions[20]; /* Version numbers */ |
|
143 |
}; |
|
144 |
||
145 |
/* Handle returned by opendir(), used by the other routines. You |
|
146 |
* are not supposed to care what's inside this structure. */ |
|
147 |
typedef struct _dirdesc { |
|
148 |
long context; |
|
149 |
int vms_wantversions; |
|
150 |
char *pattern; |
|
151 |
struct dirent entry; |
|
152 |
struct dsc$descriptor_s pat; |
|
153 |
} DIR; |
|
154 |
||
155 |
||
156 |
#define rewinddir(dirp) seekdir((dirp), 0L) |
|
157 |
||
158 |
||
159 |
extern DIR *opendir(); |
|
160 |
extern struct dirent *readdir(); |
|
161 |
extern long telldir(); |
|
162 |
extern void seekdir(); |
|
163 |
extern void closedir(); |
|
164 |
extern void vmsreaddirversions(); |
|
165 |
#define _DIRENT_H_INCLUDED_ |
|
166 |
/* |
|
167 |
* END dirent.h |
|
168 |
*/ |
|
169 |
||
170 |
||
171 |
/* Number of elements in vms_versions array */ |
|
172 |
#define VERSIZE(e) (sizeof e->vms_versions / sizeof e->vms_versions[0]) |
|
173 |
||
174 |
/* Linked in later. */ |
|
175 |
extern char *strrchr(); |
|
176 |
extern char *strcpy(); |
|
177 |
/* Don't need this when all these programs are lumped together. RLD |
|
178 |
extern char *malloc(); |
|
179 |
*/ |
|
180 |
||
181 |
/* |
|
182 |
** Open a directory, return a handle for later use. |
|
183 |
*/ |
|
184 |
DIR * |
|
185 |
opendir(name) |
|
186 |
char *name; |
|
187 |
{ |
|
188 |
DIR *dd; |
|
189 |
||
190 |
/* Get memory for the handle, and the pattern. */ |
|
191 |
if ((dd = (DIR *)malloc(sizeof *dd)) == NULL) { |
|
192 |
errno = ENOMEM; |
|
193 |
return NULL; |
|
194 |
} |
|
195 |
||
196 |
if (strcmp(".",name) == 0) name = ""; |
|
197 |
||
198 |
dd->pattern = malloc((unsigned int)(strlen(name) + sizeof "*.*" + 1)); |
|
199 |
if (dd->pattern == NULL) { |
|
200 |
free((char *)dd); |
|
201 |
errno = ENOMEM; |
|
202 |
return NULL; |
|
203 |
} |
|
204 |
||
205 |
/* Fill in the fields; mainly playing with the descriptor. */ |
|
206 |
(void)sprintf(dd->pattern, "%s*.*", name); |
|
207 |
dd->context = 0; |
|
208 |
dd->vms_wantversions = 0; |
|
209 |
dd->pat.dsc$a_pointer = dd->pattern; |
|
210 |
dd->pat.dsc$w_length = strlen(dd->pattern); |
|
211 |
dd->pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
212 |
dd->pat.dsc$b_class = DSC$K_CLASS_S; |
|
213 |
||
214 |
return dd; |
|
215 |
} |
|
216 |
||
217 |
/* |
|
218 |
** Set the flag to indicate we want versions or not. |
|
219 |
*/ |
|
220 |
void |
|
221 |
vmsreaddirversions(dd, flag) |
|
222 |
DIR *dd; |
|
223 |
int flag; |
|
224 |
{ |
|
225 |
dd->vms_wantversions = flag; |
|
226 |
} |
|
227 |
||
228 |
/* |
|
229 |
** Free up an opened directory. |
|
230 |
*/ |
|
231 |
void |
|
232 |
closedir(dd) |
|
233 |
DIR *dd; |
|
234 |
{ |
|
235 |
free(dd->pattern); |
|
236 |
free((char *)dd); |
|
237 |
} |
|
238 |
||
239 |
/* |
|
240 |
** Collect all the version numbers for the current file. |
|
241 |
*/ |
|
242 |
static void |
|
243 |
collectversions(dd) |
|
244 |
DIR *dd; |
|
245 |
{ |
|
246 |
struct dsc$descriptor_s pat; |
|
247 |
struct dsc$descriptor_s res; |
|
248 |
struct dirent *e; |
|
249 |
char *p; |
|
250 |
char buff[sizeof dd->entry.d_name]; |
|
251 |
int i; |
|
252 |
char *text; |
|
253 |
long context; |
|
254 |
||
255 |
/* Convenient shorthand. */ |
|
256 |
e = &dd->entry; |
|
257 |
||
258 |
/* Add the version wildcard, ignoring the "*.*" put on before */ |
|
259 |
i = strlen(dd->pattern); |
|
260 |
text = malloc((unsigned int)(i + strlen(e->d_name)+ 2 + 1)); |
|
261 |
if (text == NULL) |
|
262 |
return; |
|
263 |
(void)strcpy(text, dd->pattern); |
|
264 |
(void)sprintf(&text[i - 3], "%s;*", e->d_name); |
|
265 |
||
266 |
/* Set up the pattern descriptor. */ |
|
267 |
pat.dsc$a_pointer = text; |
|
268 |
pat.dsc$w_length = strlen(text); |
|
269 |
pat.dsc$b_dtype = DSC$K_DTYPE_T; |
|
270 |
pat.dsc$b_class = DSC$K_CLASS_S; |
|
271 |
||
272 |
/* Set up result descriptor. */ |
|
273 |
res.dsc$a_pointer = buff; |
|
274 |
res.dsc$w_length = sizeof buff - 2; |
|
275 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
276 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
277 |
||
278 |
/* Read files, collecting versions. */ |
|
279 |
for (context = 0; e->vms_verscount < VERSIZE(e); e->vms_verscount++) { |
|
280 |
if (lib$find_file(&pat, &res, &context) == RMS$_NMF || context == 0) |
|
281 |
break; |
|
282 |
buff[sizeof buff - 1] = '\0'; |
|
283 |
if (p = strchr(buff, ';')) |
|
284 |
e->vms_versions[e->vms_verscount] = atoi(p + 1); |
|
285 |
else |
|
286 |
e->vms_versions[e->vms_verscount] = -1; |
|
287 |
} |
|
288 |
||
289 |
free(text); |
|
290 |
} |
|
291 |
||
292 |
/* |
|
293 |
** Read the next entry from the directory. |
|
294 |
*/ |
|
295 |
struct dirent * |
|
296 |
readdir(dd) |
|
297 |
DIR *dd; |
|
298 |
{ |
|
299 |
struct dsc$descriptor_s res; |
|
300 |
char *p; |
|
301 |
char buff[sizeof dd->entry.d_name]; |
|
302 |
int i; |
|
303 |
||
304 |
/* Set up result descriptor, and get next file. */ |
|
305 |
res.dsc$a_pointer = buff; |
|
306 |
res.dsc$w_length = sizeof buff - 2; |
|
307 |
res.dsc$b_dtype = DSC$K_DTYPE_T; |
|
308 |
res.dsc$b_class = DSC$K_CLASS_S; |
|
309 |
if (lib$find_file(&dd->pat, &res, &dd->context) == RMS$_NMF |
|
310 |
|| dd->context == 0L) |
|
311 |
/* None left... */ |
|
312 |
return NULL; |
|
313 |
||
314 |
/* Force the buffer to end with a NUL. */ |
|
315 |
buff[sizeof buff - 1] = '\0'; |
|
316 |
for (p = buff; !isspace(*p); p++) |
|
317 |
; |
|
318 |
*p = '\0'; |
|
319 |
||
320 |
/* Skip any directory component and just copy the name. */ |
|
321 |
if (p = strchr(buff, ']')) |
|
322 |
(void)strcpy(dd->entry.d_name, p + 1); |
|
323 |
else |
|
324 |
(void)strcpy(dd->entry.d_name, buff); |
|
325 |
||
326 |
/* Clobber the version. */ |
|
327 |
if (p = strchr(dd->entry.d_name, ';')) |
|
328 |
*p = '\0'; |
|
329 |
||
330 |
dd->entry.d_namlen = strlen(dd->entry.d_name); |
|
331 |
||
332 |
dd->entry.vms_verscount = 0; |
|
333 |
if (dd->vms_wantversions) |
|
334 |
collectversions(dd); |
|
335 |
return &dd->entry; |
|
336 |
} |
|
337 |
||
338 |
/* |
|
339 |
** Return something that can be used in a seekdir later. |
|
340 |
*/ |
|
341 |
long |
|
342 |
telldir(dd) |
|
343 |
DIR *dd; |
|
344 |
{ |
|
345 |
return dd->context; |
|
346 |
} |
|
347 |
||
348 |
/* |
|
349 |
** Return to a spot where we used to be. |
|
350 |
*/ |
|
351 |
void |
|
352 |
seekdir(dd, pos) |
|
353 |
DIR *dd; |
|
354 |
long pos; |
|
355 |
{ |
|
356 |
dd->context = pos; |
|
357 |
} |
|
358 |
||
359 |
#endif /* VMS */ |
|
360 |
%} |
|
361 |
! ! |
|
362 |
||
1 | 363 |
!DirectoryStream methodsFor:'instance release'! |
364 |
||
365 |
closeFile |
|
206 | 366 |
"low level close of a directoryStream" |
1 | 367 |
%{ |
77 | 368 |
#ifdef HAS_OPENDIR |
360 | 369 |
OBJ dp; |
370 |
||
371 |
if ((dp = _INST(dirPointer)) != nil) { |
|
372 |
_INST(dirPointer) = nil; |
|
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
438
diff
changeset
|
373 |
closedir( (DIR *)(__FILEVal(dp)) ); |
206 | 374 |
} |
1 | 375 |
#endif |
376 |
%} |
|
377 |
! ! |
|
378 |
||
379 |
!DirectoryStream class methodsFor:'instance creation'! |
|
380 |
||
381 |
directoryNamed:dirName |
|
382 |
"return a DirectoryStream for directory named dirName, aString" |
|
383 |
||
384 |
|newStream| |
|
385 |
||
386 |
newStream := (self basicNew) pathName:dirName. |
|
387 |
newStream openForReading isNil ifTrue:[^nil]. |
|
388 |
^ newStream |
|
389 |
! ! |
|
390 |
||
391 |
!DirectoryStream methodsFor:'access reading'! |
|
392 |
||
393 |
nextLine |
|
394 |
"return the next filename as a string" |
|
395 |
||
396 |
|prevEntry nextEntry| |
|
397 |
%{ |
|
77 | 398 |
#ifdef HAS_OPENDIR |
1 | 399 |
DIR *d; |
400 |
#ifdef NEXT |
|
401 |
struct direct *dp; |
|
402 |
#else |
|
403 |
struct dirent *dp; |
|
404 |
#endif |
|
92 | 405 |
OBJ dirP; |
1 | 406 |
|
92 | 407 |
if ((dirP = _INST(dirPointer)) != nil) { |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
438
diff
changeset
|
408 |
d = (DIR *)__FILEVal(dirP); |
92 | 409 |
|
362 | 410 |
__BEGIN_INTERRUPTABLE__ |
92 | 411 |
errno = 0; |
155 | 412 |
do { |
413 |
dp = readdir(d); |
|
414 |
} while ((dp == NULL) && (errno == EINTR)); |
|
362 | 415 |
__END_INTERRUPTABLE__ |
92 | 416 |
|
155 | 417 |
if (dp != NULL) { |
418 |
nextEntry = _MKSTRING((char *)(dp->d_name) COMMA_CON); |
|
92 | 419 |
} else { |
420 |
_INST(hitEOF) = true; |
|
421 |
if (errno) { |
|
155 | 422 |
_INST(lastErrorNumber) = _MKSMALLINT(errno); |
92 | 423 |
} |
155 | 424 |
} |
1 | 425 |
} |
426 |
#endif |
|
155 | 427 |
%}. |
428 |
lastErrorNumber notNil ifTrue:[^ self ioError]. |
|
1 | 429 |
prevEntry := readAhead. |
430 |
readAhead := nextEntry. |
|
431 |
^ prevEntry |
|
432 |
! ! |
|
433 |
||
434 |
!DirectoryStream methodsFor:'private'! |
|
435 |
||
436 |
openForReading |
|
437 |
"open the file for readonly" |
|
438 |
||
155 | 439 |
|ok| |
1 | 440 |
|
441 |
mode := #readonly. |
|
442 |
%{ |
|
77 | 443 |
#ifdef HAS_OPENDIR |
1 | 444 |
DIR *d; |
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
445 |
OBJ path, dp; |
1 | 446 |
|
155 | 447 |
ok = false; |
1 | 448 |
if (_INST(dirPointer) == nil) { |
155 | 449 |
path = _INST(pathName); |
450 |
if (__isString(path)) { |
|
362 | 451 |
__BEGIN_INTERRUPTABLE__ |
92 | 452 |
errno = 0; |
155 | 453 |
do { |
454 |
d = opendir((char *) _stringVal(path)); |
|
455 |
} while ((d == NULL) && (errno == EINTR)); |
|
362 | 456 |
__END_INTERRUPTABLE__ |
92 | 457 |
|
155 | 458 |
if (d == NULL) { |
459 |
_INST(lastErrorNumber) = _MKSMALLINT(errno); |
|
460 |
} else { |
|
477
8710aba7876b
oops - making id's real objects requires a store macro
Claus Gittinger <cg@exept.de>
parents:
475
diff
changeset
|
461 |
_INST(dirPointer) = dp = __MKOBJ(d); __STORE(self, dp); |
155 | 462 |
ok = true; |
463 |
} |
|
464 |
} |
|
1 | 465 |
} |
466 |
#endif |
|
155 | 467 |
%}. |
468 |
ok isNil ifTrue:[ |
|
469 |
" |
|
470 |
opendir not avalable - use slower pipe |
|
471 |
" |
|
472 |
^ PipeStream readingFrom:('cd ' , pathName , '; ls -a') |
|
1 | 473 |
]. |
155 | 474 |
|
475 |
(ok == true) ifTrue:[ |
|
476 |
Lobby register:self. |
|
477 |
self nextLine. "read 1st entry into readAhead buffer" |
|
478 |
^ self |
|
1 | 479 |
]. |
155 | 480 |
dirPointer notNil ifTrue:[^ self errorOpen]. |
481 |
lastErrorNumber notNil ifTrue:[^ self openError]. |
|
1 | 482 |
^ nil |
483 |
! |
|
484 |
||
485 |
reOpen |
|
38 | 486 |
"reOpen the stream after image restart" |
487 |
||
2 | 488 |
dirPointer := nil. |
489 |
super reOpen |
|
1 | 490 |
! ! |
491 |
||
492 |
!DirectoryStream methodsFor:'testing'! |
|
493 |
||
494 |
atEnd |
|
495 |
"return true, if position is at end" |
|
496 |
||
497 |
^ readAhead == nil |
|
498 |
! ! |
|
499 |
||
500 |
!DirectoryStream methodsFor:'closing'! |
|
501 |
||
502 |
close |
|
503 |
"close the stream - tell operating system" |
|
504 |
||
505 |
dirPointer notNil ifTrue:[ |
|
155 | 506 |
Lobby unregister:self. |
507 |
self closeFile. |
|
508 |
dirPointer := nil |
|
1 | 509 |
] |
510 |
! ! |