Reputation: 1480
I have written this code using futimens()
function:
int set_time(const char *name)
{
int dskr = open_file( name );
struct timespec now;
int retval = clock_gettime(CLOCK_REALTIME, &now);
printf("Clock_gettime returned %d \n", retval);
printf("%ld \n", now.tv_sec);
int status = futimens(dskr, &now);
handle_error("futimens", status);
close_file(dskr);
return 0;
}
It gives such output:
Clock_gettime returned 0
1619210402
futimens status: -1
futimens() error: Invalid argument
ow do I use futimens
function to change file timestamp to current time?
I am quite new in C so I am really struggling to find needed information to implement the solution.
P.S. handle_error()
function is just my function to catch exceptions.
Upvotes: 0
Views: 324
Reputation: 1452
futimens
requires an array of two struct timespec
. That's why you get an invalid argument error. The zeroth element is the access time, and the first is the modification time. So you could set the modification time like this:
struct timespec now[2];
now[0].tv_nsec = UTIME_OMIT;
int retval = clock_gettime(CLOCK_REALTIME, &now[1]);
int status = futimens(dskr, now);
Or, alternatively:
struct timespec now[2];
now[0].tv_nsec = UTIME_OMIT;
now[1].tv_nsec = UTIME_NOW;
int status = futimens(dskr, now);
See the man page.
Upvotes: 1