blob: a015609a186df5bfa446f1aab1becd9e8346fa94 [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{
Jack Jansen74a1e632000-07-14 22:37:27 +000023#if TARGET_API_MAC_CARBON
Jack Jansen2b44ba52000-06-02 21:38:19 +000024 Str255 ppath;
25 FSSpec fss;
26 int plen;
27 OSErr err;
28
29 if (opened.nextfile != 0) {
30 errno = EBUSY;
31 return NULL; /* A directory is already open. */
32 }
33 plen = strlen(path);
34 c2pstrcpy(ppath, path);
35 if ( ppath[plen] != ':' )
36 ppath[++plen] = ':';
37 ppath[++plen] = 'x';
38 ppath[0] = plen;
39 if( (err = FSMakeFSSpec(0, 0, ppath, &fss)) < 0 && err != fnfErr ) {
40 errno = EIO;
41 return NULL;
42 }
43 opened.dirid = fss.parID;
44 opened.vrefnum = fss.vRefNum;
45 opened.nextfile = 1;
46 return &opened;
47#else
Guido van Rossumd4d77281994-08-19 10:51:31 +000048 union {
49 WDPBRec d;
50 VolumeParam v;
51 } pb;
52 char ppath[MAXPATH];
53 short err;
54
55 if (opened.nextfile != 0) {
56 errno = EBUSY;
57 return NULL; /* A directory is already open. */
58 }
59 strncpy(ppath+1, path, ppath[0]= strlen(path));
60 pb.d.ioNamePtr= (unsigned char *)ppath;
61 pb.d.ioVRefNum= 0;
Jack Jansen2b44ba52000-06-02 21:38:19 +000062 pb.d.ioWDProcID= 0;
63 pb.d.ioWDDirID= 0;
64 err= PBOpenWD((WDPBPtr)&pb, 0);
Guido van Rossumd4d77281994-08-19 10:51:31 +000065 if (err != noErr) {
66 errno = ENOENT;
67 return NULL;
68 }
69 opened.dirid= pb.d.ioVRefNum;
70 opened.nextfile= 1;
71 return &opened;
Jack Jansen2b44ba52000-06-02 21:38:19 +000072#endif
Guido van Rossumd4d77281994-08-19 10:51:31 +000073}
74
75/*
76 * Close a directory.
77 */
78
79void
80closedir(dirp)
81 DIR *dirp;
82{
Jack Jansen74a1e632000-07-14 22:37:27 +000083#if TARGET_API_MAC_CARBON
Jack Jansen2b44ba52000-06-02 21:38:19 +000084 dirp->nextfile = 0;
85#else
86 WDPBRec pb;
87
88 pb.ioVRefNum= dirp->dirid;
89 (void) PBCloseWD(&pb, 0);
Guido van Rossumd4d77281994-08-19 10:51:31 +000090 dirp->dirid= 0;
91 dirp->nextfile= 0;
Jack Jansen2b44ba52000-06-02 21:38:19 +000092#endif
Guido van Rossumd4d77281994-08-19 10:51:31 +000093}
94
95/*
96 * Read the next directory entry.
97 */
98
99struct dirent *
100readdir(dp)
101 DIR *dp;
102{
103 union {
104 DirInfo d;
105 FileParam f;
106 HFileInfo hf;
107 } pb;
108 short err;
109 static struct dirent dir;
110
111 dir.d_name[0]= 0;
112 pb.d.ioNamePtr= (unsigned char *)dir.d_name;
Jack Jansen74a1e632000-07-14 22:37:27 +0000113#if TARGET_API_MAC_CARBON
Jack Jansen2b44ba52000-06-02 21:38:19 +0000114 pb.d.ioVRefNum= dp->vrefnum;
115 pb.d.ioDrDirID= dp->dirid;
116#else
Guido van Rossumd4d77281994-08-19 10:51:31 +0000117 pb.d.ioVRefNum= dp->dirid;
Guido van Rossumd4d77281994-08-19 10:51:31 +0000118 pb.d.ioDrDirID= 0;
Jack Jansen2b44ba52000-06-02 21:38:19 +0000119#endif
120 pb.d.ioFDirIndex= dp->nextfile++;
121 err= PBGetCatInfo((CInfoPBPtr)&pb, 0);
Guido van Rossumd4d77281994-08-19 10:51:31 +0000122 if (err != noErr) {
123 errno = EIO;
124 return NULL;
125 }
Jack Jansen74a1e632000-07-14 22:37:27 +0000126#if TARGET_API_MAC_CARBON
Jack Jansen2b44ba52000-06-02 21:38:19 +0000127 p2cstrcpy(dir.d_name, (StringPtr)dir.d_name);
128#else
Guido van Rossumd4d77281994-08-19 10:51:31 +0000129 (void) p2cstr((unsigned char *)dir.d_name);
Jack Jansen2b44ba52000-06-02 21:38:19 +0000130#endif
Guido van Rossumd4d77281994-08-19 10:51:31 +0000131 return &dir;
132}