What you need to do is implement a recursive "find" function, that will recursively traverse the directory structure, this is the fastest way I know of finding every file in a filesystem structure
Here is some example code I wrote that executes a supplied function on every file within a supplied path including all sub-directories, ie so you could give it the root path / to get all files in the filesystem....
This example is for a Linux machine using the dirent structure:
Code:
/* These are the macros being used in the function below */
#define ff_get_dname(d) ((d)->d_name) /* just change it once here :) if needbe */
#define ff_fmt_str(p) (*((p)+strlen((p))-1)=='/'?"%s%s":"%s/%s")
#define ff_overhead 2 /* \0 and possible / so we add 2 to the string buffer */
Code:
int find_path(int (*exec)(const char *), const char * path)
{
DIR * dptr; /* directory pointer */
register struct dirent * cur_dirent; /* current entry in directory */
struct stat sb; /* information about entry, type etc.. */
register char * pathbuf; /* string buffer for constructing filepath */
register char * p; /* working pointer */
size_t allocsiz; /* holds size to currently allocate */
if (!(dptr = opendir(path)))
return 1;
while (cur_dirent = readdir(dptr)) { /* intentional = */
allocsiz = strlen(path)+strlen(ff_get_dname(cur_dirent))+ff_overhead;;
if (!(pathbuf = malloc(allocsiz)))
continue; /* skip file, couldn't get that much memory */
sprintf(pathbuf,ff_fmt_str(path),path,ff_get_dname(cur_dirent));
if (lstat(pathbuf,&sb) < 0) {
free(pathbuf); /* free up our buffer */
continue; /* failed to stat, skip */
}
if (S_ISDIR(sb.st_mode)) {
p = ff_get_dname(cur_dirent);
while (*p == '.') /* skip . or .. or ...etc.. */
p++;
if (*p)
find_path(exec,pathbuf);
}
if (S_ISREG(sb.st_mode)) /* regular file */
(void) exec(pathbuf); /* postive value indicates some sort of error occured */
free(pathbuf); /* free our string buffer */
}
closedir(dptr);
return 0;
}
Some declarations might be left out but you get the idea