Another SIGIO-removal fixe
[gnokii.git] / common / devices / device.c
index 826eee4..be2f079 100644 (file)
@@ -185,6 +185,7 @@ void usleep_watchdevice(unsigned long usecs)
 int err;
 fd_set readfds;
 struct timeval target,timeout;
+long timeout_sec,timeout_usec; /* signed! */
 
   if (gettimeofday(&target/*tv*/,NULL/*tz*/))
     perror("usleep_watchdevice()/gettimeofday(2) init");
@@ -192,23 +193,26 @@ struct timeval target,timeout;
   target.tv_sec +=usecs/1000000 + (target.tv_usec/1000000);
   target.tv_usec%=1000000;
 
-  for (;;)
-    FD_ZERO(&readfds);
-    if (device_portfd>=0)
-      FD_SET(device_portfd,&readfds);
-
+  for (;;) {
     if (gettimeofday(&timeout/*tv*/,NULL/*tz*/))
       perror("usleep_watchdevice()/gettimeofday(2) loop");
-    if (target.tv_sec  < timeout.tv_sec)
-      return;
-    timeout.tv_sec =target.tv_sec - timeout.tv_sec;
-    if (target.tv_usec < timeout.tv_usec) {
-      target.tv_usec+=1000000;
-      if (target.tv_sec <= 0)
-       return;
-      target.tv_sec--;
+
+    timeout_sec =target.tv_sec -(long)timeout.tv_sec ;
+    timeout_usec=target.tv_usec-(long)timeout.tv_usec;
+
+    if (timeout_usec<0) {
+      timeout_sec--;
+      timeout_usec+=1000000;
     }
-    timeout.tv_usec=target.tv_usec - timeout.tv_usec;
+    if (timeout_sec<0)
+      return;
+
+    timeout.tv_sec =timeout_sec ;
+    timeout.tv_usec=timeout_usec;
+
+    FD_ZERO(&readfds);
+    if (device_portfd>=0)
+      FD_SET(device_portfd,&readfds);
 
     err=select((device_portfd<0 ? 0 : device_portfd+1),&readfds,NULL,NULL,&timeout);
     if ( err > 0 ) {
@@ -220,6 +224,7 @@ struct timeval target,timeout;
       if (err == -1)
       perror("Error in SelectLoop");
     }
+  }
 }
 #endif