Add documentation to program to explain its functionality Ad
Add documentation to program to explain its functionality
Add a header comment to the top of the program explaining the overall purpose of the program.
Add internal comments within the program to explain the purpose of different sections of the program.
Create a short of description of WHY the program works the way it does. Do not just document the output of the program… you must also explain why the program provides that output.
Program:
printdir1.c
/* printdir1.c
* Documentation intentially left out as part of the assignment
*/
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
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);
}
Solution
/* printdir1.c
* The program scans the current directory and print all
* the file names of current directory
*/
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir, int depth)
{
// first input parameter is the current directory location
// 2nd input location is the depth upto which you want to go and print file names
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
// directory name validity check
fprintf(stderr,\"cannot open directory: %s\ \", dir);
return;
}
chdir(dir); // change to the directory, i.e go to the directory
while((entry = readdir(dp)) != NULL) {
// read files one by one
lstat(entry->d_name,&statbuf); // store the data about the file in statbuf
if(S_ISDIR(statbuf.st_mode)) {
// we are inside a directory
/* Found a directory, but ignore . and .. */
if(strcmp(\".\",entry->d_name) == 0 ||
strcmp(\"..\",entry->d_name) == 0) {
// if the file name is . or .. ignore it
continue;
}
printf(\"%*s%s/\ \",depth,\"\",entry->d_name); // otherwise print the directory name
// format the printing by depth level, i.e if the directory is deeper , we will use more tabs
/* Recurse at a new indent level */
printdir(entry->d_name,depth+4);
}
else {
printf(\"%*s%s\ \",depth,\"\",entry->d_name); // it is a simple file, print the file 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);
}
Why does the program works
The program basically prints the file names of current directory and sub directory. It use system calls to read all the filenames as well as directory names. If it is file, it prints the file name directly but if it is directory, it goes inside the directory and recursively do the same work.


