- 论坛徽章:
- 0
|
学生想做一个检查两个目录程序是否一致的小程序。主要是通过对文件数量和文件最后修改时间来判断。。
我现在想把程序中发现的那个错误文件名给提取出来。(printf出来)。如何做。
一下是代码。写的很差,希望各位老师海涵。
#include <sys/stat.h>
#include <unistd.h>
#include <ftw.h>
#define MAXFILENUM 150000
struct stat info;
struct stat info1;
int count=0;
int count_temp=0;
void num()
{
count++;
}
void num_temp()
{
count_temp++;
}
int myftw_temp(const char * file, const struct stat *sb, int flag)
{
if(flag==FTW_F)
{
num();
memset(&info,0,sizeof(struct stat));
stat(file, &info);
printf("\n%s",ctime(&info.st_mtime));
printf("%s\n",file); //打印文件名
}
return 0;
}
int myftw_temp1(const char * file, const struct stat *sb, int flag)
{
if(flag==FTW_F)
{
num_temp();
memset(&info,0,sizeof(struct stat));
stat(file, &info);
printf("\n%s",ctime(&info.st_mtime));
/* if(info.st_mtime==info1.st_mtime)
{
printf("ok\n");
}
else
{
printf("error\n");
}*/
printf("%s\n",file); //打印文件名
}
return 0;
}
int main(int argc,char **argv)
{
if (argc<3)
{
printf("./ftw /etc \n");
exit(1);
}
else
{
printf("\n主目录为/web\n\n");
ftw(argv[1],myftw_temp,MAXFILENUM);
printf("\n主目录文件总数为:%d\n\n\n\n\n",count);
printf("\n备份目录为/webtemp\n\n");
ftw(argv[2],myftw_temp1,MAXFILENUM);
printf("\n备份目录文件总数为:%d\n\n\n\n",count_temp);
}
if(count_temp!=count)
{
printf("你的网站存在可疑文件,请检查!\n");
if(info.st_mtime==info1.st_mtime)
{
printf("文件时间没有问题\n");
}
else
{
printf("文件时间有问题\n");
}
}
else
{
printf("未发现可疑文件。\n");
}
}
|
程序运行结果
主目录为/web
Wed Aug 5 07:39:18 2009
/web/1.php
Wed Aug 5 07:40:10 2009
/web/file3/3.php
Wed Aug 5 07:39:25 2009
/web/2.php
Wed Aug 5 07:39:43 2009
/web/4.php
Wed Aug 5 07:39:33 2009
/web/3.php
Wed Aug 5 07:40:05 2009
/web/file2/2.php
Wed Aug 5 07:49:46 2009
/web/1111.txt
Wed Aug 5 07:39:57 2009
/web/file1/1.php
主目录文件总数为:8
备份目录为/webtemp
Wed Aug 5 07:42:24 2009
/webtemp/1.php
Wed Aug 5 07:42:24 2009
/webtemp/file3/3.php
Wed Aug 5 07:42:24 2009
/webtemp/2.php
Wed Aug 5 07:42:24 2009
/webtemp/4.php
Wed Aug 5 07:42:24 2009
/webtemp/3.php
Wed Aug 5 07:42:24 2009
/webtemp/file2/2.php
Wed Aug 5 07:42:24 2009
/webtemp/file1/1.php
备份目录文件总数为:7
你的网站存在可疑文件,请检查!
文件时间有问题
这里主要就是想把1111.txt文件名给提出来。到底要怎么做呢? |
|