summaryrefslogtreecommitdiffstats
path: root/usr.sbin/rdate
diff options
context:
space:
mode:
authorjakob <jakob@openbsd.org>2002-08-01 06:26:57 +0000
committerjakob <jakob@openbsd.org>2002-08-01 06:26:57 +0000
commit12c346a24b3dc95cdaccf94933b7cc474963249b (patch)
treea7368003ffb233950fc2f83ef57cfde7fd9ba19b /usr.sbin/rdate
parentu_short cleansing (diff)
downloadwireguard-openbsd-12c346a24b3dc95cdaccf94933b7cc474963249b.tar.xz
wireguard-openbsd-12c346a24b3dc95cdaccf94933b7cc474963249b.zip
knf and minor tweeking from thorsten
Diffstat (limited to 'usr.sbin/rdate')
-rw-r--r--usr.sbin/rdate/ntpleaps.c53
1 files changed, 32 insertions, 21 deletions
diff --git a/usr.sbin/rdate/ntpleaps.c b/usr.sbin/rdate/ntpleaps.c
index f697008994c..ef4c7eb04c3 100644
--- a/usr.sbin/rdate/ntpleaps.c
+++ b/usr.sbin/rdate/ntpleaps.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntpleaps.c,v 1.3 2002/07/31 12:48:46 jakob Exp $ */
+/* $OpenBSD: ntpleaps.c,v 1.4 2002/08/01 06:26:57 jakob Exp $ */
/*
* Copyright (c) 2002 by Thorsten "mirabile" Glaser <x86@ePOST.de>
@@ -21,35 +21,37 @@
/* Leap second support for NTP clients (generic) */
-static const char RCSId[] = "$OpenBSD: ntpleaps.c,v 1.3 2002/07/31 12:48:46 jakob Exp $";
+static const char RCSId[] = "$OpenBSD: ntpleaps.c,v 1.4 2002/08/01 06:26:57 jakob Exp $";
-/* I could include tzfile.h, but this would make the code unportable
+/*
+ * I could include tzfile.h, but this would make the code unportable
* at no real benefit. Read tzfile.h for why.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include "ntpleaps.h"
u_int64_t *leapsecs = NULL;
unsigned int leapsecs_num = 0;
-static int flaginit = 0;
+static int flaginit = -1;
static int flagwarn = 0;
int
ntpleaps_init(void)
{
- if (flaginit)
- return 0;
+ if (!flaginit)
+ return (0);
if (!ntpleaps_read()) {
- flaginit = 1;
- return(0);
+ flaginit = 0;
+ return (0);
}
/* This does not really hurt, but users will complain about
@@ -69,14 +71,14 @@ ntpleaps_sub(u_int64_t *t)
{
unsigned int i = 0;
u_int64_t u;
- int r=1;
+ int r = 1;
- if (ntpleaps_init() == -1)
- return(-1);
+ if ((flaginit ? ntpleaps_init() : 0) == -1)
+ return (-1);
u = *t;
- while (i<leapsecs_num) {
+ while (i < leapsecs_num) {
if (u < leapsecs[i])
break;
if (u == leapsecs[i++])
@@ -85,17 +87,27 @@ ntpleaps_sub(u_int64_t *t)
--r;
do_sub:
- *t=u-i;
+ *t = u - i;
return (r);
}
+u_int32_t
+read_be_dword(u_int8_t *ptr)
+{
+ u_int32_t res;
+
+ memcpy(&res, ptr, 4);
+ return (ntohl(res));
+}
+
+
int
ntpleaps_read(void)
{
int fd;
unsigned int r;
u_int8_t buf[32];
- u_int32_t m1,m2,m3;
+ u_int32_t m1, m2, m3;
u_int64_t s;
u_int64_t *l;
@@ -121,9 +133,9 @@ ntpleaps_read(void)
read(fd, buf, 28);
/* Read number of leap second entries */
- r = ntohl(*((u_int32_t *) (buf + 24)));
+ r = read_be_dword(&buf[24]);
/* Check for plausibility - arbitrary values */
- if (r < 20 || r > 60000) {
+ if ((r < 20) || (r > 60000)) {
close(fd);
return (-1);
}
@@ -134,17 +146,16 @@ ntpleaps_read(void)
/* Skip further uninteresting stuff */
read(fd, buf, 12);
- m1 = ntohl(*((u_int32_t *)(buf)));
- m2 = ntohl(*((u_int32_t *)(buf+4)));
- m3 = ntohl(*((u_int32_t *)(buf+8)));
+ m1 = read_be_dword(buf);
+ m2 = read_be_dword(&buf[4]);
+ m3 = read_be_dword(&buf[8]);
m3 += (m1 << 2)+m1+(m2 << 2)+(m2 << 1);
lseek(fd, (off_t)m3, SEEK_CUR);
/* Now go parse the tzfile leap second info */
for (m1 = 0; m1 < r; m1++) {
read(fd, buf, 8);
- m2 = ntohl(*((u_int32_t *)buf));
- s = SEC_TO_TAI64(m2);
+ s = SEC_TO_TAI64(read_be_dword(buf));
/*
* Assume just _one_ leap second on each entry, and compensate
* the lacking error checking by validating the first entry