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