Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the template program, lookout . c on p . 5 6 - 5 7 with the following additional capabilities Repeat the following steps 5

Modify the template program, lookout.c on p.56-57 with the following additional capabilities
Repeat the following steps 5 times (this means, after 5 seconds of running, your program should terminate without printing anything if there is no change detected.)
Wait 1 seconds using sleep(1) call
Call stat system call to extract the file attributes against given files in command line arguments (the lookout.c program in p.56 may accept up to 10 files). Your program should be able to accept up to 10 files for monitoring.
You program should check any changes in any of the following attributes:
st_mode
st_nlink
st_size
st_atime
st_mtime
When your program detects any changes, your program should immediately print out number codes given below and EXIT!
Change of st_mode print 1
Change of st_nlink print 2
Change of st_size print 3
Change of st_atime print 4
Change of st_mtime print 5
Once printing is done, your program should terminate. Make sure your condition checking and printing should be done in the above order! Make sure you print number codes in one line (without any newline character)!
Example output when you change the file contents (e.g, using vi): 345
If your program prints more than one number code, then place spaces in between.
Important: if your program doesnt print the corresponding number codes or if it prints more information than number codes, then your program will fail the tests, and you will get zero point for the tests. If there is no change detected during your program's execution, your program should exit without printing anything.
Testing of your program can be done by opening two terminals, and run lookout in one terminal and, in another terminal, make some changes to the file contents, file permission modes, number of links, by using a variety of shell commands such as vi,chmod,link,unlink, etc.
In the terminal where lookout.c is running, the program should print the assigned code into display and exit.
Here is the lookout.c template program:
// lookout -- print message when file changes
#include
#include
#include
#define MFILE 10
void cmp(const char *, time_t);
struct stat sb;
int main(int argc, char **argv)
{
int j;
time_t last_time[MFILE+1];
if (argc <2){
fprintf(stderr, "usage: lookout filename ...
");
exit(1);
}
//int stat(const char *path, struct stat *buf);
if (--argc > MFILE ){
fprintf(stderr, "lookout: too many filenames
");
exit(1);
}
//Initialization
for(j =1, j <= argc; j++;)
if(stat(argv(j), &sb)==-1)
{
fprintf(stderr, "lookout: couldn't stat %s
", argv(j));
exit(1);
}
last_time(j)= sb.st_mtime;
//loop until file changes
for(;;)
{
for(j =1; j <= argc; j++)
cmp(argv(j), last_time(j));
/*rest for 60 SECONDS "sleep" is a standard UNIX library routine */
sleep(60);
}
}
void cmp(const char *name, time_t last)
{
/*as long as statics about the file can be read check the modification time */
if(stat(name, &sb)==-1|| sb.st_mtime != last)
{
fprintf(stderr, "lookout: %s changed
", name);
exit(0);
}
}
Please show test runs so I can compare if something goes wrong when I test it myself.

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_2

Step: 3

blur-text-image_step3

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

Describe Table Structures in RDMSs.

Answered: 1 week ago