xref: /plan9/sys/src/9/boot/settime.c (revision be6499e306be769f28d0319bbc34fb75904562d7)
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include "../boot/boot.h"
6 
7 static long lusertime(char*);
8 
9 char *timeserver = "#s/boot";
10 
11 void
settime(int islocal,int afd,char * rp)12 settime(int islocal, int afd, char *rp)
13 {
14 	int n, f;
15 	Dir dir[2];
16 	char timebuf[64];
17 	static int timeset;
18 
19 	if(timeset)
20 		return;
21 	print("time...");
22 	if(islocal){
23 		/*
24 		 *  set the time from the real time clock
25 		 */
26 		f = open("#r/rtc", ORDWR);
27 		if(f >= 0){
28 			if((n = read(f, timebuf, sizeof(timebuf)-1)) > 0){
29 				timebuf[n] = '\0';
30 				timeset = 1;
31 			}
32 			close(f);
33 		}else do{
34 			strcpy(timebuf, "yymmddhhmm[ss]");
35 			outin("\ndate/time ", timebuf, sizeof(timebuf));
36 		}while((timeset=lusertime(timebuf)) <= 0);
37 	}
38 	if(timeset == 0){
39 		/*
40 		 *  set the time from the access time of the root
41 		 */
42 		f = open(timeserver, ORDWR);
43 		if(f < 0)
44 			return;
45 		if(mount(f, afd, "/tmp", MREPL, rp) < 0){
46 			warning("settime mount");
47 			close(f);
48 			return;
49 		}
50 		close(f);
51 		if(stat("/tmp", statbuf, sizeof statbuf) < 0)
52 			fatal("stat");
53 		convM2D(statbuf, sizeof statbuf, &dir[0], (char*)&dir[1]);
54 		sprint(timebuf, "%ld", dir[0].atime);
55 		unmount(0, "/tmp");
56 	}
57 
58 	f = open("#c/time", OWRITE);
59 	if(write(f, timebuf, strlen(timebuf)) < 0)
60 		warning("can't set #c/time");
61 	close(f);
62 	print("\n");
63 }
64 
65 #define SEC2MIN 60L
66 #define SEC2HOUR (60L*SEC2MIN)
67 #define SEC2DAY (24L*SEC2HOUR)
68 
69 int
g2(char ** pp)70 g2(char **pp)
71 {
72 	int v;
73 
74 	v = 10*((*pp)[0]-'0') + (*pp)[1]-'0';
75 	*pp += 2;
76 	return v;
77 }
78 
79 /*
80  *  days per month plus days/year
81  */
82 static	int	dmsize[] =
83 {
84 	365, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
85 };
86 static	int	ldmsize[] =
87 {
88 	366, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
89 };
90 
91 /*
92  *  return the days/month for the given year
93  */
94 static int *
yrsize(int y)95 yrsize(int y)
96 {
97 
98 	if((y%4) == 0 && ((y%100) != 0 || (y%400) == 0))
99 		return ldmsize;
100 	else
101 		return dmsize;
102 }
103 
104 /*
105  *  compute seconds since Jan 1 1970
106  */
107 static long
lusertime(char * argbuf)108 lusertime(char *argbuf)
109 {
110 	char *buf;
111 	ulong secs;
112 	int i, y, m;
113 	int *d2m;
114 
115 	buf = argbuf;
116 	i = strlen(buf);
117 	if(i != 10 && i != 12)
118 		return -1;
119 	secs = 0;
120 	y = g2(&buf);
121 	m = g2(&buf);
122 	if(y < 70)
123 		y += 2000;
124 	else
125 		y += 1900;
126 
127 	/*
128 	 *  seconds per year
129 	 */
130 	for(i = 1970; i < y; i++){
131 		d2m = yrsize(i);
132 		secs += d2m[0] * SEC2DAY;
133 	}
134 
135 	/*
136 	 *  seconds per month
137 	 */
138 	d2m = yrsize(y);
139 	for(i = 1; i < m; i++)
140 		secs += d2m[i] * SEC2DAY;
141 
142 	secs += (g2(&buf)-1) * SEC2DAY;
143 	secs += g2(&buf) * SEC2HOUR;
144 	secs += g2(&buf) * SEC2MIN;
145 	if(*buf)
146 		secs += g2(&buf);
147 
148 	sprint(argbuf, "%ld", secs);
149 	return secs;
150 }
151