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