// macDIR.c 10/3/90 G.Garchar

#include	"Resources.h"
#include	"memory.h"
#include	"Files.h"
#include	"Types.h"
#include	"dosintosh.h"

void macDIR(char drive, char line[], int (*callback[])());

main(char *input, int(*callback[])(),short prefrefnum) {
	unsigned char path[256];
	unsigned char drive = 'C'; // What's this?
	
	(*callback[ParsePathCallBack])(input,path);
		
	macDIR(drive, path, callback);
	}

#include 	"Common.c"		// done after the main() proc!!!

	
short PrintSpec(short volNo, long dirID, char spec[], int (*callback[])()) {
	Boolean match;
	char name[256], nameExt[256];
	char specExt[256];
	char pString[256];		
	DateTimeRec date;
	HFileInfo pBlock;
	short fileCount = 0;
	
	pBlock.ioNamePtr = pString; // RESERVE STRING MEMORY
	pBlock.ioVRefNum = volNo;
	pBlock.ioDirID = dirID;
	pBlock.ioFDirIndex = 1;		
	SplitDot(spec, specExt);		
	/* INDEX THRU DIRECTORY */
	while (PBGetCatInfo((CInfoPBPtr)&pBlock, false) == noErr) { 
		p2cstr(pBlock.ioNamePtr);
		strcpy(name, pBlock.ioNamePtr);	
		SplitDot(name, nameExt);
		match = false;
		if (WildMatch(spec, name)) { // MATCH?
			if (*specExt == '\0') // NO ".EXT" IN FILESPEC
				match = true;
			else { // FILESPEC HAS AN EXTENSION
				if (*nameExt == '\0') strcpy(nameExt, ".");
				if (WildMatch(specExt, nameExt))
					match = true;
				}
			}
		if (match) { // PRINT INFO
			print(string(nameStr1), pBlock.ioNamePtr);
			if (pBlock.ioFlAttrib & 0x10) // A FOLDER?
				print(string(dirStr));
			else // A FILE!
				print(string(lengthStr), pBlock.ioFlPyLen + pBlock.ioFlRPyLen);
			Secs2Date(pBlock.ioFlMdDat, &date);
			print(string(dateStr), // PRINT DATE
				date.month,
				date.day,
				date.year,
				date.hour,
				date.minute);
			fileCount += 1;
			}
		pBlock.ioDirID = dirID;
		pBlock.ioFDirIndex += 1;
		}		
	return fileCount;
	}
									

														void macDIR(char drive, char line[], int (*callback[])()) {
	char spec[256];
	char path[256];
	char root[256] = "";
	long dirID;
	long freeBytes;
	short fileCount;
	short result;
	short volNo;
	
	result = GetSpec(line, path, spec, &dirID, &volNo);
	if (result == 1) print(string(invVolStr));
	else if (result == 2) print(string(invDirStr));
	else { // DISPLAY DIRECTORY
		GetVolNo(0, volNo, root, &freeBytes); // GET FREEBYTES, ROOT
		print(string(headerDirStr1), drive, root);
		print(string(headerDirStr2), path);
		fileCount = PrintSpec(volNo, dirID, spec, callback);
		print(string(footerDirStr), fileCount, freeBytes);
		}
	}