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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started