diff options
Diffstat (limited to 'util-linux')
| -rw-r--r-- | util-linux/rdate.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/util-linux/rdate.c b/util-linux/rdate.c index 8dd784d3d..a62591914 100644 --- a/util-linux/rdate.c +++ b/util-linux/rdate.c | |||
| @@ -21,11 +21,11 @@ | |||
| 21 | //kbuild:lib-$(CONFIG_RDATE) += rdate.o | 21 | //kbuild:lib-$(CONFIG_RDATE) += rdate.o |
| 22 | 22 | ||
| 23 | //usage:#define rdate_trivial_usage | 23 | //usage:#define rdate_trivial_usage |
| 24 | //usage: "[-sp] HOST" | 24 | //usage: "[-s/-p] HOST" |
| 25 | //usage:#define rdate_full_usage "\n\n" | 25 | //usage:#define rdate_full_usage "\n\n" |
| 26 | //usage: "Get and possibly set system time from a remote HOST\n" | 26 | //usage: "Set and print time from HOST using RFC 868\n" |
| 27 | //usage: "\n -s Set system time (default)" | 27 | //usage: "\n -s Only set system time" |
| 28 | //usage: "\n -p Print time" | 28 | //usage: "\n -p Only print time" |
| 29 | 29 | ||
| 30 | #include "libbb.h" | 30 | #include "libbb.h" |
| 31 | 31 | ||
| @@ -58,8 +58,22 @@ static time_t askremotedate(const char *host) | |||
| 58 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT | 58 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
| 59 | * Subtract the RFC 868 time to get Linux epoch. | 59 | * Subtract the RFC 868 time to get Linux epoch. |
| 60 | */ | 60 | */ |
| 61 | 61 | nett = ntohl(nett) - RFC_868_BIAS; | |
| 62 | return ntohl(nett) - RFC_868_BIAS; | 62 | |
| 63 | if (sizeof(time_t) > 4) { | ||
| 64 | /* Now we have 32-bit lsb of a wider time_t | ||
| 65 | * Imagine that nett = 0x00000001, | ||
| 66 | * current time cur = 0x123ffffffff. | ||
| 67 | * Assuming our time is not some 40 years off, | ||
| 68 | * remote time must be 0x12400000001. | ||
| 69 | * Need to adjust out time by (int32_t)(nett - cur). | ||
| 70 | */ | ||
| 71 | time_t cur = time(NULL); | ||
| 72 | int32_t adjust = (int32_t)(nett - (uint32_t)cur); | ||
| 73 | return cur + adjust; | ||
| 74 | } | ||
| 75 | /* This is not going to work, but what can we do */ | ||
| 76 | return (time_t)nett; | ||
| 63 | } | 77 | } |
| 64 | 78 | ||
| 65 | int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 79 | int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| @@ -73,6 +87,13 @@ int rdate_main(int argc UNUSED_PARAM, char **argv) | |||
| 73 | 87 | ||
| 74 | remote_time = askremotedate(argv[optind]); | 88 | remote_time = askremotedate(argv[optind]); |
| 75 | 89 | ||
| 90 | /* Manpages of various Unixes are confusing. What happens is: | ||
| 91 | * (no opts) set and print time | ||
| 92 | * -s: set time ("do not print the time") | ||
| 93 | * -p: print time ("do not set, just print the remote time") | ||
| 94 | * -sp: print time (that's what we do, not sure this is right) | ||
| 95 | */ | ||
| 96 | |||
| 76 | if (!(flags & 2)) { /* no -p (-s may be present) */ | 97 | if (!(flags & 2)) { /* no -p (-s may be present) */ |
| 77 | time_t current_time; | 98 | time_t current_time; |
| 78 | 99 | ||
