Problem with timestamp code under SunOS...

Posted on 2005-04-15
Last Modified: 2010-08-05

I'm using the following code to compare timestamps between two files.

  struct stat fbuf;
  int i,fres;
  long time1=0,time2=0;

  fres = stat ("file1.c",&fbuf);
  if (fres == 0) {
    if (debug) printf ("file1.c, size=%d, date=%s (%d)\n",fbuf.st_size,ctime(&fbuf.st_atime),fbuf.st_atime);
    time1 = fbuf.st_atime;
  fres = stat ("file2",&fbuf);
  if (fres == 0) {
    if (debug) printf ("file2, size=%d, date=%s (%d)\n",fbuf.st_size,ctime(&fbuf.st_atime),fbuf.st_atime);
    time2 = fbuf.st_atime;

File1.c is a C module which is compiled and made with other modules to create an executable, File2. The problem is that sometimes this code returns the correct timestamp on File1.c, sometimes not. Here's one example (this is running on SunOS, I have not see the problem occur under Linux):

Debug dump from program execution:
file1.c, size=10888, date=Wed Apr 13 17:02:36 2005
file2, size=7273948, date=Thu Apr 14 15:07:32 2005

Timestamps from SunOS:
> ls -alrt file*
-rwxr-xr-x   1 userj    pedft    7273948 Apr 14 15:07 file2
-rwxr-xr-x   1 uesrj    pedft      10888 Apr 14 15:25 file1.c


Question by:jimdgar
    1 Comment
    LVL 22

    Accepted Solution

    There are several different kinds of timestamps.  Depending on the OS, there may be time of file creation, time of last read, time of last write, or time of last open.    I suspect the ls command prints one of these, and you're printing a different one.  They'd be the same for a newly created file, so you'll get the same results some of the time, but not in general.

    Figure out which one you makes more sense for your needs and stick to that one.


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now