Question
W e have implemented a prototype version of a C program, tree.c, for showing graphically on the standard output, the structure of files and folders
We have implemented a prototype version of a C program, tree.c, for showing graphically on the standard output, the structure of files and folders inside a particular folder, specified as follows
#./tree folder path
Regarding the output of the program; in every line on the standard output is shown the name of a file or the name of a folder between angle brackets in such a way that all the files and folders inside a particular folder are written in the same column. At the beginning of each line we have written the tag FILE or FOLDER in order to check the performance of the Linux utility.
Testing the performance of the program for the current directory, where we have the particular structure of files and folders,
< . >
tree.c
tree
file1.txt
file11.txt
file2.txt
we realize that, in principle, we get the right structure but the tag associated to files that are not in the first level of the tree is not right. So;
1.- Firstly, using the debugger gdb can you debug the program to find whats wrong in the prototype. Add comments to your findings and save the file as oldtree.c
2.- Secondly, submit a right version of the program.
Concepts in practice: Debugging with gdb, File Processing and File System.
*************************************************************************************************************
#include
//Prototypes int printFilesFolders(int level, char path[]); int is_regular_file(const char path[]);
int main(int argc, char *argv[]) { printFilesFolders(2, argv[1]); return 0; }
int is_regular_file(const char *path) { struct stat path_stat; stat(path, &path_stat); return S_ISREG(path_stat.st_mode); }
int printFilesFolders(int level, char path[]){
struct dirent *de; // Pointer for directory entry char space[200]={" "}; char tmp_path[200];
// opendir() returns a pointer of DIR type. DIR *dr = opendir(path);
if (dr == NULL) // opendir returns NULL if couldn't open directory { printf("Could not open current directory" ); return 0; }
// for readdir() int k; for (k=0; k while ((de = readdir(dr)) != NULL){ if (is_regular_file(de->d_name)){ printf("FILE: %s %s ",space, de->d_name); } else{ printf("FOLDER: %s <%s> ",space, de->d_name); strcpy(tmp_path,path); strcat(tmp_path,"/"); strcat(tmp_path,de->d_name); struct stat path_stat; stat(tmp_path, &path_stat); if ((strcmp(".",de->d_name)!=0) && (strcmp("..",de->d_name)!=0) && path_stat.st_mode==16877) printFilesFolders(level+10, tmp_path); strcpy(tmp_path,""); } } closedir(dr); return 0; }
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