Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. Dodane przez: ~Anonim (2019-04-22 18:53) -> text
  2. 1.
  3. 2.
  4. 3.
  5. 4.
  6. 5.
  7. 6.
  8. 7.
  9. 8.
  10. 9.
  11. 10.
  12. 11.
  13. 12.
  14. 13.
  15. 14.
  16. 15.
  17. 16.
  18. 17.
  19. 18.
  20. 19.
  21. 20.
  22. 21.
  23. 22.
  24. 23.
  25. 24.
  26. 25.
  27. 26.
  28. 27.
  29. 28.
  30. 29.
  31. 30.
  32. 31.
  33. 32.
  34. 33.
  35. 34.
  36. 35.
  37. 36.
  38. 37.
  39. 38.
  40. 39.
  41. 40.
  42. 41.
  43. 42.
  44. 43.
  45. 44.
  46. 45.
  47. 46.
  48. 47.
  49. 48.
  50. 49.
  51. 50.
  52. 51.
  53. 52.
  54. 53.
  55. 54.
  56. 55.
  57. 56.
  58. 57.
  59. 58.
  60. 59.
  61. 60.
  62. 61.
  63. 62.
  64. 63.
  65. 64.
  66. 65.
  67. 66.
  68. 67.
  69. 68.
  70. 69.
  71. 70.
  72. 71.
  73. 72.
  74. 73.
  75. 74.
  76. 75.
  77. 76.
  78. 77.
  79. 78.
  80. 79.
  81. 80.
  82. 81.
  83. 82.
  84. 83.
  85. 84.
  86. 85.
  87. 86.
  88. 87.
  89. 88.
  90. 89.
  91. 90.
  92. #include <stdio.h>
  93. #include <unistd.h>
  94. #include <sys/stat.h>
  95. #include <string.h>
  96. #include <sys/types.h>
  97. #include <dirent.h>
  98. #include <stdlib.h>
  99. #include <fcntl.h>
  100. int checkPath(char* source, char* goal){
  101. struct stat source_st,goal_st;
  102. if(stat(source,&source_st) == -1 || stat(goal,&goal_st) == -1){
  103. return 1; //Path doesn't exist
  104. }
  105. if(S_ISDIR(source_st.st_mode) & S_ISDIR(goal_st.st_mode)){
  106. return 0; //It's directory
  107. }
  108. if(S_ISREG(source_st.st_mode) & S_ISREG(goal_st.st_mode)){
  109. return 2; //It's regular file
  110. }
  111. else return -1; //It's something else
  112. }
  113. char* concat(const char *s1, const char *s2)
  114. {
  115. char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
  116. strcpy(result, s1);
  117. strcat(result, s2);
  118. return result;
  119. }
  120.  
  121. int main(int argc, char* argv[]){
  122. struct dirent *src;
  123. struct dirent *goal;
  124. struct dirent *temp;
  125. int flag = 0,sourcehandle,destinationhandle,bytes;
  126. if(argc <= 2){
  127. printf("Invalid number of arguments. It must be at least 2.\n");
  128. }
  129. else{
  130. if(checkPath(argv[1],argv[2]) == 1) printf("Path doesn't exist\n");
  131. if(checkPath(argv[1],argv[2]) == -1 || checkPath(argv[1],argv[2]) == 2) printf("Path it's not directory\n");
  132. if(checkPath(argv[1],argv[2]) == 0){
  133. DIR *src_dir = opendir(argv[1]);
  134. if(src_dir == NULL){
  135. printf("Could not open current directory");
  136. }
  137. while ((src = readdir(src_dir)) != NULL) {
  138. DIR *goal_dir = opendir(argv[2]);
  139. if(src->d_type == 8) //Only files
  140. if ( !strcmp(src->d_name, ".") || !strcmp(src->d_name, "..") ){}
  141. else {
  142. while((goal = readdir(goal_dir)) != NULL){
  143. if(goal->d_type == 8) //Only files
  144. if(strcmp(src->d_name,goal->d_name) == 0){
  145. flag = 1;
  146. }
  147.  
  148. }
  149. if(flag == 0){
  150. printf("File %s need to be synchronized\n",src->d_name);
  151. char* src_path = concat(argv[1],src->d_name);
  152. char* goal_path = concat(argv[2],src->d_name);
  153. FILE* pnReadFile = fopen(src_path,"r");
  154. printf("\n dest file name = %s\n", goal_path);
  155. FILE* pnWriteFile = fopen(goal_path, "w"); /*File Pointer to write in file*/
  156. free(goal_path);
  157. free(src_path);
  158. char buffer[1024] = {0}; /*Buffer to store files content*/
  159. while(fgets(buffer, 1024, pnReadFile))
  160.  
  161. {
  162. fputs(buffer, pnWriteFile);
  163. }
  164. fclose(pnWriteFile);
  165.  
  166. fclose(pnReadFile);
  167. }
  168.  
  169. flag = 0;
  170. closedir(goal_dir);
  171. }
  172. }
  173.  
  174. closedir(src_dir);
  175. return 0;
  176. }
  177. }
  178.  
  179.  
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement