There is actually an ansi way of doing this. You should look into dirent.h. Try something like this out:
(NOTE: much of this was from "The C Programming Language")
Code:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
void dirwalk(char *dir, void (*fcn)(char *)) {
char name[1024];
struct dirent *dp;
DIR *dfd;
if((dfd = opendir(dir)) == NULL) {
fprintf(stderr, "dirwalk: can't open %s\r\n", dir);
return;
}
while((dp = readdir(dfd)) != NULL) {
if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
if(strlen(dir) + strlen(dp->d_name)+2 > sizeof(name))
fprintf(stderr, "dirwalk: name %s/%s too long\r\n", dir, dp->d_name);
else {
sprintf(name, "%s/%s", dir, dp->d_name);
(*fcn)(name);
}
}
closedir(dfd);
}
void fsize(char *name) {
struct stat stbuf;
if(stat(name, &stbuf) == -1) {
fprintf(stderr, "fsize: can't access %s\r\n", name);
return;
}
if((stbuf.st_mode & S_IFMT) == S_IFDIR) {
dirwalk(name, fsize);
printf("%s/\n", name);
} else {
printf("%8ld %s\r\n", stbuf.st_size, name);
puts(name);
}
}
int main(int argc, char** argv) {
if(argc != 2) {
printf("whatever.exe directory");
return 1;
}
fsize(argv[1]);
return 1;
}
If there is something wrong with the code (probably) just remember that this is old code that I just pulled out of a very old project and made a couple of quick changes to.