blah blah blah
1.10.2007
88 Lines About 44 Women
(amaranta here's a kiss, I chose you to end this list, the unlucky márquez daughter, the nails lyrics #44, 88 line UNIX/C source code to set the local time based on a network filer)
// Copyright 2007, Josh MacDonald
//
// I run Gentoo on a small computer with a bad board. I had run
// Windows on it "successfully", but the electronics were too noisy
// for audio, so I could run Kazaa but couldn't play the music. It
// wasn't until installing Linux that I found it had bad RAM. I
// replaced the memory, and now it's working with what you might call a
// "variable speed" clock. The clock skews several seconds in
// several days.
//
// Laziness made me do it.
#include <sys/time.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int change_time(double arg) {
struct timeval tv;
struct timezone tz;
if (gettimeofday(&tv, &tz)) {
perror("gettimeofday");
return -1;
}
// in version 2, finer resolution!
tv.tv_sec += (int)arg;
printf("setting time %+f\n", arg);
if (settimeofday(&tv, &tz)) {
perror("settimeofday");
return -2;
}
return 0;
}
int how_far_off(char *dir, double *offset) {
struct timezone tz;
struct timeval tv;
struct stat buf;
char *fn = (char*)malloc(1024);
strcpy(fn, dir);
strcat(fn, "/time-stamp");
unlink(fn);
FILE *f = fopen(fn, "w");
fclose(f);
if (stat(fn, &buf)) {
perror("stat");
return -1;
}
double ft = buf.st_mtime /* version 2: buf.st_mtim.tv_nsec * 10e-9 */;
if (gettimeofday(&tv, &tz)) {
perror("gettimeofday");
return -1;
}
double tt = tv.tv_sec /* version 2: tv.tv_usec * 10e-6 */;
*offset = (ft - tt);
printf("difference is %.05f secs\n", *offset);
return 0;
}
int main(int argc, char **argv) {
double arg;
int ret;
if (argc != 2) {
printf("usage: program directory\n");
return 2;
}
if ((ret = how_far_off(argv[1], &arg)) ||
(arg == 0) ||
(ret = change_time(arg))) {
return -ret;
}
return 0;
}
0 Comments:
Post a Comment
<< Home