Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <sys/stat.h>
  9. #include <malloc.h>
  10. #include <stdlib.h>
  11.  
  12. #define MOD_COPY "c"
  13. #define MOD_LINK "l"
  14. #define DIRECTOR_CURENT "."
  15. #define DIRECTOR_ANTERIOR ".."
  16. #define MAX 200
  17.  
  18. void creeaza_director_destinatie(char *dir)
  19. {
  20. DIR *s=opendir(dir);
  21. if (s==NULL) mkdir(dir, 0755);
  22. else closedir(s);
  23. }
  24.  
  25.  
  26. int copie(char *sursa, char *destinatie, char *mod)
  27. {
  28. DIR *s;
  29. struct dirent *dit;
  30. s=opendir(sursa);
  31. if (!strcmp(MOD_COPY, mod))
  32. {
  33. char *next_s;
  34. char *next_d;
  35. int l_next_s;
  36. int l_next_d;
  37. while ((dit = readdir(s)) != NULL)
  38. {
  39. if (dit->d_type == DT_DIR)
  40. {
  41. int curent = !strcmp(DIRECTOR_CURENT, dit->d_name);
  42. int precedent = !strcmp(DIRECTOR_ANTERIOR, dit->d_name);
  43. if (curent || precedent) continue;
  44. l_next_d = strlen(destinatie) + strlen(dit->d_name) + 1;
  45. next_d = (char *)malloc(l_next_d*sizeof(char));
  46. l_next_s = strlen(sursa) + strlen(dit->d_name) + 1;
  47. next_s = (char *)malloc(l_next_s*sizeof(char));
  48. sprintf(next_d, "%s/%s", destinatie, dit->d_name);
  49. sprintf(next_s, "%s/%s", sursa, dit->d_name);
  50. creeaza_director_destinatie(next_d);
  51. copie(sursa, destinatie, mod);
  52. free(next_d);
  53. free(next_s);
  54. }
  55. else
  56. {
  57. FILE * s;
  58. FILE * dest;
  59. char buffer[MAX];
  60. int readed;
  61. int writed;
  62. int dim_sursa = strlen(sursa) + strlen(dit->d_name) + 1;
  63. char *nume_fisier_sursa = (char *) malloc(dim_sursa*sizeof(char));
  64. sprintf(nume_fisier_sursa, "%s/%s", sursa, dit->d_name);
  65.  
  66. int dim_dest = strlen(destinatie) + strlen(dit->d_name) + 1;
  67. char *nume_dest = (char *) malloc(dim_dest*sizeof(char));
  68. sprintf(nume_dest, "%s/%s", destinatie, dit->d_name);
  69. s=fopen(nume_fisier_sursa, "rb");
  70. dest=fopen(nume_dest, "wb");
  71.  
  72. while (!feof(s))
  73. {
  74. readed = fread(buffer, 1, MAX, s);
  75. writed=fwrite(buffer, 1, readed, dest);
  76. }
  77. fclose(s);
  78. fclose(dest);
  79. free(nume_fisier_sursa);
  80. free(nume_dest);
  81. }
  82. }
  83. }
  84. else
  85. {
  86. char *link_sursa;
  87. char *link_dest;
  88. int dim_sursa, dim_dest;
  89. while ((dit = readdir(s)) != NULL)
  90. {
  91. int curent = !strcmp(DIRECTOR_CURENT, dit->d_name);
  92. int precedent = !strcmp(DIRECTOR_ANTERIOR, dit->d_name);
  93. if (curent || precedent) continue;
  94. dim_sursa = strlen(sursa) + strlen(dit->d_name) + 1;
  95. link_sursa = (char *) malloc(dim_sursa*sizeof(char));
  96. sprintf(link_sursa, "%s/%s", sursa, dit->d_name);
  97. dim_dest = strlen(destinatie) + strlen(dit->d_name) + 1;
  98. link_dest = (char *) malloc(dim_dest*sizeof(char));
  99. sprintf(link_dest, "%s/%s", destinatie, dit->d_name);
  100. symlink(link_sursa, link_dest);
  101. free(link_sursa);
  102. free(link_dest);
  103. }
  104. }
  105. closedir(s);
  106. return 1;
  107. }
  108.  
  109.  
  110. int main(int argc, char *argv[])
  111. {
  112. char *director_sursa;
  113. char *director_destinatie;
  114. char *mod;
  115. if (argc < 4)
  116. {
  117. printf("Usage: %s <Director sursa> <director destinatie> <mod>\n", argv[0]);
  118. printf("\t<mod> poate fi \"c\" sau \"l\"\n");
  119. return 0;
  120. }
  121. director_sursa = argv[1];
  122. director_destinatie = argv[2];
  123. mod = argv[3];
  124. if (strcmp(MOD_COPY, mod) && strcmp(MOD_LINK, mod))
  125. {
  126. printf("\t<mod> trebuie sa fie \"c\" or \"l\"\n");
  127. return 0;
  128. }
  129. creeaza_director_destinatie(director_destinatie);
  130. copie(director_sursa, director_destinatie, mod);
  131. return 1;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement