123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include "libbb.h"
- enum { RFC_868_BIAS = 2208988800UL };
- static void socket_timeout(int sig UNUSED_PARAM)
- {
- bb_error_msg_and_die("timeout connecting to time server");
- }
- static time_t askremotedate(const char *host)
- {
- uint32_t nett;
- int fd;
-
- alarm(10);
- signal(SIGALRM, socket_timeout);
- fd = create_and_connect_stream_or_die(host, bb_lookup_port("time", "tcp", 37));
- if (safe_read(fd, &nett, 4) != 4)
- bb_error_msg_and_die("%s: %s", host, "short read");
- if (ENABLE_FEATURE_CLEAN_UP)
- close(fd);
-
- nett = ntohl(nett) - RFC_868_BIAS;
- if (sizeof(time_t) > 4) {
-
- time_t cur = time(NULL);
- int32_t adjust = (int32_t)(nett - (uint32_t)cur);
- return cur + adjust;
- }
-
- return (time_t)nett;
- }
- int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int rdate_main(int argc UNUSED_PARAM, char **argv)
- {
- time_t remote_time;
- unsigned flags;
- flags = getopt32(argv, "^" "sp" "\0" "-1");
- remote_time = askremotedate(argv[optind]);
-
- if (!(flags & 2)) {
- if (time(NULL) == remote_time)
- bb_error_msg("current time matches remote time");
- else
- if (stime(&remote_time) < 0)
- bb_perror_msg_and_die("can't set time of day");
- }
- if (flags != 1)
- printf("%s", ctime(&remote_time));
- return EXIT_SUCCESS;
- }
|