Listing File Info

Hi,

From a Unix book, i'd found that the way to list files in a directory.
But those file info are all not shown, wat is shown is only the file name.
Can anyone pls teach me how to show the file details?
(etc file size, read/write permission, modified date)

my code:

Dir *dirp;
struct dirent *direntp;

while((direntp = readdir(dirp)) != NULL)
printf("%s\n",direntp->d_name);

Thx for ur help!

Once you have the file name, use that with the lstat() system call. This will return you a all of the inode information in a stat structure. Then you can just print out the elements of the structure. It should be pretty easy. Take a look at the man page for lstat().

while ((direntp = readdir(dirp)) != NULL)
{
if (lstat(direntp->d_name, &statbuf) == -1){
fprintf("...");
else
printf("%s\t%s\n",direntp->d_name, statbuf.st_size);

}

Is it the way to do it? :slight_smile:

Provided that you previously did a chdir() into this directory, the lstat will work. But look at the definition of the statbuf structure. You can't use a %s to display statbuf.st_size.