Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 1 | /* Get full pathname of current working directory. The pathname is |
| 2 | copied to the parameter array 'cwd', and a pointer to this array |
| 3 | is also returned as function result. If an error occurred, however, |
| 4 | the return value is NULL but 'cwd' is filled with an error message. |
| 5 | |
| 6 | BUG: expect spectacular crashes when called from a directory whose |
| 7 | path would be over MAXPATH bytes long (files in such directories are |
| 8 | not reachable by full pathname). |
| 9 | |
| 10 | Starting with the dir ID returned by PBHGetVol, we do successive |
| 11 | PBGetCatInfo's to get a component of the path until we reach the |
| 12 | root (recognized by a dir ID of 2). We move up along the path |
| 13 | using the dir ID of the parent directory returned by PBGetCatInfo. |
| 14 | |
| 15 | Then we catenate the components found in reverse order with the volume |
| 16 | name (already gotten from PBHGetVol), with intervening and trailing |
| 17 | colons |
| 18 | |
| 19 | The code works correctly on MFS disks (where it always returns the |
| 20 | volume name) by simply skipping the PBGetCatinfo calls in that case. |
| 21 | There is a 'bug' in PBGetCatInfo when called for an MFS disk (with |
| 22 | HFS running): it then seems to call PBHGetVInfo, which returns a |
| 23 | larger parameter block. But we won't run into this problem because |
| 24 | we never call PBGetCatInfo for the root (assuming that PBHGetVol |
| 25 | still sets the root ID in this case). |
| 26 | |
| 27 | Public domain by Guido van Rossum, CWI, Amsterdam (July 1987). |
| 28 | */ |
| 29 | |
Guido van Rossum | 739267b | 1994-08-29 08:42:37 +0000 | [diff] [blame] | 30 | #include "macdefs.h" |
Jack Jansen | 2d391f2 | 2000-07-14 22:18:32 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 32 | |
| 33 | #define ROOTID 2 /* Root directory ID */ |
| 34 | |
| 35 | char * |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 36 | getwd(char *cwd) |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 37 | { |
| 38 | /* Universal parameter block. */ |
| 39 | union { |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 40 | struct HFileInfo f; |
| 41 | struct DirInfo d; |
| 42 | struct WDPBRec w; |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 43 | } pb; |
| 44 | char buf[MAXPATH]; /* Buffer to store the name components */ |
| 45 | char *ecwd, *ebuf; /* Pointers to end of used part of cwd and buf */ |
| 46 | int err; /* Error code of last I/O call */ |
| 47 | |
| 48 | /* First, get the default volume name and working directory ID. */ |
| 49 | |
| 50 | pb.w.ioNamePtr= (unsigned char *)cwd; |
Jack Jansen | 9e1be97 | 1997-04-08 15:24:17 +0000 | [diff] [blame] | 51 | err= PBHGetVolSync(&pb.w); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 52 | if (err != noErr) { |
Jack Jansen | 9e1be97 | 1997-04-08 15:24:17 +0000 | [diff] [blame] | 53 | sprintf(cwd, "I/O error %d in PBHGetVolSync", err); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 56 | p2cstrcpy(cwd, (StringPtr)cwd); |
| 57 | ecwd = strchr(cwd, EOS); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 58 | ebuf= buf; |
| 59 | *ebuf = EOS; |
| 60 | |
| 61 | /* Next, if at least we're running HFS, walk up the path. */ |
| 62 | |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 63 | { |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 64 | long dirid= pb.w.ioWDDirID; |
| 65 | pb.d.ioVRefNum= pb.w.ioWDVRefNum; |
| 66 | while (dirid != ROOTID) { |
| 67 | pb.d.ioNamePtr= (unsigned char *) ++ebuf; |
| 68 | pb.d.ioFDirIndex= -1; |
| 69 | pb.d.ioDrDirID= dirid; |
Jack Jansen | 9e1be97 | 1997-04-08 15:24:17 +0000 | [diff] [blame] | 70 | err= PBGetCatInfoSync((CInfoPBPtr)&pb.d); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 71 | if (err != noErr) { |
Jack Jansen | 9e1be97 | 1997-04-08 15:24:17 +0000 | [diff] [blame] | 72 | sprintf(cwd, "I/O error %d in PBGetCatInfoSync", err); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
| 75 | dirid= pb.d.ioDrParID; |
Jack Jansen | 2b44ba5 | 2000-06-02 21:38:19 +0000 | [diff] [blame] | 76 | p2cstrcpy(ebuf, (StringPtr)ebuf); |
| 77 | ebuf += strlen(ebuf); |
Guido van Rossum | d4d7728 | 1994-08-19 10:51:31 +0000 | [diff] [blame] | 78 | /* Should check for buf overflow */ |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* Finally, reverse the list of components and append it to cwd. |
| 83 | Ebuf points at the EOS after last component, |
| 84 | and there is an EOS before the first component. |
| 85 | If there are no components, ebuf equals buf (but there |
| 86 | is still an EOS where it points). |
| 87 | Ecwd points at the EOS after the path built up so far, |
| 88 | initially the volume name. |
| 89 | We break out of the loop in the middle, thus |
| 90 | appending a colon at the end in all cases. */ |
| 91 | |
| 92 | for (;;) { |
| 93 | *ecwd++ = ':'; |
| 94 | if (ebuf == buf) |
| 95 | break; |
| 96 | do { } while (*--ebuf != EOS); /* Find component start */ |
| 97 | strcpy(ecwd, ebuf+1); |
| 98 | ecwd= strchr(ecwd, EOS); |
| 99 | } |
| 100 | *ecwd= EOS; |
| 101 | return cwd; |
| 102 | } |