Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Explain exothermic and endothermic reactions with examples

Answered: 1 week ago

Question

Write a short note on rancidity and corrosiveness.

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago