Added 'description' class attribute to every command class (to help the
[python/dscho.git] / Mac / Compat / opendir.c
blob74b89842ec6f5493550f65cb44a58cdb7fde708e
1 /*
2 * Macintosh version of UNIX directory access package
3 * (opendir, readdir, closedir).
4 * Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
5 */
7 #include "dirent.h"
8 #include "macdefs.h"
10 static DIR opened;
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.)
19 DIR *
20 opendir(path)
21 char *path;
23 union {
24 WDPBRec d;
25 VolumeParam v;
26 } pb;
27 char ppath[MAXPATH];
28 short err;
30 if (opened.nextfile != 0) {
31 errno = EBUSY;
32 return NULL; /* A directory is already open. */
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);
42 else {
43 pb.v.ioVolIndex= 0;
44 err= PBGetVInfo((ParmBlkPtr)&pb, FALSE);
46 if (err != noErr) {
47 errno = ENOENT;
48 return NULL;
50 opened.dirid= pb.d.ioVRefNum;
51 opened.nextfile= 1;
52 return &opened;
56 * Close a directory.
59 void
60 closedir(dirp)
61 DIR *dirp;
63 if (hfsrunning()) {
64 WDPBRec pb;
66 pb.ioVRefNum= dirp->dirid;
67 (void) PBCloseWD(&pb, FALSE);
69 dirp->dirid= 0;
70 dirp->nextfile= 0;
74 * Read the next directory entry.
77 struct dirent *
78 readdir(dp)
79 DIR *dp;
81 union {
82 DirInfo d;
83 FileParam f;
84 HFileInfo hf;
85 } pb;
86 short err;
87 static struct dirent dir;
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;
102 (void) p2cstr((unsigned char *)dir.d_name);
103 return &dir;