xref: /illumos-gate/usr/src/lib/libresolv2/common/bsd/gettimeofday.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 #include "port_before.h"
2 #include <stdio.h>
3 #include <syslog.h>
4 #include <sys/time.h>
5 #include "port_after.h"
6 
7 #if !defined(NEED_GETTIMEOFDAY)
8 /*%
9  * gettimeofday() occasionally returns invalid tv_usec on some platforms.
10  */
11 #define MILLION 1000000
12 #undef gettimeofday
13 
14 int
15 isc__gettimeofday(struct timeval *tp, struct timezone *tzp) {
16 	int res;
17 
18 	res = gettimeofday(tp, tzp);
19 	if (res < 0)
20 		return (res);
21 	if (tp == NULL)
22 		return (res);
23 	if (tp->tv_usec < 0) {
24 		do {
25 			tp->tv_usec += MILLION;
26 			tp->tv_sec--;
27 		} while (tp->tv_usec < 0);
28 		goto log;
29 	} else if (tp->tv_usec > MILLION) {
30 		do {
31 			tp->tv_usec -= MILLION;
32 			tp->tv_sec++;
33 		} while (tp->tv_usec > MILLION);
34 		goto log;
35 	}
36 	return (res);
37  log:
38 	syslog(LOG_ERR, "gettimeofday: tv_usec out of range\n");
39 	return (res);
40 }
41 #else
42 int
43 gettimeofday(struct timeval *tvp, struct _TIMEZONE *tzp) {
44 	time_t clock, time(time_t *);
45 
46 	if (time(&clock) == (time_t) -1)
47 		return (-1);
48 	if (tvp) {
49 		tvp->tv_sec = clock;
50 		tvp->tv_usec = 0;
51 	}
52 	if (tzp) {
53 		tzp->tz_minuteswest = 0;
54 		tzp->tz_dsttime = 0;
55 	}
56 	return (0);
57 }
58 #endif /*NEED_GETTIMEOFDAY*/
59