Another SIGIO-removal fixe
[gnokii.git] / common / devices / device.c
index a912a74..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,27 +193,27 @@ struct timeval target,timeout;
   target.tv_sec +=usecs/1000000 + (target.tv_usec/1000000);
   target.tv_usec%=1000000;
 
-  target.tv_usec+=1000000;
-  target.tv_sec --;
+  for (;;) {
+    if (gettimeofday(&timeout/*tv*/,NULL/*tz*/))
+      perror("usleep_watchdevice()/gettimeofday(2) loop");
+
+    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;
+    }
+    if (timeout_sec<0)
+      return;
+
+    timeout.tv_sec =timeout_sec ;
+    timeout.tv_usec=timeout_usec;
 
-  for (;;)
     FD_ZERO(&readfds);
     if (device_portfd>=0)
       FD_SET(device_portfd,&readfds);
 
-    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;
-    timeout.tv_usec=target.tv_usec - timeout.tv_usec;
-    if (timeout.tv_usec>=1000000) {
-      timeout.tv_usec-=1000000;
-      if (!timeout.tv_sec)
-       return;
-      timeout.tv_sec--;
-      }
-
     err=select((device_portfd<0 ? 0 : device_portfd+1),&readfds,NULL,NULL,&timeout);
     if ( err > 0 ) {
       if (device_portfd>=0 && FD_ISSET(device_portfd,&readfds)) {
@@ -223,6 +224,7 @@ struct timeval target,timeout;
       if (err == -1)
       perror("Error in SelectLoop");
     }
+  }
 }
 #endif