|
请各位高手帮我改一下,dos底下的dir的扩展版,能否再扩展一下,把目录`可执行文件改成和linux底下显示的颜色一样,最好main函数改得面目全非一点!
dos下运行,用tc++或bc++
//DDIR [drive:][path] [/s|/t]
#include <conio.h>
#include <dos.h>
#include <dir.h>
#include "queue.h"
#define MAXLINE 24
const int FA_ANYFILE=0X3F;
const int ESC=27;
int Line_Num=0;
typedef struct Dir_Info
{
long ff_num; //total file
long dd_size; //total byte
long ff_hidden; //total hidden file
long ff_dir; //total directory
} Dir_Info;
/*---------------------------------------*/
int isHidden(struct ffblk *finfo)
{
if ((finfo->ff_attrib & FA_HIDDEN)==FA_HIDDEN)
return 1;
else return 0;
}
/*---------------------------------------*/
int isDirectory(struct ffblk *finfo)
{
if ((finfo->ff_attrib & FA_DIREC)==FA_DIREC)
return 1;
else return 0;
}
//-------------------------------------------//
char *getCurDir(char *path)
{
strcpy(path, "X:\\"); /* fill string with form of response: X:\ */
path[0] = 'A' + getdisk(); /* replace X with current drive letter */
getcurdir(0, path+3); /* fill rest of string with current directory */
return(path);
}
/*-----------------------------------------*/
void pause(char *path)
{
char ch;
if (Line_Num==MAXLINE)
{
Line_Num=0;
printf("Press any key to continue...");
ch=getch();
if (ch==ESC) abort();
printf("\n");
Line_Num++;
pause(path);
printf("continuing %s\n", path);
Line_Num++;
pause(path);
}
}
//-------------------------------------------//
// set path like c:\dos\*.* //
//-------------------------------------------//
void fmtPath(char *path)
//remove \ from the end of 'path'
{
int l;
l=strlen(path)-1;
while (path[l]=='\\')
{
path[l]='\0';
l--;
}
}
//-------------------------------------------//
void setPath(char *path)
{
int strl;
strl=strlen(path);
if (path[strl-1]=='\\')
{
path[strl-1]='\0';
strcat(path, "\\*.*");
}
else if ((path[strl-1]==':')||(path[strl-1]=='.'))
strcat(path, "*.*");
else
strcat(path, "\\*.*");
}
/*---------------------------------------*/
// get the directory information //
/*---------------------------------------*/
void getDirInfo(char *dirpath, Dir_Info &di)
{
int err;
int seq; //control display position
struct ffblk finfo;
char path[MAXPATH];
di.ff_num=0;
di.ff_hidden=0;
di.ff_dir=0;
di.dd_size=0;
strcpy(path, dirpath);
setPath(path);
err=findfirst(path,&finfo,FA_ANYFILE);
while(!err)
{
if (!isDirectory(&finfo))
di.ff_num++;
di.dd_size+=finfo.ff_fsize;
if (isHidden(&finfo))
di.ff_hidden++;
if (isDirectory(&finfo))
di.ff_dir++;
err=findnext(&finfo);
}
}
//----------------------------------------------//
void getDirSubInfo(char *dp, Dir_Info &dinfo)
{
Dir_Info Total_Dir_Info, dir_info;
Queue q;
char path[MAXPATH],qpath[MAXPATH],tempath[MAXPATH];
struct ffblk finfo;
int err;
Total_Dir_Info.ff_num=0;
Total_Dir_Info.ff_hidden=0;
Total_Dir_Info.ff_dir=0;
Total_Dir_Info.dd_size=0;
InitQueue(q);
EnQueue(q,dp);
while (!QueueEmpty(q))
{
DeQueue(q, path);
getDirInfo(path,dir_info);
Total_Dir_Info.ff_num+=dir_info.ff_num;;
Total_Dir_Info.ff_hidden+=dir_info.ff_hidden;
Total_Dir_Info.ff_dir+=dir_info.ff_dir;
Total_Dir_Info.dd_size+=dir_info.dd_size;
strcpy(tempath,path);
setPath(path);
err=findfirst(path,&finfo,FA_ANYFILE);
while (!err)
{
if (isDirectory(&finfo)&&strcmp(finfo.ff_name,".")
&&strcmp(finfo.ff_name,".."))
{
strcpy(qpath,tempath);
fmtPath(qpath);
strcat(qpath,"\\");
strcat(qpath,finfo.ff_name);
EnQueue(q,qpath);
}
err=findnext(&finfo);
} //while
} //while
dinfo=Total_Dir_Info;
}
//-----------------------------------------//
// Show the directory information //
//-----------------------------------------//
void showDirInfo(char *path, Dir_Info &dir_info)
{
pause(path);
printf("\n\t%ld file(s)\t %ld bytes\n", dir_info.ff_num, dir_info.dd_size);
Line_Num++;
pause(path);
Line_Num++;
pause(path);
/*
printf("\t%ld hidden \n", dir_info.ff_hidden);
Line_Num++;
pause(path);
*/
printf("\t%ld dir(s)\n", dir_info.ff_dir);
Line_Num++;
pause(path);
}
//-------------------------------------------//
// display a file in two column //
//-------------------------------------------//
void display(struct ffblk finfo, int seq)
{
char drive[MAXDRIVE];
char dir[MAXDIR];
char file[MAXFILE+18],temfile[MAXFILE+18];
char ext[MAXEXT];
int flags;
int i,slen;
int year,month,day;
int tem;
if (!isDirectory(&finfo))
{
fnsplit(finfo.ff_name,drive,dir,file,ext);
slen=strlen(ext);
if (slen)
{
for (i=0;i<slen;i++)
ext=ext[i+1];
slen=strlen(ext);
for (i=1;i<=3-slen; i++)
strcat(ext," ");
}
else strcpy(ext, " ");
slen=strlen(file);
for (i=slen;i<=11;i++)
file=' ';
file='\0';
strcat(file, ext);
strcat(file," ");
}
else
{
strcpy(file,finfo.ff_name);
slen=strlen(file);
for (i=slen;i<=10;i++)
file=' ';
file='\0';
strcat(file, " <DIR>");
}
//insert a star(*) at the hidden file front
if (isHidden(&finfo))
strcpy(temfile,"*");
else
strcpy(temfile," ");
strcat(temfile, file);
strcpy(file, temfile);
day=finfo.ff_fdate & 0X1F;
month=(finfo.ff_fdate>>5)&0XF;
year=1980+((finfo.ff_fdate>>9)&0X7F);
if (!seq)
{
gotoxy(0,wherey());
if (!isDirectory(&finfo))
printf("%s%10ld %4d-%2d-%2d",file,finfo.ff_fsize,year,month,day);
else
printf("%s %4d-%2d-%2d",file,year,month,day);
}
else
{
gotoxy(40,wherey());
if (!isDirectory(&finfo))
printf("%c%s%10ld %4d-%2d-%2d",179,file,finfo.ff_fsize,year,month,day);
else
printf("%c%s %4d-%2d-%2d",179,file,year,month,day);
printf("\n");
Line_Num++;
pause("");
}
}
//----------------------------------------------//
//Display specified Directory not include subDir//
//----------------------------------------------//
void ShowDir(char *dirpath)
{
int err;
int seq; //control display position
struct ffblk finfo;
Dir_Info dir_info;
char path[MAXPATH];
strcpy(path, dirpath);
if ((strlen(path)==1)&&(path[0]=='.'))
getCurDir(path);
getDirInfo(path, dir_info);
printf("\nDirectory of %s\n", path);
Line_Num++;
pause(path);
Line_Num++;
pause(path);
setPath(path);
seq=0;
err=findfirst(path,&finfo,FA_ANYFILE);
while(!err)
{
pause(path);
if ((finfo.ff_attrib & FA_LABEL)!=FA_LABEL)
{
display(finfo,seq);
seq=!seq;
}
err=findnext(&finfo);
}
if (seq)
{
printf("\n");
Line_Num++;
pause(path);
}
showDirInfo(path,dir_info);
}
//-------------------------------------------//
void ShowDirSub(char *dp)
{
Queue q;
char path[MAXPATH],qpath[MAXPATH],tempath[MAXPATH];
struct ffblk finfo;
int err;
Dir_Info dinfo;
InitQueue(q);
EnQueue(q,dp);
while (!QueueEmpty(q))
{
DeQueue(q, path);
ShowDir(path);
strcpy(tempath,path);
setPath(path);
err=findfirst(path,&finfo,FA_ANYFILE);
while (!err)
{
if (isDirectory(&finfo)&&strcmp(finfo.ff_name,".")
&&strcmp(finfo.ff_name,".."))
{
strcpy(qpath,tempath);
fmtPath(qpath);
strcat(qpath,"\\");
strcat(qpath,finfo.ff_name);
EnQueue(q,qpath);
}
err=findnext(&finfo);
} //while
} //while
getDirSubInfo(dp, dinfo);
printf("\nTotal:");
Line_Num++;
pause(dp);
showDirInfo(dp, dinfo);
}
//-------------------------------------------//
void showCalDirSub(char *dp)
{
Dir_Info Total_Dir_Info, dir_info;
Queue q;
char path[MAXPATH],qpath[MAXPATH],tempath[MAXPATH];
struct ffblk finfo;
int err;
int show_count=0;
Total_Dir_Info.ff_num=0;
Total_Dir_Info.ff_hidden=0;
Total_Dir_Info.ff_dir=0;
Total_Dir_Info.dd_size=0;
InitQueue(q);
EnQueue(q,dp);
while (!QueueEmpty(q))
{
DeQueue(q, path);
getDirInfo(path,dir_info);
printf("%s", path);
showDirInfo(path,dir_info);
show_count++;
Total_Dir_Info.ff_num+=dir_info.ff_num;;
Total_Dir_Info.ff_hidden+=dir_info.ff_hidden;
Total_Dir_Info.ff_dir+=dir_info.ff_dir;
Total_Dir_Info.dd_size+=dir_info.dd_size;
strcpy(tempath,path);
setPath(path);
err=findfirst(path,&finfo,FA_ANYFILE);
while (!err)
{
if (isDirectory(&finfo)&&strcmp(finfo.ff_name,".")
&&strcmp(finfo.ff_name,".."))
{
strcpy(qpath,tempath);
fmtPath(qpath);
strcat(qpath,"\\");
strcat(qpath,finfo.ff_name);
EnQueue(q,qpath);
}
err=findnext(&finfo);
} //while
} //while
if (show_count>1)
{
printf("\nDirectory %s", dp);
Line_Num++;
pause(dp);
showDirInfo(dp, Total_Dir_Info);
}
}
//-------------------------------------------//
void showCopyRight()
{
printf("DDIR of 15-01-2002\n");
printf("Version 0.5\n");
printf("Copyright (c) 2002 by duotaiya\n\n");
printf("\nDDIR [drive:][path] [/s|/t]\n\n");
printf("\tdrive: Specifies a drive.\n");
printf("\tpath : Specifies a path.\n");
printf("\t/s : Display directory include sub-directory in two column.\n");
printf("\t/t : Only display directory information.\n");
printf("\n\t\tTel: 13862295051\n");
printf("\t\tE-mail: [email protected]\n");
}
//-------------------------------------------//
void main(int argc, char *argv[])
{
char curdir[MAXPATH];
getCurDir(curdir);
switch (argc)
{
case 1: ShowDir(curdir); break;
case 2:
if (!strcmp(argv[1], "/?"))
showCopyRight();
else if (!strcmp(strlwr(argv[1]),"/s"))
ShowDirSub(curdir);
else if (!strcmp(strlwr(argv[1]),"/t"))
showCalDirSub(curdir);
else ShowDir(argv[1]);
break;
case 3:
if (!strcmp(strlwr(argv[2]),"/s"))
ShowDirSub(argv[1]);
else if (!strcmp(strlwr(argv[2]),"/t"))
showCalDirSub(argv[1]);
else
{
printf("WARNING!\n");
showCopyRight();
}
break;
default:
printf("WARNING!\n");
showCopyRight();
} //switch
} //main |
|