Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/statfs.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <dirent.h>
  10. #include <time.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13.  
  14. char* findFile(char* dir_path, const char* filename){
  15.     printf("dir_path: %s\n", dir_path);
  16.     DIR* dir;
  17.     if ((dir = opendir(dir_path)) == NULL) {
  18.         fprintf(stderr, "%s %s\n", __FILE__, strerror( errno ));
  19.         return NULL;
  20.     }
  21.     struct dirent *dir_entry;
  22.     while((dir_entry = readdir(dir)) != NULL){
  23.         printf("dir_entry: %s\n", dir_entry->d_name);
  24.         if(dir_entry->d_type == DT_REG
  25.         && strcmp(dir_entry->d_name, filename) == 0){
  26.             return strcat(strcat(dir_path, "/"), dir_entry->d_name);
  27.         }
  28.  
  29.         if(dir_entry->d_type == DT_DIR
  30.         && strcmp(dir_entry->d_name, ".")
  31.         && strcmp(dir_entry->d_name, "..")){
  32.             char* newDirPath = (char*)malloc(PATH_MAX*sizeof(char));
  33.             strcpy(newDirPath, dir_path);
  34.             strcat(newDirPath, "/");
  35.             strcat(newDirPath, dir_entry->d_name);
  36.             char* d = findFile(newDirPath, filename);
  37.             if(d != NULL){
  38.                 return d;
  39.             }
  40.         }
  41.     }
  42.  
  43.     return NULL;
  44. }
  45.  
  46. int main ( int argc, char **argv ) {
  47.     char *filename = (char*)malloc(PATH_MAX*sizeof(char));
  48.     char *dir_p = (char*)malloc(PATH_MAX*sizeof(char));
  49.     struct stat file_stat;
  50.     struct statfs file_stat_fs;
  51.  
  52.     strcpy(filename, argv[2]);
  53.     strcpy(dir_p, argv[1]);
  54.     char* file_path = findFile(dir_p, filename);
  55.     if(file_path != NULL) {
  56.         char actual_path [PATH_MAX];
  57.         realpath(file_path, actual_path);
  58.         stat(file_path, &file_stat);
  59.         statfs(file_path, &file_stat_fs);
  60.         char filesystem_name[10];
  61.        
  62.         /*
  63.             ext2/ext3      0xef53
  64.             ntfs           0x5346544e
  65.         */
  66.         switch (file_stat_fs.f_type)
  67.         {
  68.             case 0xef53:
  69.                 strcat(filesystem_name, "ext2/ext3");
  70.                 break;
  71.             case 0x5346544e:
  72.                 strcat(filesystem_name, "ntfs");
  73.                 break;
  74.             default:
  75.                 sprintf(filesystem_name, "%ld", file_stat_fs.f_type);
  76.                 break;
  77.         }
  78.         char file_created_time[200];
  79.         struct tm *tm = localtime(&file_stat.st_ctime);
  80.         sprintf(file_created_time, "%d.%d.%d_%d:%d:%d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  81.        
  82.         printf("%s %s %d %s ", filesystem_name, actual_path, file_stat.st_size, file_created_time);
  83.  
  84.         printf( (S_ISDIR(file_stat.st_mode)) ? "d" : "-");
  85.         printf( (file_stat.st_mode & S_IRUSR) ? "r" : "-");
  86.         printf( (file_stat.st_mode & S_IWUSR) ? "w" : "-");
  87.         printf( (file_stat.st_mode & S_IXUSR) ? "x" : "-");
  88.         printf( (file_stat.st_mode & S_IRGRP) ? "r" : "-");
  89.         printf( (file_stat.st_mode & S_IWGRP) ? "w" : "-");
  90.         printf( (file_stat.st_mode & S_IXGRP) ? "x" : "-");
  91.         printf( (file_stat.st_mode & S_IROTH) ? "r" : "-");
  92.         printf( (file_stat.st_mode & S_IWOTH) ? "w" : "-");
  93.         printf( (file_stat.st_mode & S_IXOTH) ? "x" : "-");
  94.  
  95.         printf(" %d\n", file_stat.st_ino);
  96.     } else {
  97.         printf("File \"%s\" not found.\n", filename);
  98.     }
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement