Skip to content

Instantly share code, notes, and snippets.

@Rufflewind
Created October 15, 2018 08:50
Show Gist options
  • Save Rufflewind/fdd16dfbeccaf8b7373d4914ad3f6cf3 to your computer and use it in GitHub Desktop.
Save Rufflewind/fdd16dfbeccaf8b7373d4914ad3f6cf3 to your computer and use it in GitHub Desktop.
// stat.c
//
// TO RUN: cc -Wall stat.c && ./a.out
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
static const char *path = __FILE__;
static int f(struct timespec times[2])
{
struct stat st;
struct timespec initial[] = {
{11, 0},
{1, 0}
};
printf("%s {{%li, %li}, {%li, %li}}\n",
path,
times[0].tv_sec, times[0].tv_nsec,
times[1].tv_sec, times[1].tv_nsec);
if (utimensat(AT_FDCWD, path, initial, 0)) {
perror("utimensat1 failed");
return 1;
}
if (stat(path, &st)) {
perror("stat1 failed");
return 1;
}
printf(" atime = %li, mtime = %li\n",
st.st_atime, st.st_mtime);
if (utimensat(AT_FDCWD, path, times, 0)) {
perror("utimensat2 failed");
return 1;
}
if (stat(path, &st)) {
perror("stat2 failed");
return 1;
}
printf(" -> atime = %li, mtime = %li\n",
st.st_atime, st.st_mtime);
return 0;
}
int main(void)
{
return f((struct timespec [2]){{0, UTIME_OMIT}, {0, 0}})
|| f((struct timespec [2]){{12, 0}, {0, UTIME_OMIT}})
|| f((struct timespec [2]){{12, 0}, {0, -1}})
|| f((struct timespec [2]){{12, 0}, {0, -3}});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment