futimens() can be used to change timestamps of or timestomp files using a file descriptor.
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
int main() {
int fd = open("example.txt", O_WRONLY);
if (fd == -1) {
perror("open");
return 1;
}
struct timespec times[2];
times[0].tv_sec = 1672531200; // Access time
times[0].tv_nsec = 0;
times[1].tv_sec = 1672534800; // Modification time
times[1].tv_nsec = 0;
if (futimens(fd, times) == -1) {
perror("futimens");
return 1;
}
close(fd);
printf("Timestamps updated using futimens().\n");
return 0;
}