Linux项目一

Linux项目一

引言:

  这是我去年做的东西,一直没有时间整理,今天又要做一个基于这个项目的客户端与服务器版本。

  以前我写的库文件中的函数耦合度很大,在一个函数中调用另一个函数,导致一无法拆开使用!

  因此,我对以前写的库进行接口的更改,以更好的适应更多的项目。

  当然我也把以前写的小程序贴出来,以供大家参考!

需求:

  1.搜索bmp图片转换成黑白图片复制到另一个文件中

  2.系统为Linux

自定义功能函数:

  1.打印目录:

 1 #include<unistd.h>
 2 #include<stdio.h>
 3 #include<dirent.h>
 4 #include<string.h>
 5 #include<sys/stat.h>
 6 #include<stdlib.h>
 7 void printdir(char *dir,int depth)/*打印目录*/
 8 {
 9     DIR *dp;
10     struct dirent *entry;
11     struct stat statbuf;
12 
13     if((dp=opendir(dir))==NULL)
14     {
15         fprintf(stderr,"cannot open directory:%s\n",dir);
16         return;
17     }
18     chdir(dir);
19     while((entry=readdir(dp))!=NULL)
20     {
21         lstat(entry->d_name,&statbuf);/**获取文件信息,保存在statbuf**/
22         if(S_ISDIR(statbuf.st_mode))/***判断是否为目录***/
23         {
24             if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
25                 continue;
26             printf("%*s%s/\n",depth," ",entry->d_name);
27             printdir(entry->d_name,depth+4);
28         }
29         else
30              printf("%*s%s/\n",depth," ",entry->d_name);/***为文件***/
31     }
32     chdir("..");
33     closedir(dp);
34 }

  2.删除文件:

1 #include<unistd.h>
2 void del_file(const char *filename)
3 {
4     unlink(filename);
5     return 1;
6 }

  3.复制文件:

#include<unistd.h>
void lkbmp(const char *source,const char *target)
{
    if(link(source,target)==0)
      printf(" link is successful\n");
    else
    printf(" link is error\n");
}

  4.对彩色图片转换成黑白图片

#include<stdio.h>
#include<string.h>
int bmp(char filename[])
{
    FILE *fo,*fin;
    RGN rgb,black={0,0,0,0},white={255,255,255,0};
    char name[256]="\0";
    unsigned char temp[14]="\0";
    unsigned int offset=0;
    /***在文件名中添加copy_来区别原文件***/
    int len=strlen(filename);
    strncpy(name,filename,len-4);
    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);
    fseek(fo,10,0);
    fread(&offset,4,1,fo);
    printf("%X\n",offset);
    fseek(fo,0,0);
    unsigned int i=1;
    while(i<=offset)
    {
        fread(temp,1,1,fo);
        fwrite(temp,1,1,fin);
        i+=1;
    }
    while(!feof(fo))
    {
        fread(&rgb,4,1,fo);
        if((rgb.Blue+rgb.Green+rgb.Red)/3<125)
            fwrite(&black,4,1,fin);
        else
            fwrite(&white,4,1,fin);
    }

    fclose(fo);
    fclose(fin);
    return 0;
}

完整自定义库文件:

  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<stdlib.h>
 10 #include<time.h>
 11 #include <sys/msg.h>
 12 FILE *fp,*fbmp,*flog;
 13 struct dirent *entry;
 14 struct stat statbuf;
 15 unsigned short  BMP=0x4D42,
 16             JPG=0xD8FF,
 17         PNG[4]={0x5089,0x474E,0x0A0D,0x0A1A},
 18         GIF[3]={0x4947,0x3846,0x6139};
 19 static char path1[256],path2[256],watchdog[256],name[256];
 20 struct tm *tm_ptr;
 21 time_t the_time;
 22 
 23 //判断图片头文件的结构体
 24 typedef struct tagRGBQUAD
 25 {
 26 unsigned char Blue;// À¶É«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
 27 unsigned char Green; // ÂÌÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
 28 unsigned char Red; // ºìÉ«µÄÁÁ¶È£šÖµ·¶Î§Îª0-255)
 29 unsigned char Reserved;// ±£Áô£¬±ØÐëΪ0
 30 } RGN;
 31 
 32 void init()/*用于初始化程序*/
 33 {
 34     printf("file opened now\n");
 35     fp=fopen("wrg.text","w+");
 36     flog=fopen("wrlog.text","w+");
 37     fbmp=fopen("bmpname.text","w+");
 38 }
 39 void Exit()
 40 {
 41     printf("everything is gone\n");
 42     fclose(fp);
 43     fclose(fbmp);
 44     fclose(flog);
 45 }
 46 int istupian(char entry[])
 47 /*返加值为
 48 1 bmp;
 49 2 jpg;
 50 3 png;
 51 4 gif;
 52 */
 53 {
 54     FILE *fis;
 55     short int i=0;
 56     unsigned short pis[5];
 57     int flag=0;//每次调用都进行初始化
 58     if((fis=fopen(entry,"r"))==NULL)
 59         printf("can not read %s\n",entry);
 60     fread(pis,8,1,fis);
 61 
 62     if(pis[0]==BMP)
 63     {
 64         flag=1;
 65         printf("it is a bmp\n");
 66     }
 67     else if(pis[0]==JPG)
 68     {
 69         flag=2;
 70         printf("it is a jpg\n");
 71     }
 72     else if(PNG[0]==pis[0]&&PNG[1]==pis[1]&&PNG[2]==pis[2]&&PNG[3]==pis[3])
 73     {
 74         flag=3;
 75         printf("it is a png\n");
 76     }
 77     else if(GIF[0]==pis[0]&&GIF[1]==pis[1]&&GIF[2]==pis[2])
 78     {
 79         flag=4;
 80         printf("it is a gif\n");
 81     }
 82     return flag;
 83 
 84 
 85 }
 86 void wrtlog()/*将文件的路径写入路径中*/
 87 {
 88     printf("write to wrtlg\n");
 89     fprintf(fp,"%s/%s\n",getcwd(NULL,1024),entry->d_name);
 90     fprintf(fbmp,"%s\n",entry->d_name);
 91 }
 92 void wrlog(char name[])//  将日记写入文件中
 93 {
 94   static int i=0;
 95   (void)time(&the_time);
 96   //tm_ptr=gmtime(&the_time);
 97   if(i==0) fprintf(flog,"文件名                    /状态            /时间            /\n");
 98   ++i;
 99   fprintf(flog,"%s\t\t删除\t\t ",name);
100 
101   fprintf(flog,"%s\n",ctime(&the_time));
102   //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);
103 }
104 void printdir(char dir[])/*打印目录*/
105 {
106     DIR *dp;
107         printf("printdir ******doing now\n");
108     if((dp=opendir(dir))==NULL)
109     {
110         fprintf(stderr,"cannot open directory:%s\n",dir);
111         return;
112     }
113     chdir(dir);
114     while((entry=readdir(dp))!=NULL)
115     {
116         lstat(entry->d_name,&statbuf);
117         if(S_ISDIR(statbuf.st_mode))
118         {
119             if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
120                 continue;
121             printdir(entry->d_name);
122         }
123         else
124             wrtlog();
125     }
126     chdir("..");
127     closedir(dp);
128 }
129 int bmp(char filename[])
130 {
131     FILE *fo,*fin;
132     RGN rgb,black={0,0,0,0},white={255,255,255,0};
133     char name[256]="\0";
134     unsigned char temp[14]="\0";
135     unsigned int offset=0;
136     /***在文件名中添加copy_来区别原文件***/
137     int len=strlen(filename);
138     strncpy(name,filename,len-4);
139     strcat(name,"_copy.bmp");
140 
141     if((fo=fopen(filename,"rb+"))==NULL)
142         printf("can not open %s",filename);
143     if((fin=fopen(name,"wb+"))==NULL)
144         printf("can not open %s",name);
145     fseek(fo,10,0);
146     fread(&offset,4,1,fo);
147     printf("%X\n",offset);
148     fseek(fo,0,0);
149     unsigned int i=1;
150     while(i<=offset)
151     {
152         fread(temp,1,1,fo);
153         fwrite(temp,1,1,fin);
154         i+=1;
155     }
156     while(!feof(fo))
157     {
158         fread(&rgb,4,1,fo);
159         if((rgb.Blue+rgb.Green+rgb.Red)/3<125)
160             fwrite(&black,4,1,fin);
161         else
162             fwrite(&white,4,1,fin);
163     }
164 
165     fclose(fo);
166     fclose(fin);
167     return 0;
168 }
169 //硬连接
170 /*返加值为
171 1 bmp;
172 2 jpg;
173 3 png;
174 4 gif;
175 */
176 void lkbmp()
177 {
178   fseek(fp,0,0);
179   fseek(fbmp,0,0);
180   while(fscanf(fp,"%s",path1)!=EOF&&fscanf(fbmp,"%s",name)!=EOF)
181   {
182     /***path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中***/
183     strcat(path2,name);
184     printf("%s",path2);
185     if(link(path1,path2)==0)
186     {
187       printf(" link is successful\n");
188     }
189       else
190     printf(" link is error\n");
191       //恢复母路径
192       if(istupian(path2)==1)
193     bmp(path2);
194       strcpy(path2,watchdog);
195   }
196 
197 }
198 //删除非图片文件
199 int del_dir()
200 {
201   fseek(fp,0,0);
202   fseek(fbmp,0,0);
203   while(fscanf(fbmp,"%s",name)!=EOF)
204   {
205     //path2 的路径已经初始化,将文件名接到path2中,链接好后,将watchsog重新载入watchdog中
206     strcat(path2,name);
207     if(!istupian(path2)&&unlink(path2)==0)
208       {unlink(path2);
209         printf("unlink %s is successful\n",path2);
210         wrlog(path2);
211       }
212     else
213       printf("unlink %s is error\n",path2);
214           //恢复母路径
215     strcpy(path2,watchdog);
216   }
217   return 1;
218 
219 }

对应的执行文件:

 1 #include"myhead.h"
 2 #pragma pack(2)
 3 int main(int argc,char *argv[])
 4 {
 5     char topdir[128] = ".";
 6         if(argc>=2)
 7         strcpy(topdir,argv[1]);
 8         printf("已经得到目录:\"%s\"\n",topdir);
 9         printf("初始化中");
10         init();
11         printf("初始化完毕\n");
12         printf("start printdir\n");
13         printf("进入原始路径,得到母路径。");
14         chdir(topdir);
15         chdir("..");
16         strcpy(path2,getcwd(NULL,1024));
17             //创建D目录,可以以作为子进程
18           strcat(path2,"/D/");
19           mkdir(path2,0770);
20           printf("母路径:%s\n",path2);
21           strcpy(watchdog,path2);
22     pid_t fork_res;
23     fork_res=fork();
24       if(fork_res==0)
25       {
26           printf("Child now\n");
27         while(del_dir()!=1);
28         printf("del_dir done.\n");
29           return 1;
30       }
31       else if(fork_res>0)
32       { 
33           printdir(topdir);
34           printf("printdir done.\n");
35           lkbmp();
36           printf("lkbmp done\n");
37           del_dir();
38       return 0 ;
39       }
40       else  
41       { perror("fork failed");
42           exit(1);
43       }
44      
45     
46     
47     printf("程序运行结束");  
48     exit(0);
49 }

 

 

  

 

 

你可能感兴趣的:(linux)