So I am having trouble implementing the mtime struct in C, where I am trying to check the last modification time of a file. When compiling, I receive this error:
pr8.1.c:246: error: incompatible types when assigning to type struct timespec from type time_t
make: *** [pr8] Error 1
The code I am using for this is as follows:
static struct timespec mtime(const char *file)
{
struct stat s;
struct timespec t = { 0, 0 };
if (stat(file, &s) == 0)
#if defined(MTIME) && MTIME == 1 // Linux
{ t = s.st_mtime; }
#elif defined(MTIME) && MTIME == 2 // Mac OS X
{ t = s.st_mtimespec; }
#elif defined(MTIME) && MTIME == 3 // Mac OS X, with some additional settings
{ t.tv_sec = s.st_mtime; t.tv_nsec = s.st_mtimensec; }
#else // Solaris
{ t.tv_sec = s.st_mtime; }
#endif
return t;
}
And the struct stat:
struct stat
{ time_t st_mtime; };
P.S. sorry about the format, I am not sure why the format is acting like this. Running this with Linux. Thanks in advance for the help.
In the linux and first mac os x version, you’re assigning to the structure from an int (time_t). In the other two versions, you are correctly assigning from a member of s to a member of t. If you change to this, do you get correct operation?