diff options
author | Anton Kling <anton@kling.gg> | 2024-12-16 22:48:41 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-12-16 22:58:32 +0100 |
commit | f2e31397b0e20e962eba1c2096fba73a0b210846 (patch) | |
tree | 1245f107f8a8e69bf3a99b1cc59c31645841de4b | |
parent | 9501c3f45b9b5d3b2cef5e78c83c362dbe6295a5 (diff) |
rdate: Fix error reporting and code formatting
-rw-r--r-- | userland/minibox/utilities/rdate.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/userland/minibox/utilities/rdate.c b/userland/minibox/utilities/rdate.c index 54a2746..bd03b0b 100644 --- a/userland/minibox/utilities/rdate.c +++ b/userland/minibox/utilities/rdate.c @@ -8,7 +8,7 @@ int rdate_main(int argc, char **argv) { int fd = socket(AF_INET, SOCK_STREAM, 0); - if (fd < 0) { + if (-1 == fd) { perror("socket"); return 1; } @@ -23,26 +23,37 @@ int rdate_main(int argc, char **argv) { hints.ai_next = NULL; int rc = getaddrinfo("time-d-g.nist.gov", "37", &hints, &result); - if (-1 == rc) { - fprintf(stderr, "Error in getaddrinfo()\n"); + if (0 != rc) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rc)); return 1; } - if (connect(fd, (struct sockaddr *)result->ai_addr, result->ai_addrlen) < 0) { + if (-1 == + connect(fd, (struct sockaddr *)result->ai_addr, result->ai_addrlen)) { perror("connect"); return 1; } uint32_t t; - read(fd, &t, sizeof(t)); - t = ntohl(t); + if (-1 == (rc = read(fd, &t, sizeof(t)))) { + perror("read"); + return 1; + } close(fd); + if (sizeof(t) != rc) { + fprintf(stderr, "Invalid message recieved from time server.\n"); + return 1; + } + + t = ntohl(t); + int64_t unix_time = (t - 2208988800) * 1000; int clock_fd = open("/dev/clock", O_RDWR); int64_t current; dprintf(clock_fd, "%lld", unix_time); close(clock_fd); + freeaddrinfo(result); return 0; } |