1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<time.h>
#define COMMAND_LEN 1024
#define DATA_SIZE 1024
int inodeData(char* file)
{
struct stat sb;
printf("FILE:\t\t\t%s", file);
printf("FILE TYPE:\t\t");
switch (sb.st_mode & S_IFMT)
{
case S_IFBLK: printf("BLANK DEVICE\n"); break;
case S_IFCHR: printf("CHARACTER DEVICE\n"); break;
case S_IFDIR: printf("DIRECTORY\n"); break;
case S_IFIFO: printf("SYMLINK\n"); break;
case S_IFLNK: printf("REGULAR FILE\n"); break;
case S_IFSOCK: printf("SOCKET\n"); break;
default: printf("UNKNOWN\n"); break;
}
printf("I-NODE NUMBER: %ld\n", (long) sb.st_ino);
printf("MODE: %lo (octal)\n",
(unsigned long) sb.st_mode);
printf("LINK COUNT: %ld\n", (long) sb.st_nlink);
printf("OWNERSHIP: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);
printf("I/O BLOCK SIZE: %ld bytes\n",
(long) sb.st_blksize);
printf("FILE SIZE: %lld bytes\n",
(long long) sb.st_size);
printf("BLOCKS ALLOCATED: %lld\n", (long long) sb.st_blocks);
printf("FILE PERMISSIONS:\t");
printf((S_ISDIR(sb.st_mode)) ? "d" : "-");
printf((sb.st_mode & S_IRUSR) ? "r" : "-");
printf((sb.st_mode & S_IWUSR) ? "w" : "-");
printf((sb.st_mode & S_IXUSR) ? "x" : "-");
printf((sb.st_mode & S_IRGRP) ? "r" : "-");
printf((sb.st_mode & S_IWGRP) ? "w" : "-");
printf((sb.st_mode & S_IXGRP) ? "x" : "-");
printf((sb.st_mode & S_IROTH) ? "r" : "-");
printf((sb.st_mode & S_IWOTH) ? "w" : "-");
printf((sb.st_mode & S_IXOTH) ? "x" : "-");
printf("\nLAST STATUS CHANGE: %s", ctime(&sb.st_ctime));
printf("LAST FILE ACCESS: %s", ctime(&sb.st_atime));
printf("LAST FILE MODIFICATION: %s\n", ctime(&sb.st_mtime));
return 0;
}
int main(int argc, char **argv)
{
FILE *pf;
char command[COMMAND_LEN];
char data[DATA_SIZE];
char inode_data[DATA_SIZE];
char input[DATA_SIZE];
int iNode;
printf("Enter inode number to be found: ");
scanf("%s", input);
iNode = (int)atol(input);
/*Uses the sprintf function to make a command line command to
search for the file name based on the provided inode number*/
sprintf(command, "find . -inum %i -type f", iNode);
pf = popen(command, "r");
if(!pf){
fprintf(stderr, "Could not open pipe for output.\n");
return;
}
/*Grabs data from process execution*/
fgets(data, DATA_SIZE, pf);
/*function call to display file information*/
inodeData(data);
if (pclose(pf) !=0)
fprintf(stderr, "Error: Failed to close command stream!\n");
return 0;
}
|