]> The Tcpdump Group git mirrors - tcpdump/commitdiff
from Gisle Vanem <[email protected]>: privatize filetime_to_unix_epoch() gettimeofday...
authorhannes <hannes>
Mon, 5 Dec 2005 08:57:30 +0000 (08:57 +0000)
committerhannes <hannes>
Mon, 5 Dec 2005 08:57:30 +0000 (08:57 +0000)
util.c

diff --git a/util.c b/util.c
index 178525d4a0c6af2c8acc77acc792a4eed719312a..2faebcd15757f039580f50860d80678e32872ba1 100644 (file)
--- a/util.c
+++ b/util.c
@@ -21,7 +21,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.100 2005-06-16 01:19:38 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/util.c,v 1.101 2005-12-05 08:57:30 hannes Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -526,3 +526,43 @@ safeputchar(int c)
        else
                printf("\\%03o", ch);
 }
+
+#ifdef WIN32
+/*
+ * Number of micro-seconds between the beginning of the Windows epoch
+ * (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970).
+ *
+ * This assumes all Win32 compilers have 64-bit support.
+ */
+#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__)
+  #define DELTA_EPOCH_IN_USEC  11644473600000000Ui64
+#else
+  #define DELTA_EPOCH_IN_USEC  11644473600000000ULL
+#endif
+
+static u_int64_t filetime_to_unix_epoch (const FILETIME *ft)
+{
+    u_int64_t res = (u_int64_t) ft->dwHighDateTime << 32;
+
+    res |= ft->dwLowDateTime;
+    res /= 10;                   /* from 100 nano-sec periods to usec */
+    res -= DELTA_EPOCH_IN_USEC;  /* from Win epoch to Unix epoch */
+    return (res);
+}
+
+int gettimeofday (struct timeval *tv, void *tz _U_)
+{
+    FILETIME  ft;
+    u_int64_t tim;
+
+    if (!tv) {
+        errno = EINVAL;
+        return (-1);
+    }
+    GetSystemTimeAsFileTime (&ft);
+    tim = filetime_to_unix_epoch (&ft);
+    tv->tv_sec  = (long) (tim / 1000000L);
+    tv->tv_usec = (long) (tim % 1000000L);
+    return (0);
+}
+#endif