UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old August 8th, 2004, 12:10 AM
bulsona bulsona is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 bulsona User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 31 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old August 8th, 2004, 04:24 AM
stevengs stevengs is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Germany
Posts: 394 stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level)stevengs User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 4 h 36 m 24 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
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

Reply With Quote
  #3  
Old August 8th, 2004, 07:39 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
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.

Reply With Quote
  #4  
Old August 8th, 2004, 02:36 PM
bulsona bulsona is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 bulsona User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 31 sec
Reputation Power: 0
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.

Reply With Quote
  #5  
Old August 11th, 2004, 03:28 AM
bulsona bulsona is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 16 bulsona User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 31 sec
Reputation Power: 0
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);
}
}

Reply With Quote
  #6  
Old August 11th, 2004, 09:24 AM
jim mcnamara jim mcnamara is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,299 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 8 h 45 m 44 sec
Reputation Power: 47
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;
}
...............

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > monitoring and printing a file using stat commands.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway