Mercurial > hg > toybox
annotate toys/pending/hwclock.c @ 1639:856b544f8fce draft
strncpy(optptr, hname, strlen(hname)) is really just strcpy().
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 01 Jan 2015 16:49:55 -0600 |
parents | f42dceed5071 |
children | 7e3372a47248 |
rev | line source |
---|---|
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
1 /* hwclock.c - get and set the hwclock |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
2 * |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
3 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com> |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
4 * |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
5 * No Standard. |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
6 * |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
7 USE_HWCLOCK(NEWTOY(hwclock, ">0(fast)f(rtc):u(utc)l(localtime)t(systz)w(systohc)s(hctosys)r(show)[!ul][!rsw]", TOYFLAG_USR|TOYFLAG_BIN)) |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
8 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
9 config HWCLOCK |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
10 bool "hwclock" |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
11 default n |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
12 help |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
13 usage: hwclock [-rswtluf] |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
14 |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
15 -f FILE Use specified device file instead of /dev/rtc (--show) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
16 -l Hardware clock uses localtime (--localtime) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
17 -r Show hardware clock time (--show) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
18 -s Set system time from hardware clock (--hctosys) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
19 -t Set the system time based on the current timezone (--systz) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
20 -u Hardware clock uses UTC (--utc) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
21 -w Set hardware clock from system time (--systohc) |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
22 */ |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
23 #define FOR_hwclock |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
24 #include "toys.h" |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
25 #include <linux/rtc.h> |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
26 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
27 GLOBALS( |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
28 char *fname; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
29 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
30 int utc; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
31 ) |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
32 |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
33 static int rtc_open(int flag) |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
34 { |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
35 if (!TT.fname) { |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
36 int fd; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
37 |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
38 if ((fd = open((TT.fname = "/dev/rtc"), flag)) != -1) return fd; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
39 else if ((fd = open((TT.fname = "/dev/rtc0"), flag)) != -1) return fd; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
40 else TT.fname = "/dev/misc/rtc"; |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
41 } |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
42 return xopen(TT.fname, flag); |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
43 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
44 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
45 static time_t get_rtc() |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
46 { |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
47 struct tm time; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
48 time_t tm; |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
49 char *ptz_old = 0; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
50 int fd = rtc_open(O_RDONLY); |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
51 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
52 xioctl(fd, RTC_RD_TIME, &time); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
53 close(fd); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
54 if (TT.utc) { |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
55 ptz_old = getenv("TZ"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
56 if (putenv((char*)"TZ=UTC0")) perror_exit("putenv"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
57 tzset(); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
58 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
59 if ((tm = mktime(&time)) < 0) error_exit("mktime failed"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
60 if (TT.utc) { |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
61 if (unsetenv("TZ") < 0) perror_exit("unsetenv"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
62 if (ptz_old && putenv(ptz_old - 3)) perror_exit("putenv"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
63 tzset(); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
64 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
65 return tm; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
66 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
67 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
68 static void set_sysclock_from_hwclock() |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
69 { |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
70 struct timezone tmzone; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
71 struct timeval tmval; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
72 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
73 tmzone.tz_minuteswest = timezone / 60 - 60 * daylight; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
74 tmzone.tz_dsttime = 0; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
75 tmval.tv_sec = get_rtc(); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
76 tmval.tv_usec = 0; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
77 if (settimeofday(&tmval, &tmzone) < 0) perror_exit("settimeofday"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
78 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
79 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
80 static void set_hwclock_from_sysclock() |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
81 { |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
82 struct timeval tmval; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
83 struct tm time; |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
84 int fd = rtc_open(O_WRONLY); |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
85 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
86 if (gettimeofday(&tmval, NULL) < 0) perror_exit("gettimeofday"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
87 // converting a time value to broken-down UTC time |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
88 if (TT.utc && !gmtime_r((time_t*)&tmval.tv_sec, &time)) |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
89 error_exit("gmtime_r failed"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
90 // converting a time value to a broken-down localtime |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
91 else if (!(localtime_r((time_t*)&tmval.tv_sec, &time))) |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
92 error_exit("localtime_r failed"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
93 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
94 /* The value of tm_isdst will positive if daylight saving time is in effect, |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
95 * zero if it is not and negative if the information is not available. |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
96 * */ |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
97 time.tm_isdst = 0; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
98 xioctl(fd, RTC_SET_TIME, &time); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
99 close(fd); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
100 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
101 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
102 static void set_sysclock_timezone() |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
103 { |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
104 struct timezone tmzone; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
105 struct timeval tmval; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
106 struct tm *pb; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
107 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
108 if (gettimeofday(&tmval, NULL) < 0) perror_exit("gettimeofday"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
109 if (!(pb = localtime(&tmval.tv_sec))) error_exit("localtime failed"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
110 // extern long timezone => defined in header sys/time.h |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
111 tmzone.tz_minuteswest = timezone / 60; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
112 if (pb->tm_isdst) tmzone.tz_minuteswest -= 60; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
113 tmzone.tz_dsttime = 0; // daylight saving time is not in effect |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
114 if (gettimeofday(&tmval, NULL) < 0) perror_exit("gettimeofday"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
115 if (!TT.utc) tmval.tv_sec += tmzone.tz_minuteswest * 60; |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
116 if (settimeofday(&tmval, &tmzone) < 0) perror_exit("settimeofday"); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
117 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
118 |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
119 void hwclock_main() |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
120 { |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
121 // check for UTC |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
122 if (!(toys.optflags & FLAG_u)) { |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
123 FILE *fp = fopen("/etc/adjtime", "r"); |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
124 |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
125 if (fp) { |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
126 char *line = NULL; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
127 size_t st; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
128 |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
129 while (0 < getline(&line, &st, fp)) { |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
130 if (!strncmp(line, "UTC", 3)) { |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
131 TT.utc = 1; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
132 break; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
133 } |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
134 free(line); |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
135 } |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
136 fclose(fp); |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
137 } |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
138 } else TT.utc = 1; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
139 |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
140 if (toys.optflags & FLAG_w) set_hwclock_from_sysclock(); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
141 else if (toys.optflags & FLAG_s) set_sysclock_from_hwclock(); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
142 else if (toys.optflags & FLAG_t) set_sysclock_timezone(); |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
143 else if ((toys.optflags & FLAG_r) || (toys.optflags & FLAG_l) |
1549
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
144 || !*toys.optargs) |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
145 { |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
146 time_t tm = get_rtc(); |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
147 char *s, *pctm = ctime(&tm); |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
148 |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
149 // ctime() is defined as equivalent to asctime(localtime(t)), |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
150 // which is defined to overflow its buffer rather than return NULL. |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
151 // if (!pctm) error_exit("can't happen"); |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
152 if ((s = strrchr(pctm, '\n'))) *s = '\0'; |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
153 // TODO: implement this. |
f42dceed5071
First cleanup pass on hwclock.
Rob Landley <rob@landley.net>
parents:
1548
diff
changeset
|
154 xprintf("%s 0.000000 seconds\n", pctm); |
1548
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
155 } |
e3216b4e6a0f
hwclock: get and set the hwclock
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
156 } |