ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 文件备份c程序(转)

文件备份c程序(转)

原创 Linux操作系统 作者:WebSnap 时间:2019-03-11 12:15:06 0 删除 编辑
文件备份c程序(转)

//前两天购买刻录机供备份之用,于是编了一个程序以找出某一日

//期之后的文件,并复制到指定位置及建立相应的目录结构。

//This programme is writed by Vesuvius.
//It can be copy and motify under GNU General Public license.
#include
#include
#include
#include
#include
//The result file for test output.
FILE *out;
typedef struct dirent Di;
//The time which to be compared.
time_t com;
//The tmp root dirent for each find function.
char *rot;
extern char *mkstring(char *a,char*b,char *c);
//We need function which can create dir and cp file properly.
void makedir(char *a)
{
        int n,j;
        char *b;
        b=(char *)malloc(sizeof(char)*strlen(a));
        for(n=0;n<=strlen(a);n++)
          if(a[n]=='/'){
                strncpy(b,a,n);b[n]='';mkdir(b,666);
  //printf("The dir to be created is %s. ",b);
  }
        mkdir(a,666);
 //printf("The dir to be created is %s. ",a);
 free(b);
}
void copy(char *from,char *to)
{
        char buffer[4096];
        int in,out,n,m;
        in=open(from,O_RDONLY,666);
        if(in==-1) perror("open");
        out=open(to,O_WRONLY|O_CREAT,666);
        if(out==-1) perror("open");
        while(1){
         n=read(in,buffer,2048);
         if(n==-1) {perror("read");break;}
         if(n==0) break;
         m=write(out,buffer,n);
         if(m==-1) {perror("write");break;}
         if(m        }
        n=close(in);m=close(out);
        if(n==-1 || m==-1) printf("end ");
 printf("The file to be copyed is %s and %s . ",from,to);
}
//The two functions below is for scandir()
//scandir() find dir and file which after com.
//then qsort them with dir first file below.
int select1(const Di *a)
{
 struct stat buf;
 char *fname;
 
 if(strcmp(a->d_name,"..")!=0 && strcmp(a->d_name,".")!=0
  && strcmp(a->d_name,"System Volume Information")!=0) {
  if (a->d_type==DT_DIR) return 1;
  else{
   fname=mkstring(rot,"/",a->d_name);
   if(stat(fname,&buf)!=0) perror("stat");
   free(fname);
   if(buf.st_ctime>com) return 1;else return 0;  
  }
 }
 else return 0;
}
int compare(const Di **a,const Di **b)
{
 if((*a)->d_type!=DT_DIR) return 1;
 else return 0;
}
//This function link strings.
char *mkstring(char *a,char*b,char *c)
{
 char *r;
 r=(char *)malloc(sizeof(char)*(strlen(a)+strlen(b)+strlen(c)+1));
 sprintf(r,"%s%s%s",a,b,c);
 return r;
}
//The kernel function.
//find the file needed and write in result(like ls -R) or copy them.
void find(char *root,char *des)
{
 Di **list;
 int n;
 char *name,*name2,*name3;
 struct stat buf;

 rot=root;
 n=scandir(root,&list,select1,0);
 if(n>0 && list[n-1]->d_type!=DT_DIR) {
  fprintf(out,"%s ",root);
  name2=mkstring(des,"/",root+1);
  makedir(name2);
  free(name2);
 }
 if(n<=0) {printf("%s is %d. ",root,n);perror("scandir");}
 n--;
 while(n>=0){
    name=mkstring(root,"/",list[n]->d_name);
    stat(name,&buf);
    if(list[n]->d_type==DT_DIR) {
  find(name,des);
  free(name);
    }else{
  fprintf(out," %s ",name);
  name3=mkstring(des,"/",name+1);
  copy(name,name3);
  free(name);
  free(name3);
    }
    free(list[n]);
    n--;
 }
}
//change the string refer to time.
//xxxxxxxx is 20040102
//to time_t
time_t gettime(char *a)
{
 struct tm t;
 int i;
 for(i=0;i<=7;i++) a[i]-=48;
 t.tm_year=a[0]*1000+a[1]*100+a[2]*10+a[3]-1900;
 t.tm_mon=a[4]*10+a[5]-1;
 t.tm_mday=a[6]*10+a[7];
 t.tm_sec=t.tm_hour=t.tm_min=t.tm_isdst=0;
 return mktime(&t);
}
int main(int a,char **li)
{
 out=fopen("result","w");
 com=gettime(li[2]);
 find(li[1],li[3]);
 fclose(out);
 return 0;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10797429/viewspace-101674/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    163
  • 访问量
    121781