Hey guys
I have a function that recursively searches a directory tree for files and grabs their size and name.
Code:
void printdir(char *rdir, char *dir, char *fullpath, int depth, int fd)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
struct stat st;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
printdir(rdir, entry->d_name, fullpath, depth+4, fd);
}
else if(S_ISREG(statbuf.st_mode))
{
char *name = entry->d_name
stat(entry->d_name, &st);
int size = st.st_size;
}
}
chdir("..");
closedir(dp);
}
It works fine but I'd like to get a file's name relative to the dir that it is searching, entry->d_name just returns the name of the file. I tried messing around with strcat() and strcpy() but I couldn't get desired results.