Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h> //pentru diferite tipuri clock, time ,off_set
  5. #include <sys/stat.h> //pentru fisiere
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <dirent.h>
  9. #include <string.h>
  10.  
  11. char buffer[1024];
  12. char txt[3]="txt";
  13.  
  14. //functia creaza un fisier identic de la pathul aux in pathul text
  15. void copiere_txt(char* aux,char * temp){
  16. int file;
  17. int whcpy;
  18. if((file=open(aux,O_RDWR))<0){
  19. printf("Eroare la deschidere fisier");
  20. exit(3);
  21. }
  22.  
  23.  
  24. if((whcpy=open(temp,O_WRONLY | O_CREAT | O_TRUNC,S_IWUSR|S_IRUSR))<0){
  25. printf("Eroare la deschidere fisier");
  26. exit(4);
  27. }
  28.  
  29. int rez;
  30. char c;
  31. while((rez=read(file,&c,sizeof(char)))>0)
  32. if(write(whcpy,&c,rez)<0)
  33. printf("Eroare la copiere");
  34. close(file);
  35. close(whcpy);
  36.  
  37. }
  38.  
  39. void copiere_dir(char* temp){
  40.  
  41. mkdir(temp,S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH);
  42. }
  43.  
  44.  
  45. void func(int nr,char* args1,char* args2){
  46. char tab[20][20];
  47. int tab_counter=0;
  48. DIR* dir=NULL;
  49. struct dirent* box;
  50.  
  51. if(nr<3){
  52. printf("Nu s-au introdus suficienti paramatri");
  53. exit(1);
  54.  
  55. }
  56.  
  57.  
  58. if((dir=opendir(args1))==NULL){
  59. printf("Eroare la deschidere fisier");
  60. exit(2);
  61. }
  62.  
  63. box=readdir(dir);
  64. while(box!=NULL){
  65. printf("%s\n",box->d_name);
  66. strcpy(tab[tab_counter++],box->d_name);
  67. box=readdir(dir);
  68. }
  69.  
  70. int i;
  71. printf("\n\n");
  72. for(i=0;i<tab_counter;i++){
  73. if(tab[i][0]!='.'){
  74. char* ttxt=strstr(tab[i],txt);
  75. if(ttxt){
  76. //formam path-ul pentru fiecare fisier de tip text
  77. char* aux=(char*)malloc(sizeof(char)*100);
  78. char* temp=(char*)malloc(sizeof(char)*100);
  79.  
  80. strcpy(aux,args1);
  81. strcpy(temp,args2);
  82. int m=strlen(aux);
  83. int n=strlen(temp);
  84.  
  85. aux[m]='/';
  86. temp[n]='/';
  87. strcat(aux,tab[i]);
  88. strcat(temp,tab[i]);
  89. //printf("%s\n",temp); //afisare cale
  90.  
  91. //deschidem fisierele
  92. copiere_txt(aux,temp);
  93.  
  94. aux=NULL;
  95. temp=NULL;
  96.  
  97.  
  98. }
  99.  
  100. else{
  101. char aux[3][100];
  102. strcpy(aux[1],args1);
  103. strcpy(aux[2],args2);
  104. int m=strlen(aux[1]);
  105. int n=strlen(aux[2]);
  106.  
  107. aux[1][m]='/';
  108. aux[2][n]='/';
  109.  
  110. strcat(aux[1],tab[i]);
  111. strcat(aux[2],tab[i]);
  112. //merge recursiv in fiecare director
  113. func(3,aux[1],aux[2]);
  114. copiere_dir(aux[2]);
  115.  
  116.  
  117. }
  118. }
  119.  
  120. }
  121.  
  122. }
  123.  
  124.  
  125. int main(int args,char** argv){
  126. func(args,argv[1],argv[2]);
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement