summaryrefslogtreecommitdiff
path: root/usr/src/lib/libresolv2_joy/common/bsd/gettimeofday.c
blob: f5ff8c54f50252c813bcfe93a290875901d20a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "port_before.h"
#include <stdio.h>
#include <syslog.h>
#include <sys/time.h>
#include "port_after.h"

#if !defined(NEED_GETTIMEOFDAY)
/*%
 * gettimeofday() occasionally returns invalid tv_usec on some platforms.
 */
#define MILLION 1000000
#undef gettimeofday

int
isc__gettimeofday(struct timeval *tp, struct timezone *tzp) {
	int res;

	res = gettimeofday(tp, tzp);
	if (res < 0)
		return (res);
	if (tp == NULL)
		return (res);
	if (tp->tv_usec < 0) {
		do {
			tp->tv_usec += MILLION;
			tp->tv_sec--;
		} while (tp->tv_usec < 0);
		goto log;
	} else if (tp->tv_usec > MILLION) {
		do {
			tp->tv_usec -= MILLION;
			tp->tv_sec++;
		} while (tp->tv_usec > MILLION);
		goto log;
	}
	return (res);
 log:
	syslog(LOG_ERR, "gettimeofday: tv_usec out of range\n");
	return (res);
}
#else
int
gettimeofday(struct timeval *tvp, struct _TIMEZONE *tzp) {
	time_t clock, time(time_t *);

	if (time(&clock) == (time_t) -1)
		return (-1);
	if (tvp) {
		tvp->tv_sec = clock;
		tvp->tv_usec = 0;
	}
	if (tzp) {
		tzp->tz_minuteswest = 0;
		tzp->tz_dsttime = 0;
	}
	return (0);
}
#endif /*NEED_GETTIMEOFDAY*/