Explain the below C Program syntax and what its doing? I\'m a Java guy, having d
ID: 3665369 • Letter: E
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. 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.
Explanation / Answer
//I have added more comments for u to understand it better
//Preprocessing directives
#include
#include
#include
#include
#include
#include
//Function defination in c.This function takes 2 paramters.One is a pointer variable and the other one is a normal //variable
void printdir(char *dir, int depth)
{
//pointer variable declaration in c.A pointer variable is the variable which keeps the address of other or //normal variable.Here DIR is a directory pointer
DIR *dp;
struct dirent *entry;
struct stat statbuf;
//opening a directory.i.e If dp pointer contains null value,then we cannot open a directory.So appropriate //error message is printed.In c fprintf and fscanf //commands are used when we deal with writing and //reading from files.If dp variable is one means,we can open a directory
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("..");
//command to close a directory.This we do by closing the pointer variable,which holds its value.
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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.