Longstanding __USE_W32_SOCKETS hiccup.

Dave Korn dave.korn.cygwin@googlemail.com
Fri Aug 7 14:16:00 GMT 2009

Corinna Vinschen wrote:

> struct timeval in winsock2.h only differs by using `long' instead of
> time_t and susecond_t as types of tv_sec and tv_usec.  The size of the
> structs is the same, the underlying types are equivalent.
> So, wouldn't it be less hassle in the long run to define struct timeval
> in winsock2.h equivalent to sys/time.h if building for Cygwin?

  Looking at your patch, I discovered that we never actually define

  That gave me another idea; what if we turn your idea on its head, and define
struct timeval in sys/time.h equivalent to winsock2.h if *not* building for
Cygwin (sockets, i.e. if __USE_W32_SOCKETS).  And on taking a closer look, this
turns out to be what happens in mingw/sys/time.h, so how about doing it this
way; duplicating the hunk from winsock2.h that would get excluded by
_TIMEVAL_DEFINED into time.h, but only when __USE_W32_SOCKETS, and then setting

--- include/sys/time.h.orig	2009-08-07 15:13:30.484375000 +0100
+++ include/sys/time.h	2009-08-07 15:13:34.593750000 +0100
@@ -13,10 +13,25 @@ extern "C" {

 #ifndef _WINSOCK_H
+#ifdef __USE_W32_SOCKETS
+struct timeval {
+  long tv_sec;
+  long tv_usec;
+#define timerisset(tvp)	 ((tvp)->tv_sec || (tvp)->tv_usec)
+#define timercmp(tvp, uvp, cmp) \
+	(((tvp)->tv_sec != (uvp)->tv_sec) ? \
+	((tvp)->tv_sec cmp (uvp)->tv_sec) : \
+	((tvp)->tv_usec cmp (uvp)->tv_usec))
+#define timerclear(tvp)	 (tvp)->tv_sec = (tvp)->tv_usec = 0
+#else /* !__USE_W32_SOCKETS */
 struct timeval {
   time_t      tv_sec;
   suseconds_t tv_usec;
+#endif /* __USE_W32_SOCKETS */

 struct timezone {
   int tz_minuteswest;

  I _think_ (not having pondered it for very long yet) that doing it this way
should work for any order of includes and any state of the __USE_W32_SOCKETS macro.


More information about the Cygwin-developers mailing list