blob: 74b89842ec6f5493550f65cb44a58cdb7fde708e [file] [log] [blame]
Guido van Rossumd4d77281994-08-19 10:51:31 +00001/*
2 * Macintosh version of UNIX directory access package
3 * (opendir, readdir, closedir).
4 * Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
5 */
6
7#include "dirent.h"
8#include "macdefs.h"
9
10static DIR opened;
11
12/*
13 * Open a directory. This means calling PBOpenWD.
14 * The value returned is always the address of opened, or NULL.
15 * (I have as yet no use for multiple open directories; this could
16 * be implemented by allocating memory dynamically.)
17 */
18
19DIR *
20opendir(path)
21 char *path;
22{
23 union {
24 WDPBRec d;
25 VolumeParam v;
26 } pb;
27 char ppath[MAXPATH];
28 short err;
29
30 if (opened.nextfile != 0) {
31 errno = EBUSY;
32 return NULL; /* A directory is already open. */
33 }
34 strncpy(ppath+1, path, ppath[0]= strlen(path));
35 pb.d.ioNamePtr= (unsigned char *)ppath;
36 pb.d.ioVRefNum= 0;
37 if (hfsrunning()) {
38 pb.d.ioWDProcID= 0;
39 pb.d.ioWDDirID= 0;
40 err= PBOpenWD((WDPBPtr)&pb, FALSE);
41 }
42 else {
43 pb.v.ioVolIndex= 0;
44 err= PBGetVInfo((ParmBlkPtr)&pb, FALSE);
45 }
46 if (err != noErr) {
47 errno = ENOENT;
48 return NULL;
49 }
50 opened.dirid= pb.d.ioVRefNum;
51 opened.nextfile= 1;
52 return &opened;
53}
54
55/*
56 * Close a directory.
57 */
58
59void
60closedir(dirp)
61 DIR *dirp;
62{
63 if (hfsrunning()) {
64 WDPBRec pb;
65
66 pb.ioVRefNum= dirp->dirid;
67 (void) PBCloseWD(&pb, FALSE);
68 }
69 dirp->dirid= 0;
70 dirp->nextfile= 0;
71}
72
73/*
74 * Read the next directory entry.
75 */
76
77struct dirent *
78readdir(dp)
79 DIR *dp;
80{
81 union {
82 DirInfo d;
83 FileParam f;
84 HFileInfo hf;
85 } pb;
86 short err;
87 static struct dirent dir;
88
89 dir.d_name[0]= 0;
90 pb.d.ioNamePtr= (unsigned char *)dir.d_name;
91 pb.d.ioVRefNum= dp->dirid;
92 pb.d.ioFDirIndex= dp->nextfile++;
93 pb.d.ioDrDirID= 0;
94 if (hfsrunning())
95 err= PBGetCatInfo((CInfoPBPtr)&pb, FALSE);
96 else
97 err= PBGetFInfo((ParmBlkPtr)&pb, FALSE);
98 if (err != noErr) {
99 errno = EIO;
100 return NULL;
101 }
102 (void) p2cstr((unsigned char *)dir.d_name);
103 return &dir;
104}