Thread: FileStat.st_mtime confusion

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    FileStat.st_mtime confusion

    Hello all,
    I am editing a piece of code that has the following:

    Code:
     FileStat.st_mtime = 0l;
       stat("infile",&FileStat);
       time(&chktime);
       if(FileStat.st_mtime+7776000l<chktime ){
          up(error6);
          time_delay(500);
          dn(error6);
          exit(FALSE);
          }
    It's a security measure on the code. My assumption is that it checks the modified date of the infile, adds 77760001 and makes sure it's less than chktime. Does that sound correct?

    Is chktime the current system time?

    The thing I don't understand about the code, is what the 77760001 represents. How can i figure what the modification date and system time must be to get into and/or bypass this function?

    Thanks in advance!

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    chktime is a variable that has it's value assigned somewhere else in the code. Do a search to find it in the source.

    Time in C works with seconds elapsed since a "start time" - there are 86400 seconds per day, so 77760001 /86400 = 900 days + 1 second. It is checking to see if the file was modified more than 900 days earlier than the time specified by chktime.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't you love code with arbitrary numbers sprinkled around in it? To think people say macros are useless! Hah!


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Jim thanks a million for the help. The value chktime isn't specified anywhere else in the code. It is defined:

    Code:
    long int   chktime;
    but that's it.

    Does that not make sense?


    There is also another part of the code that does this:

    Code:
     stat("infile2",&FileStat);
       if(FileStat.st_mtime!=copied){
          up(error7);
          time_delay(500);
          dn(error7);
          exit(FALSE);
          }
    
    where
    
    long int   copied = 535523774;
    how does that one make sense?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    In the first example, time() is storing the current system time in chktime. Look at the man page for time():
    SYNOPSIS
    #include <time.h>

    time_t time(time_t *t);

    DESCRIPTION
    time returns the time since the Epoch (00:00:00 UTC, Jan..
    uary 1, 1970), measured in seconds.

    If t is non-NULL, the return value is also stored in the
    memory pointed to by t.


    RETURN VALUE
    On success, the value of time in seconds since the Epoch
    is returned. On error, ((time_t)-1) is returned, and
    errno is set appropriately.
    The second one is doing something very similar using a different value. What don't you understand about it?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by itsme86
    In the first example, time() is storing the current system time in chktime. Look at the man page for time():

    The second one is doing something very similar using a different value. What don't you understand about it?

    I just don't understand what 535523774 is equal to in terms of last modified date.

    If it's seconds, then it's 6198.1918 days. Do I have to figure what's that many days past Janurary 1st 1970 in order to figure out what the last modified date of the file has to be to get into the function?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's the number of seconds since the Epoch, yeah. But you don't have to figure it out yourself. You would if C didn't have a bunch of useful time-related functions. Fortunately, that isn't the case. C has plenty of useful time-related functions, such as ctime():
    Code:
    itsme@itsme:~/C$ cat ctime.c
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
      time_t t = 535523774;
    
      printf("%s", ctime(&t));
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./ctime
    Sat Dec 20 20:36:14 1986
    itsme@itsme:~/C$
    You'll want to run it against your system's local time. The system I ran that on is PDT (GMT-8).

    EDIT: For more information on ctime() and other functions look at: http://unixhelp.ed.ac.uk/CGI/man-cgi?ctime+3
    Last edited by itsme86; 11-09-2005 at 01:21 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Thank you itsme for your time and patience. I jsut realized that the first snippet:

    Code:
    FileStat.st_mtime = 0l;
       stat("infile",&FileStat);
       time(&chktime);
       if(FileStat.st_mtime+7776000l<chktime ){
          up(error6);
          time_delay(500);
          dn(error6);
          exit(FALSE);
          }
    is actually 7776000L (alpha L, not a 1). Do you know what that means?

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Following a numeric constant with L tells the compiler that it's type long.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Thank you itsme for your time and patience.

    So if the code is as follows:

    Code:
    FileStat.st_mtime = 0l;
       stat("infile",&FileStat);
       time(&chktime);
       if(FileStat.st_mtime+7776000l<chktime ){
          up(error6);
          time_delay(500);
          dn(error6);
          exit(FALSE);
          }
    
    
    stat("infile2",&FileStat);
       if(FileStat.st_mtime!=copied){
          up(error7);
          time_delay(500);
          dn(error7);
          exit(FALSE);
          }
    where long int copied = 535523774;


    Infile2 would have to have a last modified or creation date of what the program you posted above returns. That makes perfect sense.

    As for infile1, it's modified date/time would be added to the value 7776000, and then be less than the current date/time. So say the date/time of infile1 was Thu Aug 07 11:34:04 2003, that converts to 5355237740. 5355237740 + 7776000 is 5363013740. That in calendar format is Wed Nov 05 10:34:04 2003. So is it safe to assume that infile1 has to be created more than about 3 months before the current system date/time?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That sounds right.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    7776000 / seconds_in_day
    = 7776000 / (60*60*24)
    = 90

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terrible confusion with time variables
    By LowlyIntern in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2008, 07:23 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. for loop confusion
    By Enges in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2006, 08:21 AM
  4. Server-net newbie confusion
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 04-28-2005, 02:08 AM
  5. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM