0

I need to print all possible file attributes in a directory such as file size, type, ect. The code below uses the dirent structure and it doesnt have all the file information, like file size. The d_type returns some integer, but i can find a chart to look up the meaning of those numbers. I am printing the inode and the name of the file. Can i use the inode to get more information about the file with c commands?

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h> 
#include <sys/stat.h>
#include <errno.h>

int main(int ac, char *av[])
{


  DIR *d;
  struct dirent *dir;
  d = opendir("/home/CS/user/unix/Project4/TestDirectory");
  if (d) {
    while ((dir = readdir(d)) != NULL) {
       printf("Name: %s \n", dir->d_name);
       printf("- Inode: %lu\n", dir->d_ino);
       printf("- reclen: %u\n", dir->d_reclen);
       printf("- type: %u\n", dir->d_type);

    }
    closedir(d);
  }
  return(0);

}
Some_Dude
  • 309
  • 5
  • 21
  • 2
    You need to call `stat`, or maybe `lstat`, on each file. (And when you do you'll need to be careful, as explained at [this question](https://stackoverflow.com/questions/34168266/).) – Steve Summit Mar 17 '18 at 19:39
  • There is no user-level interface in POSIX that uses an inode number to identify the file. You have to use a file name somewhere along the line. There are security reasons for this, amongst other reasons. – Jonathan Leffler Mar 17 '18 at 21:24

0 Answers0