Advertisement
Guest User

Untitled

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