Question: Explain the below C Program syntax and what its doing? I'm a Java guy, having difficulty understanding. Output of the code is also shown below.

Explain the below C Program syntax and what its doing? I'm a Java guy, having difficulty understanding. Output of the code is also shown below. Line by Line brief explanation would be helpful. ***DO NOT EXPLAIN THE MAIN METHOD and INCLUDES*** (Those are provided for clarity only.).

/* printdir1.c */

#include #include #include #include #include #include

void printdir(char *dir, int depth) { DIR *dp; struct dirent *entry; struct stat statbuf;

if((dp = opendir(dir)) == NULL) { fprintf(stderr,"cannot open directory: %s ", dir); return; }

chdir(dir);

while((entry = readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf);

if(S_ISDIR(statbuf.st_mode)) { /* Found a directory, but ignore . and .. */ if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) { continue; }

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

/* Recurse at a new indent level */ printdir(entry->d_name,depth+4); } else { printf("%*s%s ",depth,"",entry->d_name); } } chdir(".."); closedir(dp); }

/* Now we move onto the main function. */

int main() { printf("Directory scan of /home: "); printdir("./",0); // print current directory entries printf("done. ");

exit(0); }

OUTPUT:

Directory scan of /home:

.cproject

.project

Debug/

Assignment_1

makefile

objects.mk

printdir1.d

printdir1.o

sources.mk

subdir.mk

printdir1.c

done.

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 Programming Questions!