summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorNatanael Copa <natanael.copa@gmail.com>2008-10-18 11:52:54 +0200
committerNatanael Copa <natanael.copa@gmail.com>2008-10-18 11:52:54 +0200
commit88d712478a82cad28ddaefb164d5d2df832bb98b (patch)
treeb351e355064dbaef8087e86db5afa63dde42caaa /util.c
downloadsntpc-88d712478a82cad28ddaefb164d5d2df832bb98b.tar.bz2
sntpc-88d712478a82cad28ddaefb164d5d2df832bb98b.tar.xz
initial commit
Diffstat (limited to 'util.c')
-rw-r--r--util.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..1f30abc
--- /dev/null
+++ b/util.c
@@ -0,0 +1,100 @@
+/* $OpenBSD: util.c,v 1.13 2007/03/27 18:22:02 otto Exp $ */
+
+/*
+ * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <arpa/inet.h>
+#include <sys/time.h>
+
+#include <limits.h>
+#include <stdlib.h>
+
+#include "log.h"
+#include "ntp.h"
+#include "util.h"
+
+
+double
+gettime(void)
+{
+ struct timeval tv;
+
+ if (gettimeofday(&tv, NULL) == -1)
+ fatal("gettimeofday");
+
+ return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
+}
+
+
+void
+d_to_tv(double d, struct timeval *tv)
+{
+ tv->tv_sec = (long)d;
+ tv->tv_usec = (d - tv->tv_sec) * 1000000;
+ while (tv->tv_usec < 0) {
+ tv->tv_usec += 1000000;
+ tv->tv_sec -= 1;
+ }
+}
+
+double
+lfp_to_d(struct l_fixedpt lfp)
+{
+ double ret;
+
+ lfp.int_partl = ntohl(lfp.int_partl);
+ lfp.fractionl = ntohl(lfp.fractionl);
+
+ ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
+
+ return (ret);
+}
+#if 0
+struct l_fixedpt
+d_to_lfp(double d)
+{
+ struct l_fixedpt lfp;
+
+ lfp.int_partl = htonl((u_int32_t)d);
+ lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
+
+ return (lfp);
+}
+
+double
+sfp_to_d(struct s_fixedpt sfp)
+{
+ double ret;
+
+ sfp.int_parts = ntohs(sfp.int_parts);
+ sfp.fractions = ntohs(sfp.fractions);
+
+ ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
+
+ return (ret);
+}
+
+struct s_fixedpt
+d_to_sfp(double d)
+{
+ struct s_fixedpt sfp;
+
+ sfp.int_parts = htons((u_int16_t)d);
+ sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
+
+ return (sfp);
+}
+#endif