⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cd.c

📁 这是个重量级的东西
💻 C
字号:
/*
	cd.c copyright by robin @2004.7.3
*/

#include "..\typedef\type.h"

#include "..\file\file.h"
#include "..\file\fat.h"
#include "..\file\fat16.h"
#include "..\txlib\txlib.h"
#include "..\hd\hd.h"

#include "txstdio.h"
#include "memory.h"
#include "string.h"


#define  printf kprintf
#define CD_DEBUG

extern PVOL CurVol;
extern DWORD CurDir;
extern VOL SysVolume[8];

DWORD SearchRoot(char * strFileName,DWORD dwType,PVOL pVol,DWORD dwCurDir);

void CdUsage()
{
	printf("Usage : cd [path]\n");
}
int cd(int argc,char ** argv)
{

	char * path = 0;
	char * file = 0;
	PFATV pFATV = 0;
	DWORD dwIsDone		= 0;
	DWORD dwIsPartion	= 0;
	
	DWORD dwCluster		= 0;
	DWORD dwSector		= 0;
	DWORD fatnum		= 0;
	DWORD fatoff		= 0;
	DWORD dwLeng		= 0;
	FATSearchDir	search			= 0;
	FATFirstSectorofCluster first	= 0;
	int i;

	if(argc > 2) return 0;
	
	for( i = 1 ; i < argc ; i++)
	{
		if(argv[i][0] == '-' || argv[i][0] == '/')
		{
			//printf("some parameter invalid,check it out!");
			return 0;
		}
		else{
			path = argv[i];
		}
	}

	if(argc == 1){
		CdUsage();
		return 1;
	}

	// 去掉前导空格
	for( ; IS_WHITE_SPACE(*path) && *path != 0 ;path++)
		;

	file = path;
	upcase(file);
#ifdef CD_DEBUG
		printf("dir name : %s current dir cluster : %d\n",file,CurDir);
#endif // CD_DEBUG
	// 是否有分区前导
	for( ; *file ; file++)
	{
		if( *file == ':') {
			*file = 0;
			file++;
			dwIsPartion = 1;
#ifdef CD_DEBUG
			printf("have to chang partion.\n");
#endif // CD_DEBUG
			break;
		}
	}

	// 没有分区则路径恢复
	if(dwIsPartion == 0)
		file = path;
	// 确定分区
	if(dwIsPartion == 1)
	{
#ifdef CD_DEBUG
			printf("changing partion...\n");
#endif // CD_DEBUG
		for( i = 0 ; i < 8 && SysVolume[i].dwVolType != 0 ; i++)
		{
			if(strcmp(path,SysVolume[i].strName) == 0){
				CurVol = &(SysVolume[i]);
				CurDir = 0;
#ifdef CD_DEBUG
			printf("chang partion complete! CurDir : %d\n",CurDir);
#endif // CD_DEBUG
				dwIsPartion = 0;
				break;
			}
		}
		// 找不到分区
		if(dwIsPartion)
		{
#ifdef CD_DEBUG
			printf("can not found partion!");
#endif // CD_DEBUG
			return 0;
		}
	}

	search	= ((PFATV)(CurVol->dwVolParam))->SearchDir		;
	first	= ((PFATV)(CurVol->dwVolParam))->FirstSectorofCluster;
	
	// 去掉前导'\'

	for( ; *file == '\\' ; file++) ;

	path = file;
	dwCluster = CurDir;
#ifdef CD_DEBUG
		printf("dir name : %s\n",file);
#endif // CD_DEBUG

	while(!dwIsDone)
	{	

		file = path;
		for( ; *path ; path++)
		{
			if(*path == '\\'){
				*path = 0;
				path++;
				break;
			}
		}

#ifdef CD_DEBUG
		printf("dir name : %s current path : %d\n",file,*path & 0xff);
#endif // CD_DEBUG
		if(dwCluster == 0)
		{
#ifdef CD_DEBUG
			printf("dir in the root region!\nSearching dir...\n");
#endif // CD_DEBUG

			dwCluster = SearchRoot(file,1,CurVol,((PFATPB)(CurVol->dwVolParam))->dwRootDirSec);

#ifdef CD_DEBUG
			printf("dir Search complete! cluster %d \n", dwCluster);
#endif // CD_DEBUG
		}
		else
		{
			//dwSector = first(dwCluster,((PFATPB)(CurVol->dwVolParam)));
#ifdef CD_DEBUG
			printf("dir in the data region!\nCluster : %d Searching dir... \n",dwCluster);
#endif // CD_DEBUG

			dwCluster = search(file,dwCluster,1,&dwLeng,((PFATPB)(CurVol->dwVolParam)));

#ifdef CD_DEBUG
			printf("dir Search complete! cluster %d \n", dwCluster);
#endif // CD_DEBUG
		}

		if(dwCluster == -1 )
		{
			printf("bad path or file name!\n");
			return 0;
		}

		if(*path == 0) dwIsDone = 1;
	}
	
	CurDir = dwCluster;

	return 1;
}

char * 	ToDirName(char *,char *);

DWORD SearchRoot(char * strFileName,DWORD dwType,PVOL pVol,DWORD dwCurDir)
{
	PFATV pFATV = 0;
	DAP dap;
	char buf[512];
	PDIRENTRY pDir = 0;
	char name[16];
	DWORD i,j;

	dap.dwSecCnt	= 1;
	dap.pBuffer		= buf;
	
	memset(name,' ',16);
	ToDirName(name,strFileName);
	name[11] = 0;

	pFATV = (PFATV)(pVol->dwVolParam);
	for( i = 0 ; i < pFATV->FATParamBlk.dwRootDirSectors ; i++)
	{
		dap.dwDiskAddr	= dwCurDir + i;

		HDRead(pVol->dwDevID,&dap);

		pDir = (PDIRENTRY)buf;

		for ( j = 0 ; j < 16  ; j++)
		{
			if(pDir[j].dir_Name[0] == 0x00)
				goto sum;
			if(pDir[j].dir_Name[0] == 0xe5)
				continue;
			if(memcmp(name,pDir[j].dir_Name,11) == 0 && ((pDir[j].dir_Attr & 0x10)))
			{
				return pDir[j].dir_FstClusHI * 0x10000 + pDir[j].dir_FstClusLO;
			}
		}
	}
sum:
	return -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -