I have this recursive function to search the tree structure of files. And I need to find out the parameters of each file (type, owner, group, permissions, creation date, last modification date, ..) How to do it?
void search(const char * path)
{
char newpath[PATH_SIZE + 1];
DIR * dp;
struct dirent * ep;
dp = opendir(path);
if (dp == NULL)
return;
while ((ep = readdir(dp)) != NULL)
{
if (strcmp(".", ep->d_name) == 0 ||
strcmp("..", ep->d_name) == 0)
{
continue;
}
printf("%s/%s\n", path, ep->d_name);
if ((ep->d_type & DT_DIR) == DT_DIR)
{
if (strlen(path) + strlen(ep->d_name) + 1 <= PATH_SIZE)
{
sprintf(newpath, "%s/%s", path, ep->d_name);
search(newpath);
}
}
}
closedir(dp);
return;
}
I only know the type of file (ep-> d_type);
By the stat() function:
Manual:
man 2 statPrototype:
where the stat structure is defined as: