X-Git-Url: http://git.jankratochvil.net/?p=gnokii.git;a=blobdiff_plain;f=common%2Fdevices%2Fdevice.c;fp=common%2Fdevices%2Fdevice.c;h=be2f079f46811131ea06eb439b509dfd7cb23be7;hp=a912a74d325bd48aadff88bea659cd57dc866044;hb=e35da9a4cc74781bc73fbc5a56c45c98af97fe0b;hpb=47dd33ef5e67856eb012b82b7b16d44bd05166c8 diff --git a/common/devices/device.c b/common/devices/device.c index a912a74..be2f079 100644 --- a/common/devices/device.c +++ b/common/devices/device.c @@ -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