printdir-deldir-bmp

  1 #include<unistd.h>

  2 #include<stdio.h>

  3 #include<dirent.h>

  4 #include<string.h>

  5 #include<stdlib.h>

  6 #include<sys/stat.h>

  7 #include<sys/types.h>

  8 #include<sys/wait.h>

  9 #include<time.h>

 10 FILE *fp,*fbmp,*flog;

 11 struct dirent *entry;

 12 struct stat statbuf;

 13 unsigned short  BMP=0x4D42,

 14             JPG=0xD8FF,

 15         PNG[4]={0x5089,0x474E,0x0A0D,0x0A1A},

 16         GIF[3]={0x4947,0x3846,0x6139};

 17 char path1[256],path2[256],watchdog[256],name[256];

 18 struct tm *tm_ptr;

 19 time_t the_time;

 20 

 21 //判断图片头文件的结构体

 22 typedef struct tagRGBQUAD 

 23 {

 24 unsigned char Blue;// À¶É«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)

 25 unsigned char Green; // ÂÌÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)

 26 unsigned char Red; // ºìÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)

 27 unsigned char Reserved;// ±£Áô£¬±ØÐëΪ0

 28 } RGN;

 29 

 30 void init()/*用于初始化程序*/

 31 {

 32     printf("file opened now\n");

 33     fp=fopen("wrg.text","w+");

 34     flog=fopen("wrlog.text","w+");

 35     fbmp=fopen("bmpname.text","w+");

 36 }

 37 void Exit()

 38 {

 39     printf("everything is gone\n");

 40     fclose(fp);

 41     fclose(fbmp);

 42     fclose(flog);

 43 }

 44 int istupian(char entry[])

 45 /*返加值为

 46 1 bmp;

 47 2 jpg;

 48 3 png;

 49 4 gif;

 50 */

 51 {

 52     FILE *fis;

 53     short int i=0;

 54     unsigned short pis[5];

 55     int flag=0;//每次调用都进行初始化

 56     if((fis=fopen(entry,"r"))==NULL) 

 57         printf("can not read %s\n",entry);

 58     fread(pis,8,1,fis);

 59     

 60     if(pis[0]==BMP)    

 61     {

 62         flag=1;        

 63         printf("it is a bmp\n");

 64     }

 65     else if(pis[0]==JPG)    

 66     {

 67         flag=2;

 68         printf("it is a jpg\n");

 69     }

 70     else if(PNG[0]==pis[0]&&PNG[1]==pis[1]&&PNG[2]==pis[2]&&PNG[3]==pis[3])    

 71     {

 72         flag=3;        

 73         printf("it is a png\n");

 74     }

 75     else if(GIF[0]==pis[0]&&GIF[1]==pis[1]&&GIF[2]==pis[2])    

 76     {

 77         flag=4;        

 78         printf("it is a gif\n");

 79     }

 80     return flag;

 81 

 82 

 83 }

 84 void wrtlog()/*将文件的路径写入路径中*/

 85 {    

 86     printf("write to wrtlg\n");

 87     fprintf(fp,"%s/%s\n",getcwd(NULL,1024),entry->d_name);

 88     fprintf(fbmp,"%s\n",entry->d_name);

 89 }

 90 void wrlog(char name[])//  将日记写入文件中

 91 {

 92   static int i=0;

 93   (void)time(&the_time);

 94   //tm_ptr=gmtime(&the_time);

 95   if(i==0) fprintf(flog,"文件名                    /状态            /时间            /\n");

 96   ++i;

 97   fprintf(flog,"%s\t\t删除\t\t ",name);

 98   

 99   fprintf(flog,"%s\n",ctime(&the_time));

100   //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);

101 }

102 void printdir(char dir[])/*打印目录*/

103 {

104     DIR *dp;

105         printf("printdir ******doing now\n");

106     if((dp=opendir(dir))==NULL)

107     {

108         fprintf(stderr,"cannot open directory:%s\n",dir);

109         return;

110     }

111     chdir(dir);

112     while((entry=readdir(dp))!=NULL)

113     {

114         lstat(entry->d_name,&statbuf);

115         if(S_ISDIR(statbuf.st_mode))

116         {

117             if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)

118                 continue;

119             printdir(entry->d_name);

120         }

121         else 

122             wrtlog();

123     }

124     chdir("..");

125     closedir(dp);

126 }

127 int bmp(char filename[])

128 {

129     FILE *fo,*fin;

130     RGN rgb,black={0,0,0,0},white={255,255,255,0};

131     char name[256]="\0";

132     unsigned char temp[14]="\0";

133     unsigned int offset=0;

134     //在文件名中添加copy_来区别原文件

135     int len=strlen(filename);

136     strncpy(name,filename,len-4);

137     strcat(name,"_copy.bmp");

138     

139     if((fo=fopen(filename,"rb"))==NULL)

140         printf("can not open %s",filename);

141     if((fin=fopen(name,"wb"))==NULL)

142         printf("can not open %s",name);

143                             printf("œ«ÖžÕëÒÆ¶¯µœÎ»ÍŒÊýŸÝµÄÆðʌλÖÃ\n");

144     fseek(fo,10,0);

145                             printf("¿ªÊŒ¶ÁÈ¡Æ«ÒÆÁ¿\n");

146     fread(&offset,4,1,fo);

147                             printf("ÏÔÊŸÆ«ÒÆÎ»ÖÃŽóС:");

148     printf("%X\n",offset);

149                             printf("œ«ÖžÕëÒÆ¶¯µœÍŒÆ¬µÄÆðʌλÖÃ\n");

150     fseek(fo,0,0); 

151                             

152     

153                         printf("¿ªÊŒÐŽÈëbmpÎÄŒþ\n");  

154     unsigned int i=1;

155                             printf("Í·ÎÄŒþÐŽÈëÖÐ\n");

156     while(i<=offset)

157     {

158         fread(temp,1,1,fo);

159         fwrite(temp,1,1,fin);

160         i+=1;

161     }

162                             printf("ÑÕɫת»»ÖÐ\n");

163     while(!feof(fo))

164     {

165         fread(&rgb,4,1,fo);

166         if((rgb.Blue+rgb.Green+rgb.Red)/3<125)

167             fwrite(&black,4,1,fin);

168         else

169             fwrite(&white,4,1,fin);

170     }

171         

172     fclose(fo);

173     fclose(fin);

174     printf("͌Ƭת»»³ÉºÚ°×¶þֵ͌œáÊø");

175     return 0;

176 }

177 //硬连接

178 /*返加值为

179 1 bmp;

180 2 jpg;

181 3 png;

182 4 gif;

183 */

184 void lkbmp()

185 {    

186   fseek(fp,0,0);

187   fseek(fbmp,0,0);

188   while(fscanf(fp,"%s",path1)!=EOF&&fscanf(fbmp,"%s",name)!=EOF)

189   {  

190     //path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中

191     strcat(path2,name);

192     printf("%s",path2);

193     if(link(path1,path2)==0)

194     {

195       printf(" link is successful\n");

196     }

197       else 

198     printf(" link is error\n");

199       //恢复母路径

200       if(istupian(path2)==1)

201     bmp(path2);

202       strcpy(path2,watchdog);

203   }

204   

205 }

206 //删除非图片文件

207 void del_dir()

208 {

209   int picture=0;

210   fseek(fbmp,0,0);

211   fseek(fp,0,0);

212   if(fscanf(fbmp,"%s",name)==EOF)

213     printf("del_dir is error");

214   else while(fscanf(fp,"%s",path1)!=EOF&&fscanf(fbmp,"%s",name)!=EOF)

215   {  

216     //path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中

217     strcat(path2,name);

218 

219     if(!(picture=istupian(path2))&&unlink(path2)==0)

220       {

221         printf("unlink %s is successful\n",path2);

222         wrlog(path2);

223       }

224     else 

225       printf("unlink %s is error\n",path2);

226           //恢复母路径

227     strcpy(path2,watchdog);

228   }

229   

230 }
View Code

 

 1 #include"myhead.h"

 2 #pragma pack(1)

 3 int main(int argc,char *argv[])

 4 {

 5     pid_t fork_res;

 6     char topdir[128] = ".";

 7     if(argc>=2)

 8     strcpy(topdir,argv[1]);

 9     

10     printf("已经得到目录:\"%s\"\n",topdir);

11     printf("初始化中");

12     init();

13     printf("初始化完毕\n");

14     printf("start printdir\n");

15     printf("进入原始路径,得到母路径。");

16     chdir(topdir);

17     chdir("..");

18     strcpy(path2,getcwd(NULL,1024));

19 

20       //创建D目录,可以以作为子进程

21       strcat(path2,"/D/");

22       mkdir(path2,0770);

23       printf("母路径:%s\n",path2);

24       strcpy(watchdog,path2);

25     

26       printdir(topdir);

27       //链接文件

28       lkbmp();

29       printf("done.\n");

30       //退出

31       //删除原文件

32      del_dir();

33      //进入D目录

34      Exit();

35     

36      printf("程序运行结束");

37      

38      

39       

40 }
View Code

 

 

你可能感兴趣的:(print)