user1083952
user1083952

Reputation: 51

C stat struct does not have st_ctime field but only st_ctim

I've now been googleing this for about two hours, yet I was unable to find any answers that helped.

The definition of 'stat' as spcified in the manpage says that a st_ctime field exists.

       struct stat {
           dev_t     st_dev;     /* ID of device containing file */
           ino_t     st_ino;     /* inode number */
           mode_t    st_mode;    /* protection */
           nlink_t   st_nlink;   /* number of hard links */
           uid_t     st_uid;     /* user ID of owner */
           gid_t     st_gid;     /* group ID of owner */
           dev_t     st_rdev;    /* device ID (if special file) */
           off_t     st_size;    /* total size, in bytes */
           blksize_t st_blksize; /* blocksize for file system I/O */
           blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
           time_t    st_atime;   /* time of last access */
           time_t    st_mtime;   /* time of last modification */
           time_t    st_ctime;   /* time of last status change */
       };

However, this does not seem to be true for my system even though I'm using gcc (which should be behaving according to the standard).

In fact, all of the time fields (atime, mtime, ctime) are missing and therefore the struct contains some atim, mtim and ctim values which return a timespec instead of the desired time_t value.

Now my questions:

  1. Why is this so? Maybe I included the wrong header but I'm really sure it's gotta be sys/stat.h.
  2. I have not been finding too much information about timespec, what is it and why is it returned here?
  3. Even if I find a workaround, does it help m or will any other system fail to execute my code?

I am using Ubuntu 11.10 and gcc 4.6.1.

My code (partly):

struct stat file_info;
    time_t t;

    if( lstat( path, &file_info ) == 0 ) {

        struct tm* timeinfo;
        t = file_info.st_ctime;
        timeinfo = localtime( &t );

I'd be really glad if you could help with this one, I really got no clue why I can not compile using the st_ctime field of my struct and as usual gcc is not too much of a help when it comes to talking about errors ;-)

Probably it's got to do something with #include problems, but I'm not able to determine what.

Upvotes: 5

Views: 4051

Answers (2)

Doug Currie
Doug Currie

Reputation: 41170

POSIX 2008 requires that stat() return struct timespec, so that the fractional portion of a timestamp is available for refined precision of timestamps -- one second resolution is not sufficient for file times.

ADDED:

From man page

Since kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields. Glibc exposes the nanosecond component of each field using names of the form st_atim.tv_nsec if the _BSD_SOURCE or _SVID_SOURCE feature test macro is defined. These fields are specified in POSIX.1-2008, and, starting with version 2.12, glibc also exposes these field names if _POSIX_C_SOURCE is defined with the value 200809L or greater, or _XOPEN_SOURCE is defined with the value 700 or greater. If none of the aforementioned macros are defined, then the nanosecond values are exposed with names of the form st_atimensec. On file systems that do not support subsecond timestamps, the nanosecond fields are returned with the value 0.

Upvotes: 2

pmg
pmg

Reputation: 108968

My documentation (man lstat) specifies the headers to include and the feature test macro requirements to define before the headers

#define _POSIX_C_SOURCE 200112L
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/* use stat, fstat, or lstat */

Upvotes: 0

Related Questions