Question: c programming - can anyone explain to me how does the following code work recursively? I highlighted the parts that I don't quit understand. The

c programming - can anyone explain to me how does the following code work recursively? I highlighted the parts that I don't quit understand.

The code should be able to print out all of directory and its subdirectories recursively

#include

#include

void listdir(const char *name)

{

DIR *dir;

struct dirent *entry;

if (!(dir = opendir(name)))

return;

if (!(entry = readdir(dir)))

return;

do {

if (entry->d_type == DT_DIR) {

char path[1024];

int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);

path[len] = 0;

if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)

continue;

printf("%s/%s ", name, entry->d_name);

listdir(path);

}

else

printf("%s/%s ",name, entry->d_name);

} while (entry = readdir(dir));

closedir(dir);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!