|
|
1.1 root 1: #include <fio.h>
2: #include <time.h>
3: #include <sys/types.h>
4: #include <sys/clock.h>
5: #include <sys/timeb.h>
6: #include <signal.h>
7: #include <libc.h>
8:
9: #define NULL 0
10:
11: char *clocks[5] = {
12: "dk!nj/astro/clock",
13: "dk!nj/mhe/mhpbs.clock",
14: NULL
15: };
16:
17: char telno[] = "913034944774", class[] = "1200";
18:
19: #define UNIXEPOCH (24*60*60*40587L) /* MJD of 1 Jan 1970 */
20:
21: /* buffer to read time into and print it from */
22: #define ARB 50
23: char buf[ARB];
24:
25: typedef unsigned long tod_t;
26: tod_t utovax();
27:
28: time_t readnet(), phonewwv();
29: long labs();
30: int sflag, vflag, pflag;
31:
32: main (argc, argv)
33: int argc;
34: char **argv;
35: {
36: time_t wwvtime;
37: struct timeb ourtime;
38: int c;
39: extern char *optarg;
40:
41: while ((c = getopt (argc, argv, "vspc:")) >= 0) {
42: switch (c) {
43: case 's':
44: sflag++;
45: break;
46: case 'v':
47: vflag++;
48: break;
49: case 'p':
50: pflag++;
51: break;
52: case 'c':
53: clocks[0] = optarg;
54: clocks[1] = NULL;
55: break;
56:
57: case '?':
58: exit(1);
59: }
60: }
61: if (!sflag)
62: vflag++;
63: if ((wwvtime = pflag ? phonewwv() : readnet()) == 0)
64: exit(1);
65: ftime(&ourtime);
66: if (sflag)
67: settime(ourtime, wwvtime);
68: if (vflag)
69: prdiff(ourtime, wwvtime);
70: exit(0);
71: }
72:
73: /*
74: * try biasclock (new); if can't, use settod (old) instead
75: */
76: settime(ourtime, wwvtime)
77: struct timeb ourtime;
78: time_t wwvtime;
79: {
80: tod_t oldtod, newtod;
81: long delta;
82:
83: delta = wwvtime - ourtime.time;
84: if (labs(delta) > 100L*3600L) { /* time too far off, give up */
85: print("settod: diff %d seconds, too big\n", delta);
86: return;
87: }
88: delta = delta * 1000L - ourtime.millitm;
89: if (delta == 0L) /* well ... */
90: return;
91: if (biasclock(delta) >= 0)
92: return;
93: print("settod: could not do biasclock\n");
94: #if NOTDEF
95: /*
96: * should probably check errno here
97: * someday the rest of this code will vanish
98: */
99: newtod = utovax(wwvtime);
100: if ((oldtod = settod(0L)) == -1) {
101: print("settod: can't read TOY\n");
102: return;
103: }
104: delta = newtod - oldtod;
105: /* If delta is too great, maybe TOY year is wrong */
106: if (labs(delta) > 100L*86400L*10) {
107: time_t tt;
108: time(&tt);
109: stime(&tt);
110: oldtod = settod(0L);
111: delta = newtod - oldtod;
112: }
113: if (labs(delta) > 100L*86400L*10)
114: print("settod: delta %d too big: no settod\n", delta);
115: else if (settod(newtod) == -1)
116: print("settod: could not do settod\n");
117: #endif
118: }
119:
120: prdiff(ourtime, wwvtime)
121: struct timeb ourtime;
122: time_t wwvtime;
123: {
124: float delta;
125: char *ctime();
126:
127: print("%.20s ", ctime(&wwvtime)+4);
128: delta = wwvtime - ourtime.time;
129: delta -= (float)ourtime.millitm/1000;
130: if (delta == 0)
131: print("exact\n");
132: else if (delta > 0)
133: print("advance %.2f\n", delta);
134: else
135: print("retard %.2f\n", -delta);
136: }
137:
138: /*
139: * turn a unix time to a VAX TODR time
140: */
141:
142: tod_t
143: utovax(t)
144: time_t t;
145: {
146: register int i;
147:
148: for (i=1970; ; i++) {
149: long secyr;
150: secyr = SECYR;
151: if (LEAPYEAR(i))
152: secyr += SECDAY;
153: if (t < secyr)
154: break;
155: t -= secyr;
156: }
157: return(t * 100 + TODRZERO);
158: }
159:
160: time_t
161: readnet()
162: {
163: register int i;
164: int f;
165: int alcatch();
166:
167: signal(SIGALRM, alcatch);
168: alarm(30);
169: for (i=0; clocks[i]; i++) {
170: if ((f = ipcopen(ipcpath(clocks[i], "dk", ""), "light")) >= 0)
171: break;
172: }
173: if (f < 0) {
174: alarm(0);
175: print("settod: can't open clock\n");
176: return(0);
177: }
178: alarm(15);
179: do {
180: if (read(f, buf, 1) <= 0) {
181: print("settod: read error\n");
182: alarm(0);
183: close(f);
184: return (0);
185: }
186: buf[0] &= 0177;
187: } while (buf[0]!='\r' && buf[0]!='\n');
188: for (i=0; i < sizeof(buf); i++) {
189: if (read(f, &buf[i], 1) <= 0) {
190: print("settod: read error\n");
191: alarm(0);
192: close(f);
193: return (0);
194: }
195: buf[i] &= 0177;
196: if (buf[i] == '\n' || buf[i] == '\r') {
197: if (i)
198: break;
199: i--; /* ignore empty line */
200: }
201: }
202: alarm(0);
203: close(f);
204: if (i < 10 || i == sizeof(buf)) { /* plausibility check */
205: print("settod: clock format error: %s\n", buf);
206: return (0);
207: }
208: buf[i] = 0; /* just to make sure */
209: return (atol(buf));
210: }
211:
212: time_t
213: phonewwv()
214: {
215: register fd;
216: register char *l;
217: int alcatch();
218:
219: signal(SIGALRM, alcatch);
220: alarm(60);
221: fd = dialout(telno, class);
222: if (fd < 0) {
223: print("settod: dialout(\"%s\", \"%s\") returned %d\n",
224: telno, class, fd);
225: return 0;
226: }
227: Finit(fd, (char *)0);
228: while (l = Frdline(fd)) {
229: if (vflag > 1)
230: print("%s\n", l);
231: if (FIOLINELEN(fd) == 51 && strcmp(&l[38], " UTC(NIST) *\r") == 0)
232: break;
233: }
234: if (l == 0) {
235: print("settod: wwv read error\n");
236: return 0;
237: }
238: alarm(0);
239: return atol(&l[21]) +
240: 60*(atol(&l[18]) +
241: 60*(atol(&l[15]) +
242: 24*atol(&l[0])))-UNIXEPOCH;
243: }
244:
245: alcatch() {}
246:
247: long
248: labs(n)
249: long n;
250: {
251: if (n >= 0)
252: return n;
253: return -n;
254: }
255:
256: #if NOTDEF
257:
258: /*
259: * old heathkit conversion code
260: * not guaranteed even to compile
261: *
262: * heathkit dates look like
263: * HH:MM:SS.S DD/MM/YY
264: * with several irritating quirks
265: */
266:
267: static char dmsize[12] =
268: {
269: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
270: };
271:
272: tod_t
273: hconv()
274: {
275: int year, mon;
276: int day, hour, mins, secs, tenth;
277: tod_t timerr;
278:
279: /* accept '?' for tenths */
280: if (buf[9]=='?')
281: buf[9] = '0';
282: if (sscanf(buf, "%d:%d:%d.%d %d/%d/%d\r", &hour, &mins, &secs,
283: &tenth, &mon, &day, &year) != 7)
284: goto formerr;
285: if( mon<1 || mon>12 ||
286: day<0 || day>31 || /* June 30, 1984 == July 0, 1984 !!?? */
287: mins<0 || mins>59 ||
288: secs<0 || secs>59 ||
289: tenth<0 || tenth>9 ||
290: hour<0 || hour>23)
291: goto formerr;
292: /* leap year */
293: timbuf = 0;
294: if (year % 4 == 0 && mon >= 3)
295: timbuf++;
296: while(--mon)
297: timbuf += dmsize[mon-1];
298: timbuf += day-1;
299: timbuf = 24*timbuf + hour;
300: timbuf = 60*timbuf + mins;
301: timbuf = 60*timbuf + secs;
302: timbuf = 10*timbuf + tenth;
303: timbuf = timbuf * 10 + TODRZERO;
304: return timbuf;
305: formerr:
306: print("settod: clock format error: %.24s\n", buf);
307: return(0);
308: }
309:
310: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.