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