Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. include <stdlib.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <grp.h>
  8. #include <pwd.h>
  9. #include <time.h>
  10. int comp (const void *i, const void *j, char *directory) {
  11. struct stat iStat;
  12. struct stat jStat;
  13. char *fileDirectory = (char*)malloc(1000000000);
  14. if(fileDirectory == NULL){
  15. perror("");
  16. exit(0);
  17. }
  18. strcat(fileDirectory, directory);
  19. strcat(fileDirectory, i);
  20. stat(fileDirectory, &iStat);
  21. fileDirectory = (char*)realloc(fileDirectory, 1000000000);
  22. strcat(fileDirectory, directory);
  23. strcat(fileDirectory, j);
  24. stat(fileDirectory, &jStat);
  25. free(fileDirectory);
  26. return jStat.st_size - iStat.st_size;
  27. }
  28.  
  29. void fill(char *array, struct stat fStat){
  30. if (fStat.st_mode & S_IRUSR)
  31. array[0] = 'r';
  32. if (fStat.st_mode & S_IWUSR)
  33. array[1] = 'w';
  34. if (fStat.st_mode & S_IXUSR)
  35. array[2] = 'x';
  36. if (fStat.st_mode & S_IRGRP)
  37. array[3] = 'r';
  38. if (fStat.st_mode & S_IWGRP)
  39. array[4] = 'w';
  40. if (fStat.st_mode & S_IXGRP)
  41. array[5] = 'x';
  42. if (fStat.st_mode & S_IROTH)
  43. array[6] = 'r';
  44. if (fStat.st_mode & S_IWOTH)
  45. array[7] = 'w';
  46. if (fStat.st_mode & S_IXOTH)
  47. array[8] = 'x';
  48. }
  49.  
  50. void print(struct stat fStat, char* array, char* f){
  51. if (S_ISDIR(fStat.st_mode)) {
  52. printf("%c%s%d%s%s%.6s%d\t%s%s\n%s", 'd', array, fStat.st_nlink, getgrgid(fStat.st_gid)->gr_name,
  53. getpwuid(fStat.st_uid)->pw_name, 4 + ctime(&fStat.st_mtime), fStat.st_size, "\e[1;36m", f, "\x1b[0m");
  54. }
  55. else if(S_ISREG(fStat.st_mode)) {
  56. printf("%c%s%d%s%s%.6s%d\t%s\n", '-', array, fStat.st_nlink, getgrgid(fStat.st_gid)->gr_name,
  57. getpwuid(fStat.st_uid)->pw_name, 4 + ctime(&fStat.st_mtime), fStat.st_size, f);
  58. }
  59. else if(S_ISLNK(fStat.st_mode)) {
  60. printf("%c%s%d%s%s%.6s%d\t%s\n", 'l', array, fStat.st_nlink, getgrgid(fStat.st_gid)->gr_name,
  61. getpwuid(fStat.st_uid)->pw_name, 4 + ctime(&fStat.st_mtime), fStat.st_size, f);
  62. }
  63. else {
  64. printf("%c%s%d%s%s%.6s%d\t%s\n", ' ', array, fStat.st_nlink, getgrgid(fStat.st_gid)->gr_name,
  65. getpwuid(fStat.st_uid)->pw_name, 4 + ctime(&fStat.st_mtime), fStat.st_size, f);
  66. }
  67. }
  68.  
  69. int countF(DIR *dir){
  70. int count = 0;
  71. while(readdir(dir) != NULL){
  72. ++count;
  73. }
  74. rewinddir(dir);
  75. return count;
  76. }
  77. void recursion(char *directoryName){
  78. DIR *dir = opendir(directoryName);
  79. if(dir == NULL){
  80. perror("Error");
  81. closedir(dir);
  82. return;
  83. }
  84. int countOfFiles = countF(dir);
  85.  
  86. if(directoryName[strlen(directoryName) - 1] != '/'){
  87. directoryName = (char *)realloc(directoryName, strlen(directoryName) + 2);
  88. strcat(directoryName, "/");
  89. }
  90. char* arrayOfFiles[countOfFiles];
  91. int j, k;
  92. for(j = 0; j < countOfFiles; ++j){
  93. arrayOfFiles[j] = (char*)malloc(1000000000);
  94. if(arrayOfFiles[j] == NULL){
  95. perror("");
  96. exit(0);
  97. }
  98. for(k = 0; k < strlen(readdir(dir)->d_name); ++k){
  99. arrayOfFiles[j][k] = readdir(dir)->d_name[k];
  100. }
  101. }
  102. qsort_r(arrayOfFiles, countOfFiles, sizeof(char *), (int (*)(const void *, const void *)) comp, directoryName);
  103. printf("%s:\n", directoryName);
  104. for(j = 0; j < countOfFiles; ++j){
  105. struct stat fStat;
  106. char *tempDirectory = (char*)malloc(1000000000);
  107. if(tempDirectory == NULL){
  108. perror("");
  109. exit(0);
  110. }
  111. int k;
  112. for(k = 0; k < strlen(directoryName); ++k){
  113. tempDirectory[k] = directoryName[k];
  114. }
  115. strcat(tempDirectory, arrayOfFiles[j]);
  116. stat(tempDirectory, &fStat);
  117.  
  118. char *array = (char *)malloc(10);
  119. if(array == NULL){
  120. perror("");
  121. exit(0);
  122. }
  123. for(k = 0; k < 9; ++k){
  124. array[k] = '-';
  125. }
  126. fill(array, fStat);
  127.  
  128. print(fStat, array, directoryName);
  129.  
  130. free(array);
  131. free(tempDirectory);
  132. }
  133.  
  134. printf("\n");
  135. for(k = 0; k < countOfFiles; ++k){
  136. if(!(strlen(arrayOfFiles[k]) == 1 && arrayOfFiles[k][0] == '.') && !(strlen(arrayOfFiles[k]) == 2 && arrayOfFiles[k][0] == '.' && arrayOfFiles[k][1] == '.')){
  137. struct stat Stat;
  138. char *fDir = (char*)malloc(1000000000);
  139. if(fDir == NULL){
  140. perror("");
  141. exit(0);
  142. }
  143. for(k = 0; k < strlen(directoryName); ++k){
  144. fDir[k] = directoryName[k];
  145. }
  146. strcat(fDir, arrayOfFiles[k]);
  147. stat(fDir, &Stat);
  148. recursion(fDir);
  149. free(fDir);
  150. }
  151. }
  152. for(j = 0; j < countOfFiles; ++j){
  153. free(arrayOfFiles[j]);
  154. }
  155. free(arrayOfFiles);
  156. free(directoryName);
  157. closedir(dir);
  158. }
  159. int main(int argc, char **argv) {
  160. for(int i = 1; i < argc; ++i){
  161. recursion(argv[i]);
  162. }
  163. return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement