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