Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include <pwd.h>
  10.  
  11. char * selTable[4];
  12. char selection[20];
  13. char * arg;
  14.  
  15. int dot_entry(char *str){
  16. return (strcmp(".", str) == 0) ||
  17. (strcmp("..", str) == 0);
  18. }
  19.  
  20. void procArg(char *uSource, char *uSelect, char *uArg){
  21. int j;
  22. selTable[0] = "-name";
  23. selTable[1] = "-ntime";
  24. selTable[2] = "-user";
  25. selTable[3] = NULL;
  26. fprintf(stdout,"source = %s, selection = %s, arg = %s.\n",uSource,uSelect,uArg);
  27. for(j=0; j<3; j=j+1){
  28. if(strcasecmp(uSelect,selTable[j])==0){
  29. strcpy(selection, uSelect);
  30. arg= (char *) malloc(strlen(uArg)*sizeof(char));
  31. strcpy(arg, uArg);
  32. }
  33. }
  34. }
  35. void visit_directory(char *path){
  36. DIR *directory;
  37. struct dirent *entry;
  38. struct stat file_attributes;
  39. struct passwd *pw;
  40. char *full_path;
  41. int path_length;
  42.  
  43. if ((directory = opendir(path)) == NULL) {
  44. fprintf(stderr, "invalid directory: %s\n", path);
  45. exit(EXIT_FAILURE);
  46. }
  47.  
  48. while ((entry = readdir(directory)) != NULL) {
  49. if (!dot_entry(entry->d_name)) {
  50. path_length = strlen(path) + strlen(entry->d_name) + 2;
  51. full_path = (char *) malloc(path_length * sizeof(char));
  52. strcat(full_path,path);
  53. strcat(full_path,"/");
  54. strcat(full_path, entry->d_name);
  55. if(lstat(full_path, &file_attributes)< 0){
  56. perror("There was an issue with this path de-allocating memory.\n");
  57. free(full_path);
  58. continue;
  59. }
  60. else{
  61. fprintf(stdout,"DIR %s\n",path);
  62. int mod_time = (int)file_attributes.st_mtime;
  63. int userID = file_attributes.st_uid;
  64. if(strcasecmp(selection,"-name")==0){
  65. if(strcasecmp(arg, entry->d_name)==0){
  66. if(S_ISDIR(file_attributes.st_mode)){
  67. fprintf(stdout, "DIR %s\n", entry->d_name);
  68. visit_directory(full_path);
  69. }
  70. else if(S_ISREG(file_attributes.st_mode)){
  71. fprintf(stdout, "REG %s\n", entry->d_name);
  72. }
  73. else{
  74. fprintf(stdout, "OTH %s\n", entry->d_name);
  75. }
  76. }
  77. }
  78. else if(strcasecmp(selection,"-mtime")==0){
  79. time_t mytime = time(NULL);
  80. int ctime = atoi(arg)*60;
  81. int utime = mytime - (ctime);
  82. if(utime <= mod_time){
  83. if(S_ISDIR(file_attributes.st_mode)){
  84. fprintf(stdout, "DIR %s\n", entry->d_name);
  85. visit_directory(full_path);
  86. }
  87. else if(S_ISREG(file_attributes.st_mode)){
  88. fprintf(stdout, "REG %s\n", entry->d_name);
  89. }
  90. else{
  91. fprintf(stdout, "OTH %s\n", entry->d_name);
  92. }
  93. }
  94. }
  95. else if(strcasecmp(selection,"-user")==0){
  96. fprintf(stdout,"DIR %s\n",path);
  97. fprintf(stdout,"%s\n",arg);
  98. pw = getpwnam(arg);
  99.  
  100. fprintf(stdout,"----- %s\n",pw->pw_uid);
  101. //printf(" %-8.8s", pw->pw_name);
  102. /*if(strcasecmp(arg, userID)==0){
  103. if(S_ISDIR(file_attributes.st_mode)){
  104. fprintf(stdout, "DIR %s\n", entry->d_name);
  105. visit_directory(full_path);
  106. }
  107. else if(S_ISREG(file_attributes.st_mode)){
  108. fprintf(stdout, "REG %s\n", entry->d_name);
  109. }
  110. else{
  111. fprintf(stdout, "OTH %s\n", entry->d_name);
  112. }
  113. }*/
  114. }
  115.  
  116. }
  117. }
  118. }
  119. free(full_path);
  120. closedir(directory);
  121. }
  122.  
  123. int main(int argc, char *argv[]){
  124. if (argc <= 1) {
  125. fprintf(stderr, "no directory specified\n");
  126. exit(EXIT_FAILURE);
  127. }
  128. fprintf(stdout,"%s %s %s\n",argv[1],argv[2],argv[3]);
  129. procArg(argv[1],argv[2],argv[3]);
  130. //fprintf(stdout,"DIR %s\n",argv[1]);
  131. visit_directory(argv[1]);
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement