Question: C PROGRAM! test.txt is in another program, which is prog1.c. code for that is after below question & code. you will see prog1.c Prog2.c: Modify

C PROGRAM! test.txt is in another program, which is prog1.c. code for that is after below question & code. you will see prog1.c

Prog2.c:

Modify the template program, lookout.c below these questions with the following additional capabilities

  1. Repeat the following steps 100 times
    1. Wait 1 seconds using sleep() call
    2. Call stat system call to extract the file attributes against test.txt
    3. If you find any changes in any of the attributes,
      1. Print the information on which attribute is changed. Make sure to print the previous value and the new value (i.e., the changed attribute value). There may exist more than 1 attribute values changed.

prog2.c (2nd code to be modified)

// 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); } }

Following is prog1.c. you extract test.txt by running two terminal(both program, prog1.c & prog2.c). Modify the above code only!!

#include  #include  #include  #include  int main() { // opening test.txt for reading and writing // with 0644 permission mode const char *path_test_txt = "test.txt"; int fd = open(path_test_txt, O_CREAT | O_RDWR, 0644); // 10 times for(int i = 0; i < 10; ++i) { // writing abcdefg const char *abcdefg = "abcdefg"; write(fd, abcdefg, strlen(abcdefg)); // waiting for 2 seconds sleep(2); // changing file permission mode to 0666 chmod(path_test_txt, 0666); // waiting for 2 seconds sleep(2); // linking the file with tset.txt const char *path_tset_txt = "tset.txt"; link(path_test_txt, path_tset_txt); // waiting for 2 seconds sleep(2); // unlinking the file unlink(path_tset_txt); // waiting for 2 seconds sleep(2); // reading 80 characters from test.txt char buf[80]; read(fd, buf, 80); } // end of program return 0; }

Many thanks!!

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!