Comparing two variables time_t generates a compiler warning

I know that this should be a simple warning, but I cannot solve it.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FILE_PATH "/sdcard/ex_file.txt"
static time_t time_now;

int main()
{
    struct stat f_stat;
    stat(FILE_PATH, &file_stat);
    time_now = time(&time_now);
    if (file_stat.st_mtime > time_now)
        unlink(FILE_PATH)
}

In this simple program, I get a warning in if (file_stat.st_mtime > time_now)). I am compiling the source code in an Android environment.

What could be the problem? Here are both variables time_t.

warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
+4
source share
1 answer

tl; dr : this will depend on the platform, but is struct statnot always (always) determined by time_t, so you will come across this warning on any platform, where time_tand unsigned longis not the same thing.

time_t linux - typedef __kernel_time_t, typedef - , .

struct stat does time_t, , Ubuntu 12.04 (x64), , stat.h :

struct stat {
    /** .............. **/
    unsigned long   st_atime;
    unsigned long   st_atime_nsec;
    unsigned long   st_mtime;
    unsigned long   st_mtime_nsec;
    unsigned long   st_ctime;
    unsigned long   st_ctime_nsec;
};

, unsigned long, time_t. time_t, __kernel_time_t, :

typedef __kernel_long_t       __kernel_time_t;

, ( FILE ), , , time_t unsigned long .

shree.pat18, , android stat.h unsigned long: http://www.netmite.com/android/mydroid/bionic/libc/include/sys/stat.h

types.h "time_t" "__kernel_time_t" http://www.netmite.com/android/mydroid/bionic/libc/include/sys/types.h

"__kernel_time_t" "long" ( "unsigned long", ): http://www.netmite.com/android/mydroid/kernel/include/asm-x86/posix_types_64.h

+4

Source: https://habr.com/ru/post/1544260/


All Articles