I am assigning a different value to *fd each time I iterate through the while loop, yet it seems that the stat information is not dynamic. The objective is to take a directory that is passed as a command line argument, loop through the files in that directory, and print relevant stat info. The file name that prints is dynamic, but the stat info is not.
Can anyone offer any insight into this?
while ((pDirent = readdir(pDir)) != NULL)
{
char *fd = pDirent->d_name;
struct stat *buf;
buf = malloc(sizeof(struct stat));
stat(fd, buf);
if (pDirent->d_name[0] != '.' && (lSwitch || noSwitch))
{
int i=0;
for (i = 0; i<4; i++)
{
printf("%s", " ");
}
printf("%s", pDirent->d_name);
if (lSwitch)
{
// print the file type
if (S_ISLNK(buf->st_mode))
printf(" L ");
else if (S_ISFIFO(buf->st_mode))
printf(" P ");
else if (S_ISREG(buf->st_mode))
printf(" F ");
else if (S_ISDIR(buf->st_mode))
printf(" D ");
else
printf(" L ");
printf("number of 512 blocks is %ld\n", buf->st_blocks);
}
printf("\n");
}
free(buf);
}