Question
Part I Modify the C program given on slide 18, from lecture 1, to obtain a program, call it findFile , to find a file
Part I
Modify the C program given on slide 18, from lecture 1, to obtain a program, call it findFile, to find a file or a directory in the current directory. In particular, your program takes a file-name as an argument.
Synopsis: findFile
Call example:
findFile lab01.c: search for lab01.c in current directory.
If name is found, it prints a line with the name found followed by either this is a file or this is a directory. Hints:
- All system calls needed to solve this problem are in the example myLs.c, that we have seen in class.
- You can check the field d type to distinguish between a regular file and a directory.
if (dirp->d_type == DT_DIR) // true if a directory
- Ignore the two special directories . and ..
Part II
Modify your program so that findFile finds a file or a directory in the current directory hierarchy (i.e., searches recursively in subdirectories).
Call example: findFile lab01.c: searches for lab01.c in current directory and all its subdirectories
Hint: use a separate function to search for file-name.
code:
//myls.c
#include
#include
#include
int main(int argc, char *argv[]){ DIR *dp;
struct dirent *dirp;
if(argc==1)
dp = opendir("./"); else
dp = opendir(argv[1]);
while ( (dirp=readdir(dp)) != NULL) printf("%s ", dirp->d_name);
closedir(dp); exit(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