|
|
1.1 root 1: #include <stdio.h>
2: /*
3: * get date from wwv network clock
4: */
5: #include <time.h>
6: #include <sys/types.h>
7: #include <sys/timeb.h>
8: #include <utmp.h>
9: #include <signal.h>
10:
11: int uflag;
12: int sflag;
13: int bflag;
14: int fflag;
15: char *timezone();
16: static int dmsize[12] =
17: {
18: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
19: };
20:
21: char clock[] = "mh/astro/clock";
22:
23: struct utmp wtmp[2] = { {"|", "", 0}, {"{", "", 0}};
24:
25: char *ctime();
26: char *asctime();
27: struct tm *localtime();
28: struct tm *gmtime();
29:
30: main(argc, argv)
31: char *argv[];
32: {
33: int wf, rc;
34: time_t wwvtime, readnet(), labs();
35:
36: rc = 0;
37: for(;;) {
38: switch(getopt(argc, argv, "usbf")) {
39:
40: case 's':
41: sflag++;
42: continue;
43:
44: case 'u':
45: uflag++;
46: continue;
47:
48: case 'b':
49: bflag++;
50: continue;
51:
52: case 'f':
53: fflag++;
54: continue;
55:
56: case '?':
57: exit(1);
58:
59: case EOF:
60: goto OK;
61: }
62: }
63: OK:
64: wwvtime = readnet();
65: if (wwvtime==0)
66: exit(1);
67: if (sflag) {
68: time_t nowtime = time((time_t)0);
69: if (fflag || labs(nowtime-wwvtime) < 20*60) {
70: wtmp[0].ut_time = nowtime;
71: wtmp[1].ut_time = wwvtime;
72: if(stime(&wwvtime) < 0) {
73: rc++;
74: printf("wwv: no permission\n");
75: } else {
76: if (wwvtime >= nowtime)
77: printf("advanced %ld sec\n",
78: wwvtime-nowtime);
79: else
80: printf("retarded %ld sec\n",
81: nowtime-wwvtime);
82: if ((wf = open("/usr/adm/wtmp", 1)) >= 0) {
83: time(&wtmp[1].ut_time);
84: lseek(wf, 0L, 2);
85: write(wf, (char *)wtmp, sizeof(wtmp));
86: close(wf);
87: }
88: }
89: } else {
90: printf("wwv: >20min difference; force with wwv -sf\n");
91: rc = 1;
92: bflag++;
93: }
94: }
95: if (bflag) {
96: printf("WWV: "); prt(wwvtime);
97: printf("you: "); prt(time((time_t)0));
98: } else
99: prt(wwvtime);
100: exit(rc);
101: }
102:
103: prt(t)
104: time_t t;
105: {
106: struct timeb info;
107: char *ap, *tzn;
108:
109: if (uflag) {
110: ap = asctime(gmtime(&t));
111: tzn = "GMT";
112: } else {
113: struct tm *tp;
114: ftime(&info.time);
115: tp = localtime(&t);
116: ap = asctime(tp);
117: tzn = timezone(info.timezone, tp->tm_isdst);
118: }
119: printf("%.20s", ap);
120: if (tzn)
121: printf("%s", tzn);
122: printf("%s", ap+19);
123: }
124:
125: time_t
126: readnet()
127: {
128: int i, j, year, mon;
129: int day, hour, mins, secs, tenth;
130: register struct tm *L;
131: char buf[24]; /* HH:MM:SS.S DD/MM/YY */
132: int f;
133: int alcatch();
134: time_t nowtime, timbuf;
135: extern errno, sys_nerr;
136: extern char *sys_errlist[];
137: extern dkp_ld;
138:
139: signal(SIGALRM, alcatch);
140: alarm(15);
141: f = tdkdial(clock, 2);
142: if (f < 0) {
143: alarm(0);
144: printf("wwv: can't open clock\n");
145: return(0);
146: }
147: if (dkproto(f, dkp_ld) < 0) {
148: alarm(0);
149: printf("wwv: can't turn on DK proto\n");
150: return(0);
151: }
152: do {
153: if ((j = read(f, buf, 1)) <= 0)
154: goto readerr;
155: } while ((buf[0]&0177) != '\r');
156: for (i=0; i<24; i += j) {
157: if ((j = read(f, buf+i, 24-i)) <= 0)
158: goto readerr;
159: }
160: alarm(0);
161: for (i=0; i<24; i++)
162: buf[i] &= 0177;
163: /* accept '?' for tenths */
164: if (buf[9]=='?')
165: buf[9] = '0';
166: if (sscanf(buf, "%d:%d:%d.%d %d/%d/%d\r", &hour, &mins, &secs,
167: &tenth, &mon, &day, &year) != 7)
168: goto formerr;
169: if (year!= 84)
170: goto formerr;
171: if( mon<1 || mon>12 ||
172: day<0 || day>31 || /* June 30, 1984 == July 0, 1984 ??!!?? */
173: mins<0 || mins>59 ||
174: secs<0 || secs>59 ||
175: hour<0 || hour>23)
176: goto formerr;
177: time(&nowtime);
178: L = gmtime(&nowtime);
179: timbuf = 0;
180: for(i=70; i<L->tm_year; i++)
181: timbuf += dysize(i);
182: /* always leap year */
183: if (mon >= 3)
184: timbuf++;
185: while(--mon)
186: timbuf += dmsize[mon-1];
187: timbuf += day-1;
188: timbuf = 24*timbuf + hour;
189: timbuf = 60*timbuf + mins;
190: timbuf = 60*timbuf + secs;
191: return(timbuf);
192: formerr:
193: printf("wwv: clock format error: %.24s\n", buf);
194: return(0);
195: readerr:
196: alarm(0);
197: printf("wwv: clock read ");
198: if (j < 0) {
199: if (errno < sys_nerr)
200: printf("error: %s\n", sys_errlist[errno]);
201: else
202: printf("error: %d\n", errno);
203: } else
204: printf("EOF\n");
205: return(0);
206: }
207:
208: alcatch()
209: {
210: return;
211: }
212:
213: time_t
214: labs(t)
215: {
216: if (t < 0)
217: return(-t);
218: return(t);
219: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.