Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using onlinegdb.com, write a c code, and show proof of the output that you are supposed to get, and explain which command line arguments you

Using onlinegdb.com, write a c code, and show proof of the output that you are supposed to get, and explain which command line arguments you are suppose to put to get the output. PLEASE SUBMIT ONE BIG CODE, THAT IS ALL TOGETHER.
NOTE YOU ARE NOT ALLOWED TO USE FTW() OR ANY SIMILAR LIBRARY FUNCTION!
What to do?
Write a C program myfind whose behavior resembles that of the system command f ind.
myfind accepts the following parameters:
$ myfind filename startpath
where
filename is a substring that is matched against each filename in each directory that my f ind
inspects. If a file name matches f ilename or if the substring filename occurs within a file
name, the name of that file is printed.
startpath is an optional parameter that specifies the directory where myfind must start the
search. If this parameter is not specified, my f ind starts in the current directory.
For each file whose name is a match, myfind must print its name and its mode.
For each directory in which myfind finds a match, it must print that directory's full path.
Test myfind using various directories on your computer attempting to match full and partial file names.
Sample files and directories are available in the class github repository. Below is a typical output produced
by myfind
I have this code, but i can not get the necessarry output.
#include
#include
#include
#include
#include
#include
// Function prototypes
void search_directory(const char *startpath, const char *filename);
int main(int argc, char *argv[]){
if (argc 2){
fprintf(stderr, "Usage: %s filename [startpath]
", argv[0]);
return EXIT_FAILURE;
}
const char *filename = argv[1];
const char *startpath =(argc >2)? argv[2] : ".";
search_directory(startpath, filename);
return EXIT_SUCCESS;
}
// Function to convert mode to a string
void mode_to_string(mode_t mode, char *str){
sprintf(str,"%04o/", mode & 0777);
str[5]=(mode & S_IRUSR)?'r' : '-';
str[6]=(mode & S_IWUSR)?'w' : '-';
str[7]=(mode & S_IXUSR)?'x' : '-';
str[8]=(mode & S_IRGRP)?'r' : '-';
str[9]=(mode & S_IWGRP)?'w' : '-';
str[10]=(mode & S_IXGRP)?'x' : '-';
str[11]=(mode & S_IROTH)?'r' : '-';
str[12]=(mode & S_IWOTH)?'w' : '-';
str[13]=(mode & S_IXOTH)?'x' : '-';
str[14]='\0';
}
// Function to search directories
void search_directory(const char *startpath, const char *filename){
DIR *dir = opendir(startpath);
if (!dir){
perror("opendir");
return;
}
struct dirent *entry;
while ((entry = readdir(dir))!= NULL){
if (strcmp(entry->d_name, ".")==0|| strcmp(entry->d_name, "..")==0){
continue;
}
char path[PATH_MAX];
snprintf(path, sizeof(path),"%s/%s", startpath, entry->d_name);
struct stat statbuf;
if (stat(path, &statbuf)==-1){
perror("stat");
continue;
}
if (S_ISDIR(statbuf.st_mode)){
// Print directory path if it contains a match
if (strstr(entry->d_name, filename)){
printf("%s
", path);
}
// Recursively search subdirectories
search_directory(path, filename);
} else {
// Print file details if it contains a match
if (strstr(entry->d_name, filename)){
char mode_str[15];
mode_to_string(statbuf.st_mode, mode_str);
printf("%s (%s)
", path, mode_str);
}
}
}
closedir(dir);
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

10. What is meant by a feed rate?

Answered: 1 week ago

Question

What is A free product or gift?

Answered: 1 week ago