Question
Hello~ I can't solove this exercise problem. I sutdy Unix System Programming by Keith Haviland, Edition 2. That textbook has a example about exercise 3.10.
Hello~ I can't solove this exercise problem. I sutdy Unix System Programming by Keith Haviland, Edition 2.
That textbook has a example about exercise 3.10. I think, If this example is something changed, i can slove it.
but i can't solve. please help me. This programming should coding by C language.
Exexercise 3.10 (in Unix system programming by Keith Haviland, Dina Gray)
Write a program 'slowwatch' which periodically monitors the modification time of a named file(it should not fail if the file does not initially exist). When the file changes, 'slowwatch' should copy it to its standard output. How can you ensure (or guess) that the file is fully updated before it is copied?
Example /* lookout --print message when file changes */
#include
#include
#include
#define MFILE 10
void cmp(const char *, time_t);
struct stat sb;
main(int argc, char **argv){
int j;
time_t last_time[MFILE+1];
if(argc<2){
fprintf(stderr, "usage: lookout filename ... ");
exit(1);
}
if(--argc>MFILE){
fprintf(stderr, "lookout: too many filenames ");
exit(1);
}
/*initialization*/
for(j=1;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 statistics 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); eixt(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