xref: /openbsd-src/usr.sbin/ntpd/util.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: util.c,v 1.15 2013/10/15 20:35:55 krw Exp $ */
2 
3 /*
4  * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/time.h>
20 #include <limits.h>
21 #include <stdio.h>
22 
23 #include "ntpd.h"
24 
25 double
26 gettime_corrected(void)
27 {
28 	return (gettime() + getoffset());
29 }
30 
31 double
32 getoffset(void)
33 {
34 	struct timeval	tv;
35 	if (adjtime(NULL, &tv) == -1)
36 		return (0.0);
37 	return (tv.tv_sec + 1.0e-6 * tv.tv_usec);
38 }
39 
40 double
41 gettime(void)
42 {
43 	struct timeval	tv;
44 
45 	if (gettimeofday(&tv, NULL) == -1)
46 		fatal("gettimeofday");
47 
48 	return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
49 }
50 
51 time_t
52 getmonotime(void)
53 {
54 	struct timespec	ts;
55 
56 	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
57 		fatal("clock_gettime");
58 
59 	return (ts.tv_sec);
60 }
61 
62 
63 void
64 d_to_tv(double d, struct timeval *tv)
65 {
66 	tv->tv_sec = d;
67 	tv->tv_usec = (d - tv->tv_sec) * 1000000;
68 	while (tv->tv_usec < 0) {
69 		tv->tv_usec += 1000000;
70 		tv->tv_sec -= 1;
71 	}
72 }
73 
74 double
75 lfp_to_d(struct l_fixedpt lfp)
76 {
77 	double	ret;
78 
79 	lfp.int_partl = ntohl(lfp.int_partl);
80 	lfp.fractionl = ntohl(lfp.fractionl);
81 
82 	ret = (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX);
83 
84 	return (ret);
85 }
86 
87 struct l_fixedpt
88 d_to_lfp(double d)
89 {
90 	struct l_fixedpt	lfp;
91 
92 	lfp.int_partl = htonl((u_int32_t)d);
93 	lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX));
94 
95 	return (lfp);
96 }
97 
98 double
99 sfp_to_d(struct s_fixedpt sfp)
100 {
101 	double	ret;
102 
103 	sfp.int_parts = ntohs(sfp.int_parts);
104 	sfp.fractions = ntohs(sfp.fractions);
105 
106 	ret = (double)(sfp.int_parts) + ((double)sfp.fractions / USHRT_MAX);
107 
108 	return (ret);
109 }
110 
111 struct s_fixedpt
112 d_to_sfp(double d)
113 {
114 	struct s_fixedpt	sfp;
115 
116 	sfp.int_parts = htons((u_int16_t)d);
117 	sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX));
118 
119 	return (sfp);
120 }
121 
122 char *
123 print_rtable(int r)
124 {
125 	static char b[11];
126 
127 	b[0] = 0;
128 	if (r > 0)
129 		snprintf(b, sizeof(b), "rtable %d", r);
130 
131 	return(b);
132 }
133