I am trying to build a program that recursively lists all folders and files in a directory, along with their file sizes. I'm still working on the first part because the program only seems to go one level of sub-folder in depth.
Can anyone spot the issue here? I'm stuck.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <unistd.h>
void listdir(const char *name) {
DIR *dir;
struct dirent *entry;
int file_size;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("./%s\n", entry->d_name);
listdir(entry->d_name);
}
else
printf("./%s\n", entry->d_name);
} while (readdir(dir) != NULL);
closedir(dir);
}
int main(void)
{
listdir(".");
return 0;
}