Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <string.h>
- #include <unistd.h>
- #include <wait.h>
- int *i_log, n,k=0;
- FILE *f_log;
- DIR *dir_log;
- void backuper(char *s1,char *s2)
- {
- DIR *d1,*d2,*dir_cp;
- struct dirent *dir;
- int i,bull,g=0;
- char *sum,*sum_cp;
- pid_t pid;
- d1=opendir(s1);
- if(d1==NULL)
- {
- printf("%s - unable to read directory\n",s1);
- }
- else
- {
- mkdir(s2,0777);
- d2=opendir(s2);
- sum=malloc(strlen(s1)+256*sizeof(char));
- sum_cp=malloc(strlen(s2)+256*sizeof(char));
- while((dir=readdir(d1))!=NULL)
- {
- strcpy(sum,s1);
- strcpy(sum_cp,s2);
- bull=1;
- if(dir->d_type==DT_DIR)
- {
- if((strcmp(dir->d_name,"..")!=0)&&(strcmp(dir->d_name,"."))!=0)
- {
- strcat(sum,dir->d_name);
- strcat(sum,"/");
- strcat(sum_cp,dir->d_name);
- strcat(sum_cp,"/");
- if((dir_cp=opendir(sum_cp))==NULL)
- {
- mkdir(sum_cp,0777);
- }
- else
- {
- closedir(dir_cp);
- }
- backuper(sum,sum_cp);
- }
- }
- else
- {
- for(i=0;i<n;i++)
- {
- if((dir->d_ino)==i_log[i])
- {
- bull=0;
- }
- }
- if(bull)
- {
- pid=fork();
- if(pid==0)
- {
- strcat(sum,dir->d_name);
- g=execl("/bin/cp","cp",sum,s2,NULL);
- exit(0);
- }
- else
- {
- waitpid(pid,NULL,0);
- }
- fprintf(f_log,"%d\n",(int)dir->d_ino);
- pid=fork();
- if(pid==0)
- {
- strcat(sum_cp,dir->d_name);
- execl("/bin/gzip","gzip","-f",sum_cp,NULL);
- exit(0);
- }
- else
- {
- waitpid(pid,NULL,0);
- }
- k++;
- }
- }
- }
- closedir(d1);
- free(sum);
- free(sum_cp);
- }
- }
- int main(int argc, char *argv[])
- {
- int R;
- char *c_log,*s1;
- DIR *d2;
- char *s2;
- d2=opendir(argv[2]);
- if(d2==NULL)
- {
- if(mkdir(argv[2],0777)!=0)
- {
- printf("Unable to write there!\n");
- }
- }
- R=strlen(argv[1]);
- if(argv[1][R]!='/')
- {
- s1=malloc(R*sizeof(char)+2);
- strcpy(s1,argv[1]);
- s1[R]='/';
- s1[R+1]=0;
- }
- else
- {
- s1=malloc(R*sizeof(char));
- strcpy(s1,argv[1]);
- }
- R=strlen(argv[2]);
- if(argv[2][R]!='/')
- {
- s2=malloc(R*sizeof(char)+6);
- strcpy(s2,argv[2]);
- s2[R]='/';
- s2[R+1]=0;
- }
- else
- {
- s2=malloc(R*sizeof(char)+4);
- strcpy(s2,argv[2]);
- }
- n=strlen(s2);
- c_log=malloc(sizeof(char)*(n+8));
- strcpy(c_log,s2);
- c_log[n]='l';
- c_log[n+1]='o';
- c_log[n+2]='g';
- c_log[n+3]='/';
- c_log[n+4]=0;
- if((dir_log=opendir(c_log))==NULL)
- {
- mkdir(c_log,0777);
- }
- else
- {
- closedir(dir_log);
- }
- c_log[n+4]='l';
- c_log[n+5]='o';
- c_log[n+6]='g';
- c_log[n+7]=0;
- n=0;
- if((f_log=fopen(c_log,"r"))!=NULL)
- {
- printf("found old log\n");
- while((fscanf(f_log,"%d",&R)!=EOF))
- {
- n++;
- }
- fclose(f_log);
- i_log=malloc(n*sizeof(int));
- n=0;
- f_log=fopen(c_log,"r");
- while((fscanf(f_log,"%d",&i_log[n]))!=EOF)
- {
- n++;
- }
- fclose(f_log);
- }
- f_log=fopen(c_log,"a");
- if(f_log==NULL)
- {
- printf("Unable to write in destination directory\n");
- exit(1);
- }
- R=strlen(s2);
- s2[R]='z';
- s2[R+1]='i';
- s2[R+2]='p';
- s2[R+3]='/';
- s2[R+4]=0;
- backuper(s1,s2);
- fclose(f_log);
- closedir(d2);
- printf("Copied and zipped elements - %d\n",k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement