From 6f412df28ec5a195c80ebd8e091c6b31be88bdcc Mon Sep 17 00:00:00 2001 From: John Vogel Date: Sun, 15 May 2016 00:25:04 -0400 Subject: Initial commit --- set_time.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 set_time.c (limited to 'set_time.c') diff --git a/set_time.c b/set_time.c new file mode 100644 index 0000000..9ed5398 --- /dev/null +++ b/set_time.c @@ -0,0 +1,59 @@ +/* set_time.c - save/restore system time based on mtime of current program file + * + * Based on fake-hwclock.c by Xan Manning from + * https://github.com/xanmanning/alarm-fake-hwclock/fake-hwclock.c + * + * Original license: + * ----------------------------------------------------------------------------- + * "THE (COFFEE)WARE LICENSE" (Rev. 1): + * + * + * wrote this file. As long as you retain this notice you can do whatever you + * want with this stuff. If we meet some day, and you think this stuff is + * worth it, you can buy me the above drink(s) in return. + * + * Xan Manning + * ----------------------------------------------------------------------------- + */ + +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + struct stat sb; + time_t current_time; + + current_time = time(NULL); + if (current_time == (time_t)-1) { + fprintf(stderr, "failed to get current time\n"); + return 1; + } + + if (stat(argv[0], &sb) == -1) { + fprintf(stderr, "stat failed: %s\n", argv[0]); + return 1; + } + + if (current_time > sb.st_mtime) { + struct utimbuf utb = { current_time, current_time }; + if (utime(argv[0], &utb) == -1) { + fprintf(stderr, "utime failed\n"); + return 1; + } + printf("Current time saved\n"); + } + else { + struct timespec ts = { current_time + 2, 0 }; + if (clock_settime(CLOCK_REALTIME, &ts) == -1) { + fprintf(stderr, "clock_settime failed\n"); + return 1; + } + printf("System time set\n"); + } + + return 0; +} -- cgit v1.2.3