|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
VeriSign Code Signing Digital Certificates provides assurance to end users. Read about this and more in the free white paper: “How to Digitally Sign Downloadable Code for Secure Content Transfer.” Learn More! |
|
#1
|
|||
|
|||
|
monitoring and printing a file using stat commands.
I am trying to write a program that would monitor the file and print each time it changes.
I am trying to compare the contents of the file using the "stat" commands - st_mtime, st_atime and also st_size. Can anybody tell me if I have to approach this program by using stat commands like this or is there any other approach to the above problem? thanks |
|
#2
|
|||
|
|||
|
you could use diff, or checksum (called md5sum in linux, methinks).
Using diff requires a copy of the file(s) which could lead to problems with hard disk space depending on the size and number of files. With checksum you can save the results in a small file, but your processor overhead could be greater, again depending on the number and size of the files. What are you trying to do? If security is your goal, I believe the times are can be manipulated, and size checks are certainly not a clear indication of manipulation either. -Steven |
|
#3
|
|||
|
|||
|
You should stat the file and look at mtime. "tail -f" works that way. You might look at the source code for tail to see how it works.
|
|
#4
|
|||
|
|||
|
I am not so experienced in Unix and am trying to learn it these days.
I am trying to write this proram for a small file first to see how the logic works. But at the same time, I realized that diff command would create problems with the disk space for larger files. So I wanted to know, if stat structure works fine for this kind of a problem. Thanks a lot for writing back to me guys, I will let you know if I face any more problems. |
|
#5
|
|||
|
|||
|
please help me again
I am trying to execute the above program . The problem is when I am exeuting the file with a.out monitored_filename it doesnt show me anything on the screen. I am not aware of how should I change the monitored_file so that it follows the loop and prints something. How shuld I call that program to modify it. I tried to search it in the Unix book , but couldnt find anything. if(fstat(filedes,&stbuf) == -1) { printf("couldnt stat \n"); exit(1); } for(; { if(stbuf.st_mtime != last_time) printf("file modified"); last_time = stbuf.st_mtime; sleep(60); fstat(filedes,&stbuf); } } |
|
#6
|
|||
|
|||
|
consider using stat() not fstat() because fstat requires that you have the file open.
try something like this: Code:
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
static time_t last_time=0; /*global static variable*/
......................
if(stat("filename",&stbuf) == (-1))
{
perror("Could not stat file");
exit(EXIT_FAILURE);
}
last_time=stbuf.st_mtime;
for(;;)
{
sleep(60);
if( stat("filename",&stbuf) == (-1)){
perror("Could not stat file");
exit(EXIT_FAILURE);
}
if(stbuf.st_mtime != last_time){
printf("file modified");
}
last_time = stbuf.st_mtime;
}
...............
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > monitoring and printing a file using stat commands. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|