summaryrefslogtreecommitdiffstats
path: root/Mac/Compat/getwd.c
blob: 1487fb0e7f2d5246818e2ca65d727cb22a72597a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Get full pathname of current working directory.  The pathname is
   copied to the parameter array 'cwd', and a pointer to this array
   is also returned as function result.  If an error occurred, however,
   the return value is NULL but 'cwd' is filled with an error message.
   
   BUG: expect spectacular crashes when called from a directory whose
   path would be over MAXPATH bytes long (files in such directories are
   not reachable by full pathname).
   
   Starting with the dir ID returned by PBHGetVol, we do successive
   PBGetCatInfo's to get a component of the path until we reach the
   root (recognized by a dir ID of 2).  We move up along the path
   using the dir ID of the parent directory returned by PBGetCatInfo.
   
   Then we catenate the components found in reverse order with the volume
   name (already gotten from PBHGetVol), with intervening and trailing
   colons
   
   The code works correctly on MFS disks (where it always returns the
   volume name) by simply skipping the PBGetCatinfo calls in that case.
   There is a 'bug' in PBGetCatInfo when called for an MFS disk (with
   HFS running): it then seems to call PBHGetVInfo, which returns a
   larger parameter block.  But we won't run into this problem because
   we never call PBGetCatInfo for the root (assuming that PBHGetVol
   still sets the root ID in this case).

   Public domain by Guido van Rossum, CWI, Amsterdam (July 1987).
*/

#include "macdefs.h"

#define ROOTID 2 /* Root directory ID */

char *
getwd(char *cwd)
{
	/* Universal parameter block. */
	union {
		struct HFileInfo f;
		struct DirInfo d;
		struct WDPBRec w;
	} pb;
	char buf[MAXPATH]; /* Buffer to store the name components */
	char *ecwd, *ebuf; /* Pointers to end of used part of cwd and buf */
	int err; /* Error code of last I/O call */
	
	/* First, get the default volume name and working directory ID. */
	
	pb.w.ioNamePtr= (unsigned char *)cwd;
	err= PBHGetVolSync(&pb.w);
	if (err != noErr) {
		sprintf(cwd, "I/O error %d in PBHGetVolSync", err);
		return NULL;
	}
#ifdef TARGET_API_MAC_CARBON
	p2cstrcpy(cwd, (StringPtr)cwd);
	ecwd = strchr(cwd, EOS);
#else
	ecwd= strchr((const char *)p2cstr((unsigned char*)cwd), EOS);
#endif
	ebuf= buf;
	*ebuf = EOS;
	
	/* Next, if at least we're running HFS, walk up the path. */
	
	{
		long dirid= pb.w.ioWDDirID;
		pb.d.ioVRefNum= pb.w.ioWDVRefNum;
		while (dirid != ROOTID) {
			pb.d.ioNamePtr= (unsigned char *) ++ebuf;
			pb.d.ioFDirIndex= -1;
			pb.d.ioDrDirID= dirid;
			err= PBGetCatInfoSync((CInfoPBPtr)&pb.d);
			if (err != noErr) {
				sprintf(cwd, "I/O error %d in PBGetCatInfoSync", err);
				return NULL;
			}
			dirid= pb.d.ioDrParID;
#ifdef TARGET_API_MAC_CARBON
			p2cstrcpy(ebuf, (StringPtr)ebuf);
			ebuf += strlen(ebuf);
#else
			ebuf += strlen((const char *)p2cstr((unsigned char *)ebuf));
#endif
			/* Should check for buf overflow */
		}
	}
	
	/* Finally, reverse the list of components and append it to cwd.
	   Ebuf points at the EOS after last component,
	   and there is an EOS before the first component.
	   If there are no components, ebuf equals buf (but there
	   is still an EOS where it points).
	   Ecwd points at the EOS after the path built up so far,
	   initially the volume name.
	   We break out of the loop in the middle, thus
	   appending a colon at the end in all cases. */
	
	for (;;) {
		*ecwd++ = ':';
		if (ebuf == buf)
			break;
		do { } while (*--ebuf != EOS); /* Find component start */
		strcpy(ecwd, ebuf+1);
		ecwd= strchr(ecwd, EOS);
	}
	*ecwd= EOS;
	return cwd;
}