xref: /onnv-gate/usr/src/lib/libresolv2/common/bsd/gettimeofday.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate #ifndef LINT
2*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: gettimeofday.c,v 1.4 2005/04/27 04:56:11 sra Exp $";
30Sstevel@tonic-gate #endif
40Sstevel@tonic-gate 
50Sstevel@tonic-gate #include "port_before.h"
6*11038SRao.Shoaib@Sun.COM #include <stdio.h>
7*11038SRao.Shoaib@Sun.COM #include <syslog.h>
8*11038SRao.Shoaib@Sun.COM #include <sys/time.h>
90Sstevel@tonic-gate #include "port_after.h"
100Sstevel@tonic-gate 
110Sstevel@tonic-gate #if !defined(NEED_GETTIMEOFDAY)
12*11038SRao.Shoaib@Sun.COM /*%
13*11038SRao.Shoaib@Sun.COM  * gettimeofday() occasionally returns invalid tv_usec on some platforms.
14*11038SRao.Shoaib@Sun.COM  */
15*11038SRao.Shoaib@Sun.COM #define MILLION 1000000
16*11038SRao.Shoaib@Sun.COM #undef gettimeofday
17*11038SRao.Shoaib@Sun.COM 
18*11038SRao.Shoaib@Sun.COM int
isc__gettimeofday(struct timeval * tp,struct timezone * tzp)19*11038SRao.Shoaib@Sun.COM isc__gettimeofday(struct timeval *tp, struct timezone *tzp) {
20*11038SRao.Shoaib@Sun.COM 	int res;
21*11038SRao.Shoaib@Sun.COM 
22*11038SRao.Shoaib@Sun.COM 	res = gettimeofday(tp, tzp);
23*11038SRao.Shoaib@Sun.COM 	if (res < 0)
24*11038SRao.Shoaib@Sun.COM 		return (res);
25*11038SRao.Shoaib@Sun.COM 	if (tp == NULL)
26*11038SRao.Shoaib@Sun.COM 		return (res);
27*11038SRao.Shoaib@Sun.COM 	if (tp->tv_usec < 0) {
28*11038SRao.Shoaib@Sun.COM 		do {
29*11038SRao.Shoaib@Sun.COM 			tp->tv_usec += MILLION;
30*11038SRao.Shoaib@Sun.COM 			tp->tv_sec--;
31*11038SRao.Shoaib@Sun.COM 		} while (tp->tv_usec < 0);
32*11038SRao.Shoaib@Sun.COM 		goto log;
33*11038SRao.Shoaib@Sun.COM 	} else if (tp->tv_usec > MILLION) {
34*11038SRao.Shoaib@Sun.COM 		do {
35*11038SRao.Shoaib@Sun.COM 			tp->tv_usec -= MILLION;
36*11038SRao.Shoaib@Sun.COM 			tp->tv_sec++;
37*11038SRao.Shoaib@Sun.COM 		} while (tp->tv_usec > MILLION);
38*11038SRao.Shoaib@Sun.COM 		goto log;
39*11038SRao.Shoaib@Sun.COM 	}
40*11038SRao.Shoaib@Sun.COM 	return (res);
41*11038SRao.Shoaib@Sun.COM  log:
42*11038SRao.Shoaib@Sun.COM 	syslog(LOG_ERR, "gettimeofday: tv_usec out of range\n");
43*11038SRao.Shoaib@Sun.COM 	return (res);
44*11038SRao.Shoaib@Sun.COM }
450Sstevel@tonic-gate #else
460Sstevel@tonic-gate int
gettimeofday(struct timeval * tvp,struct _TIMEZONE * tzp)470Sstevel@tonic-gate gettimeofday(struct timeval *tvp, struct _TIMEZONE *tzp) {
480Sstevel@tonic-gate 	time_t clock, time(time_t *);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	if (time(&clock) == (time_t) -1)
510Sstevel@tonic-gate 		return (-1);
520Sstevel@tonic-gate 	if (tvp) {
530Sstevel@tonic-gate 		tvp->tv_sec = clock;
540Sstevel@tonic-gate 		tvp->tv_usec = 0;
550Sstevel@tonic-gate 	}
560Sstevel@tonic-gate 	if (tzp) {
570Sstevel@tonic-gate 		tzp->tz_minuteswest = 0;
580Sstevel@tonic-gate 		tzp->tz_dsttime = 0;
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 	return (0);
610Sstevel@tonic-gate }
620Sstevel@tonic-gate #endif /*NEED_GETTIMEOFDAY*/
63