nfullpath.c 1.41 KB
Newer Older
1 2 3 4
/* GET FULL PATHNAME OF A FILE.
** Original by Guido, modified by Jack to handle FSSpecs 
*/

5 6
#include <string.h>

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <Files.h>

#include "nfullpath.h"

/* Mac file system parameters */
#define MAXPATH 256	/* Max path name length+1 */
#define SEP ':'		/* Separator in path names */

/* Macro to find out whether we can do HFS-only calls: */
#define FSFCBLen (* (short *) 0x3f6)
#define hfsrunning() (FSFCBLen > 0)

int
nfullpath(fsp, retbuf)
	FSSpec *fsp;
	char *retbuf;
{
	union {
		HFileInfo f;
		DirInfo d;
		WDPBRec w;
		VolumeParam v;
	} pb;
30
	char cwd[2*MAXPATH];
31 32 33 34
	unsigned char namebuf[MAXPATH];
	short err;
	int dir;
	long dirid;
35
	char *next = cwd + sizeof cwd - 1;
36
	int len;
37
	int need_sep = 1;
38 39 40 41
	
	if (!hfsrunning())
		return -1;
	
42
	dir = fsp->vRefNum;
43 44 45 46
	dirid = fsp->parID;
	/* Stuff the filename into the buffer */
	len = fsp->name[0];
	*next = '\0';
47 48
	next -= len;
	memcpy(next, fsp->name+1, len);
49
	
50 51 52 53 54
	while (dirid != fsRtParID) {
		pb.d.ioNamePtr = namebuf;
		pb.d.ioVRefNum = dir;
		pb.d.ioFDirIndex = -1;
		pb.d.ioDrDirID = dirid;
55
		err= PBGetCatInfo((CInfoPBPtr)&pb.d, 0);
56
		if (err != noErr)
57
			return err;
58 59
		*--next = SEP;
		len = namebuf[0];
60 61 62 63
		if ( len + strlen(next) >= MAXPATH )
			return -1;
		next -= len;
		memcpy(next, (char *)namebuf+1, len);
64 65
		dirid = pb.d.ioDrParID;
		need_sep = 0;
66
	}
67
	
68
	strcpy(retbuf, next);
69 70 71 72 73
	if (need_sep) {
		next = strchr(retbuf, '\0');
		*next++ = SEP;
		*next++ = '\0';
	}
74 75
	return 0;
}