Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/statfs.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <dirent.h>
- #include <time.h>
- #include <sys/types.h>
- #include <unistd.h>
- char* findFile(char* dir_path, const char* filename){
- printf("dir_path: %s\n", dir_path);
- DIR* dir;
- if ((dir = opendir(dir_path)) == NULL) {
- fprintf(stderr, "%s %s\n", __FILE__, strerror( errno ));
- return NULL;
- }
- struct dirent *dir_entry;
- while((dir_entry = readdir(dir)) != NULL){
- printf("dir_entry: %s\n", dir_entry->d_name);
- if(dir_entry->d_type == DT_REG
- && strcmp(dir_entry->d_name, filename) == 0){
- return strcat(strcat(dir_path, "/"), dir_entry->d_name);
- }
- if(dir_entry->d_type == DT_DIR
- && strcmp(dir_entry->d_name, ".")
- && strcmp(dir_entry->d_name, "..")){
- char* newDirPath = (char*)malloc(PATH_MAX*sizeof(char));
- strcpy(newDirPath, dir_path);
- strcat(newDirPath, "/");
- strcat(newDirPath, dir_entry->d_name);
- char* d = findFile(newDirPath, filename);
- if(d != NULL){
- return d;
- }
- }
- }
- return NULL;
- }
- int main ( int argc, char **argv ) {
- char *filename = (char*)malloc(PATH_MAX*sizeof(char));
- char *dir_p = (char*)malloc(PATH_MAX*sizeof(char));
- struct stat file_stat;
- struct statfs file_stat_fs;
- strcpy(filename, argv[2]);
- strcpy(dir_p, argv[1]);
- char* file_path = findFile(dir_p, filename);
- if(file_path != NULL) {
- char actual_path [PATH_MAX];
- realpath(file_path, actual_path);
- stat(file_path, &file_stat);
- statfs(file_path, &file_stat_fs);
- char filesystem_name[10];
- /*
- ext2/ext3 0xef53
- ntfs 0x5346544e
- */
- switch (file_stat_fs.f_type)
- {
- case 0xef53:
- strcat(filesystem_name, "ext2/ext3");
- break;
- case 0x5346544e:
- strcat(filesystem_name, "ntfs");
- break;
- default:
- sprintf(filesystem_name, "%ld", file_stat_fs.f_type);
- break;
- }
- char file_created_time[200];
- struct tm *tm = localtime(&file_stat.st_ctime);
- 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);
- printf("%s %s %d %s ", filesystem_name, actual_path, file_stat.st_size, file_created_time);
- printf( (S_ISDIR(file_stat.st_mode)) ? "d" : "-");
- printf( (file_stat.st_mode & S_IRUSR) ? "r" : "-");
- printf( (file_stat.st_mode & S_IWUSR) ? "w" : "-");
- printf( (file_stat.st_mode & S_IXUSR) ? "x" : "-");
- printf( (file_stat.st_mode & S_IRGRP) ? "r" : "-");
- printf( (file_stat.st_mode & S_IWGRP) ? "w" : "-");
- printf( (file_stat.st_mode & S_IXGRP) ? "x" : "-");
- printf( (file_stat.st_mode & S_IROTH) ? "r" : "-");
- printf( (file_stat.st_mode & S_IWOTH) ? "w" : "-");
- printf( (file_stat.st_mode & S_IXOTH) ? "x" : "-");
- printf(" %d\n", file_stat.st_ino);
- } else {
- printf("File \"%s\" not found.\n", filename);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement