Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 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 | */ |
| 6 | |
| 7 | #include "dirent.h" |
| 8 | #include "macdefs.h" |
| 9 | |
| 10 | static 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 | |
| 19 | DIR * |
| 20 | opendir(path) |
| 21 | char *path; |
| 22 | { |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 23 | #if TARGET_API_MAC_CARBON |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 24 | 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 Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 48 | 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 Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 62 | pb.d.ioWDProcID= 0; |
| 63 | pb.d.ioWDDirID= 0; |
| 64 | err= PBOpenWD((WDPBPtr)&pb, 0); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 65 | if (err != noErr) { |
| 66 | errno = ENOENT; |
| 67 | return NULL; |
| 68 | } |
| 69 | opened.dirid= pb.d.ioVRefNum; |
| 70 | opened.nextfile= 1; |
| 71 | return &opened; |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 72 | #endif |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Close a directory. |
| 77 | */ |
| 78 | |
| 79 | void |
| 80 | closedir(dirp) |
| 81 | DIR *dirp; |
| 82 | { |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 83 | #if TARGET_API_MAC_CARBON |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 84 | dirp->nextfile = 0; |
| 85 | #else |
| 86 | WDPBRec pb; |
| 87 | |
| 88 | pb.ioVRefNum= dirp->dirid; |
| 89 | (void) PBCloseWD(&pb, 0); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 90 | dirp->dirid= 0; |
| 91 | dirp->nextfile= 0; |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 92 | #endif |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Read the next directory entry. |
| 97 | */ |
| 98 | |
| 99 | struct dirent * |
| 100 | readdir(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 Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 113 | #if TARGET_API_MAC_CARBON |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 114 | pb.d.ioVRefNum= dp->vrefnum; |
| 115 | pb.d.ioDrDirID= dp->dirid; |
| 116 | #else |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 117 | pb.d.ioVRefNum= dp->dirid; |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 118 | pb.d.ioDrDirID= 0; |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 119 | #endif |
| 120 | pb.d.ioFDirIndex= dp->nextfile++; |
| 121 | err= PBGetCatInfo((CInfoPBPtr)&pb, 0); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 122 | if (err != noErr) { |
| 123 | errno = EIO; |
| 124 | return NULL; |
| 125 | } |
Jack Jansen | 74a1e63 | 2000-07-14 22:37:27 +0000 | [diff] [blame] | 126 | #if TARGET_API_MAC_CARBON |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 127 | p2cstrcpy(dir.d_name, (StringPtr)dir.d_name); |
| 128 | #else |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 129 | (void) p2cstr((unsigned char *)dir.d_name); |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 130 | #endif |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 131 | return &dir; |
| 132 | } |