17c478bd9Sstevel@tonic-gate #ifndef LINT 2*9525b14bSRao Shoaib static const char rcsid[] = "$Id: gettimeofday.c,v 1.4 2005/04/27 04:56:11 sra Exp $"; 37c478bd9Sstevel@tonic-gate #endif 47c478bd9Sstevel@tonic-gate 57c478bd9Sstevel@tonic-gate #include "port_before.h" 6*9525b14bSRao Shoaib #include <stdio.h> 7*9525b14bSRao Shoaib #include <syslog.h> 8*9525b14bSRao Shoaib #include <sys/time.h> 97c478bd9Sstevel@tonic-gate #include "port_after.h" 107c478bd9Sstevel@tonic-gate 117c478bd9Sstevel@tonic-gate #if !defined(NEED_GETTIMEOFDAY) 12*9525b14bSRao Shoaib /*% 13*9525b14bSRao Shoaib * gettimeofday() occasionally returns invalid tv_usec on some platforms. 14*9525b14bSRao Shoaib */ 15*9525b14bSRao Shoaib #define MILLION 1000000 16*9525b14bSRao Shoaib #undef gettimeofday 17*9525b14bSRao Shoaib 18*9525b14bSRao Shoaib int 19*9525b14bSRao Shoaib isc__gettimeofday(struct timeval *tp, struct timezone *tzp) { 20*9525b14bSRao Shoaib int res; 21*9525b14bSRao Shoaib 22*9525b14bSRao Shoaib res = gettimeofday(tp, tzp); 23*9525b14bSRao Shoaib if (res < 0) 24*9525b14bSRao Shoaib return (res); 25*9525b14bSRao Shoaib if (tp == NULL) 26*9525b14bSRao Shoaib return (res); 27*9525b14bSRao Shoaib if (tp->tv_usec < 0) { 28*9525b14bSRao Shoaib do { 29*9525b14bSRao Shoaib tp->tv_usec += MILLION; 30*9525b14bSRao Shoaib tp->tv_sec--; 31*9525b14bSRao Shoaib } while (tp->tv_usec < 0); 32*9525b14bSRao Shoaib goto log; 33*9525b14bSRao Shoaib } else if (tp->tv_usec > MILLION) { 34*9525b14bSRao Shoaib do { 35*9525b14bSRao Shoaib tp->tv_usec -= MILLION; 36*9525b14bSRao Shoaib tp->tv_sec++; 37*9525b14bSRao Shoaib } while (tp->tv_usec > MILLION); 38*9525b14bSRao Shoaib goto log; 39*9525b14bSRao Shoaib } 40*9525b14bSRao Shoaib return (res); 41*9525b14bSRao Shoaib log: 42*9525b14bSRao Shoaib syslog(LOG_ERR, "gettimeofday: tv_usec out of range\n"); 43*9525b14bSRao Shoaib return (res); 44*9525b14bSRao Shoaib } 457c478bd9Sstevel@tonic-gate #else 467c478bd9Sstevel@tonic-gate int 477c478bd9Sstevel@tonic-gate gettimeofday(struct timeval *tvp, struct _TIMEZONE *tzp) { 487c478bd9Sstevel@tonic-gate time_t clock, time(time_t *); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate if (time(&clock) == (time_t) -1) 517c478bd9Sstevel@tonic-gate return (-1); 527c478bd9Sstevel@tonic-gate if (tvp) { 537c478bd9Sstevel@tonic-gate tvp->tv_sec = clock; 547c478bd9Sstevel@tonic-gate tvp->tv_usec = 0; 557c478bd9Sstevel@tonic-gate } 567c478bd9Sstevel@tonic-gate if (tzp) { 577c478bd9Sstevel@tonic-gate tzp->tz_minuteswest = 0; 587c478bd9Sstevel@tonic-gate tzp->tz_dsttime = 0; 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate return (0); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate #endif /*NEED_GETTIMEOFDAY*/ 63