Advertisement
Guest User

Untitled

a guest
May 15th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include <errno.h>
  7. #include <libgen.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. int dir_exists(char* path);
  12. void copy(char* file1, char* file2);
  13. void move(char* filename);
  14.  
  15. char* path = "/tmp/cmanchik/trash";
  16.  
  17. int main(int argc, char *argv[]){
  18. if(dir_exists(path) < 1){
  19. int result = mkdir(path, 0777);
  20. printf("Result: %d\n", result);
  21. }
  22. if(argc == 1){
  23. printf("No arguments passed. Please pass in a file name\n");
  24. exit(1);
  25. }
  26. int i;
  27. for(i = 1; i < (argc); i++){
  28. struct stat sb;
  29. char * g = malloc(strlen(path)+ strlen(argv[i])+1);
  30. strcpy(g,path);
  31. strcat(g,"/");
  32. strcat(g,argv[i]);
  33.  
  34. if (stat(g, &sb) == 0 && S_ISDIR(sb.st_mode))
  35. {
  36. struct dirent *de;
  37. DIR *dr = opendir(g);
  38. if (dr == NULL)
  39. {
  40. printf("Could not open current directory" );
  41. return 0;
  42. }
  43. while ((de = readdir(dr)) != NULL){
  44. if (strcmp(de->d_name,".") == 0 || strcmp(de->d_name,"..") == 0){
  45. continue;
  46. }else{
  47. printf("File %s\n",de->d_name);
  48. char* t = malloc(strlen("/tmp/cmanchik/trash/") + strlen(argv[i]) + strlen(de->d_name) + 1);
  49. strcpy(t, "/tmp/cmanchik/trash/");
  50. strcat(t, argv[i]);
  51. strcat(t, "/");
  52. strcat(t, de->d_name);
  53. move(t);
  54. }
  55.  
  56. }
  57.  
  58. closedir(dr);
  59. }
  60. else{
  61. char * t = malloc(strlen("/tmp/cmanchik/trash/") + strlen(argv[i]));
  62. strcpy(t, "/tmp/cmanchik/trash/");
  63. strcat(t,argv[i]);
  64. move(t);
  65. }
  66.  
  67. }
  68. }
  69.  
  70.  
  71. void move(char* filename){
  72. if(access(filename, F_OK) == -1){
  73. printf("File does not exist\n");
  74. }
  75. char* base = basename(filename);
  76.  
  77. char p[4096];
  78. getcwd(p,sizeof(p));
  79.  
  80.  
  81.  
  82. char* temp_path = malloc(strlen(p) + strlen(base) + 1);
  83.  
  84. strcpy(temp_path, p);
  85. strcat(temp_path, "/");
  86. strcat(temp_path, base);
  87. printf("Filename: %s\n",filename);
  88. printf("%s\n", temp_path);
  89. int ret = rename(filename, temp_path);
  90. if(ret == 0){
  91. printf("Successfully moved file\n");
  92. }else{
  93. copy(filename, temp_path);
  94. unlink(filename);
  95. }
  96.  
  97. }
  98.  
  99. void copy(char* file1, char* file2){
  100. FILE *ptr1, *ptr2;
  101. ptr1 = fopen(file1, "r");
  102. if(ptr1 == NULL){
  103. printf("First Cannot open file\n");
  104. }
  105. ptr2 = fopen(file2, "w");
  106. if(ptr2 == NULL){
  107. printf("Second Cannot open file\n");
  108. }
  109. char c = fgetc(ptr1);
  110. while(c != EOF){
  111. fputc(c, ptr2);
  112. c = fgetc(ptr1);
  113. }
  114. fclose(ptr1);
  115. fclose(ptr2);
  116.  
  117. }
  118.  
  119. int dir_exists(char* path){
  120. DIR* dir = opendir(path);
  121. if(dir){
  122. closedir(dir);
  123. return 1;
  124. }
  125. else if(ENOENT == errno){
  126. return 0;
  127. }
  128. else{
  129. return -1;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement