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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!