Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <dirent.h>
  8. #include <string.h>
  9.  
  10.  
  11. #define MAX_PATH 256
  12. #define COPIE 1
  13. #define LINK 2
  14.  
  15. void CopyDirectory( char *source, char *dest, mode_t mod)
  16. {
  17. struct stat stbuf;
  18. struct dirent *dirent;
  19. DIR *dp;
  20. char tmp_src[MAX_PATH];
  21. char tmp_dest[MAX_PATH];
  22. int in_handle;
  23. int out_handle;
  24. void *buffer;
  25. int i;
  26.  
  27. /* creaza directorul */
  28. mkdir(dest, mod);
  29.  
  30. dp = opendir(source);
  31. if (dp != NULL)
  32. {
  33. dirent = readdir(dp);
  34. while (dirent != NULL)
  35. {
  36. if ( strcmp(dirent->d_name , ".") &&
  37. strcmp(dirent->d_name , ".."))
  38. {
  39.  
  40. sprintf(tmp_src,"%s/%s",source,dirent->d_name);
  41. stat(tmp_src,&stbuf);
  42.  
  43. /* if this is a file */
  44. if(!S_ISDIR(stbuf.st_mode))
  45. {
  46. sprintf(tmp_dest,"%s/%s",dest,dirent->d_name);
  47. /* deschid fisierul sursa pentru citire */
  48. in_handle = (int)open(tmp_src, O_RDONLY );
  49. /* deschid fisierul destinatie pentru scriere */
  50. out_handle = (int)open(tmp_dest, O_CREAT | O_APPEND | O_WRONLY , mod);
  51. /* aloc memorie pentru buffer citire de marimea fisierului de intrare*/
  52.  
  53. buffer = malloc( stbuf.st_size);
  54. if (buffer == NULL)
  55. {
  56. printf("Eroare alocare dinamica\n");
  57. exit(0);
  58. }
  59. i = read(in_handle, buffer, stbuf.st_size);
  60. while ( i != 0){
  61. write(out_handle, buffer, i) ;
  62. i = read(in_handle, buffer, stbuf.st_size);
  63. }
  64.  
  65. /* close files */
  66. close(in_handle);
  67. close(out_handle);
  68. free(buffer);
  69.  
  70. }
  71. /* if this is a directory */
  72. else
  73. {
  74. sprintf(tmp_dest,"%s/%s",dest,dirent->d_name);
  75. CopyDirectory(tmp_src, tmp_dest, stbuf.st_mode);
  76. }
  77. }
  78. dirent = readdir(dp);
  79. }
  80. closedir(dp);
  81. }
  82.  
  83. }
  84.  
  85.  
  86. int main(int argc, char *argv[])
  87. {
  88.  
  89. char *source;
  90. char *dest;
  91. int optiune = 0;
  92. struct stat stbuf;
  93.  
  94. if ( argc != 4 )
  95. {
  96. printf("Numarul de parametri nu e corect : Sursa Destinatie Optiune\n");
  97. return 0;
  98. }
  99.  
  100. source = argv[1];
  101. dest = argv[2];
  102.  
  103. /* valideaza sursa */
  104. stat(source, &stbuf);
  105. if(!S_ISDIR(stbuf.st_mode))
  106. {
  107. printf("Directorul sursa nu e valid \n");
  108. return 0;
  109. }
  110.  
  111. /* Valideaza optiunea */
  112. optiune = atoi(argv[3]);
  113.  
  114. if (optiune == COPIE)
  115. {
  116. printf(" Copiaza de la %s la %s \n", source, dest);
  117. CopyDirectory(source,dest,stbuf.st_mode);
  118. }
  119. else if (optiune == LINK)
  120. {
  121. printf(" Link de la %s la %s \n", source, dest);
  122. symlink(source,dest);
  123. }
  124. else
  125. {
  126. printf(" Optiunea e incorecta : Copiere = 1 .. Link = 2\n");
  127. return 0;
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement