#include<unistd.h>
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<time.h>
FILE *fp,*fbmp,*flog;
struct dirent *entry;
struct stat statbuf;
unsigned short BMP=0x4D42,
JPG=0xD8FF,
PNG[]={0x5089,0x474E,0x0A0D,0x0A1A},
GIF[]={0x4947,0x3846,0x6139};
char path1[],path2[],watchdog[],name[];
struct tm *tm_ptr;
time_t the_time;
//判断图片头文件的结构体
typedef struct tagRGBQUAD
{
unsigned char Blue;// À¶É«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
unsigned char Green; // ÂÌÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
unsigned char Red; // ºìÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
unsigned char Reserved;// ±£Áô£¬±ØÐëΪ0
} RGN;
void init()/*用于初始化程序*/
{
printf("file opened now\n");
fp=fopen("wrg.text","w+");
flog=fopen("wrlog.text","w+");
fbmp=fopen("bmpname.text","w+");
}
void Exit()
{
printf("everything is gone\n");
fclose(fp);
fclose(fbmp);
fclose(flog);
}
int istupian(char entry[])
/*返加值为
1 bmp;
2 jpg;
3 png;
4 gif;
*/
{
FILE *fis;
short int i=;
unsigned short pis[];
int flag=;//每次调用都进行初始化
if((fis=fopen(entry,"r"))==NULL)
printf("can not read %s\n",entry);
fread(pis,,,fis);
if(pis[]==BMP)
{
flag=;
printf("it is a bmp\n");
}
else if(pis[]==JPG)
{
flag=;
printf("it is a jpg\n");
}
else if(PNG[]==pis[]&&PNG[]==pis[]&&PNG[]==pis[]&&PNG[]==pis[])
{
flag=;
printf("it is a png\n");
}
else if(GIF[]==pis[]&&GIF[]==pis[]&&GIF[]==pis[])
{
flag=;
printf("it is a gif\n");
}
return flag;
}
void wrtlog()/*将文件的路径写入路径中*/
{
printf("write to wrtlg\n");
fprintf(fp,"%s/%s\n",getcwd(NULL,),entry->d_name);
fprintf(fbmp,"%s\n",entry->d_name);
}
void wrlog(char name[])// 将日记写入文件中
{
static int i=;
(void)time(&the_time);
//tm_ptr=gmtime(&the_time);
if(i==) fprintf(flog,"文件名 /状态 /时间 /\n");
++i;
fprintf(flog,"%s\t\t删除\t\t ",name);
fprintf(flog,"%s\n",ctime(&the_time));
//fprintf(flog,"%02d:%02d:%02d:%02d:%02d:%02d\n",tm_ptr->tm_year,tm_ptr->tm_mon+1,tm_ptr->tm_mday,tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec);
}
void printdir(char dir[])/*打印目录*/
{
DIR *dp;
printf("printdir ******doing now\n");
if((dp=opendir(dir))==NULL)
{
fprintf(stderr,"cannot open directory:%s\n",dir);
return;
}
chdir(dir);
while((entry=readdir(dp))!=NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(".",entry->d_name)==||strcmp("..",entry->d_name)==)
continue;
printdir(entry->d_name);
}
else
wrtlog();
}
chdir("..");
closedir(dp);
}
int bmp(char filename[])
{
FILE *fo,*fin;
RGN rgb,black={,,,},white={,,,};
char name[]="\0";
unsigned char temp[]="\0";
unsigned int offset=;
//在文件名中添加copy_来区别原文件
int len=strlen(filename);
strncpy(name,filename,len-);
strcat(name,"_copy.bmp");
if((fo=fopen(filename,"rb"))==NULL)
printf("can not open %s",filename);
if((fin=fopen(name,"wb"))==NULL)
printf("can not open %s",name);
printf("œ«ÖžÕëÒƶ¯µœÎ»ÍŒÊýŸÝµÄÆðʌλÖÃ\n");
fseek(fo,,);
printf("¿ªÊŒ¶ÁÈ¡Æ«ÒÆÁ¿\n");
fread(&offset,,,fo);
printf("ÏÔÊŸÆ«ÒÆλÖÃŽóС:");
printf("%X\n",offset);
printf("œ«ÖžÕëÒƶ¯µœÍŒÆ¬µÄÆðʌλÖÃ\n");
fseek(fo,,);
printf("¿ªÊŒÐŽÈëbmpÎÄŒþ\n");
unsigned int i=;
printf("Í·ÎÄŒþÐŽÈëÖÐ\n");
while(i<=offset)
{
fread(temp,,,fo);
fwrite(temp,,,fin);
i+=;
}
printf("ÑÕɫת»»ÖÐ\n");
while(!feof(fo))
{
fread(&rgb,,,fo);
if((rgb.Blue+rgb.Green+rgb.Red)/<)
fwrite(&black,,,fin);
else
fwrite(&white,,,fin);
}
fclose(fo);
fclose(fin);
printf("͌Ƭת»»³ÉºÚ°×¶þֵ͌œáÊø");
return ;
}
//硬连接
/*返加值为
1 bmp;
2 jpg;
3 png;
4 gif;
*/
void lkbmp()
{
fseek(fp,,);
fseek(fbmp,,);
while(fscanf(fp,"%s",path1)!=EOF&&fscanf(fbmp,"%s",name)!=EOF)
{
//path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中
strcat(path2,name);
printf("%s",path2);
if(link(path1,path2)==)
{
printf(" link is successful\n");
}
else
printf(" link is error\n");
//恢复母路径
if(istupian(path2)==)
bmp(path2);
strcpy(path2,watchdog);
}
}
//删除非图片文件
void del_dir()
{
int picture=;
fseek(fbmp,,);
fseek(fp,,);
if(fscanf(fbmp,"%s",name)==EOF)
printf("del_dir is error");
else while(fscanf(fp,"%s",path1)!=EOF&&fscanf(fbmp,"%s",name)!=EOF)
{
//path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中
strcat(path2,name);
if(!(picture=istupian(path2))&&unlink(path2)==)
{
printf("unlink %s is successful\n",path2);
wrlog(path2);
}
else
printf("unlink %s is error\n",path2);
//恢复母路径
strcpy(path2,watchdog);
}
}