From 9fd4560ba2762f646b92713a87b42ad2cc00928d Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 17 Jun 2005 13:57:27 -0400 Subject: [PATCH] BUG: Fixed polling capability of WaitForData. --- Source/kwsys/ProcessUNIX.c | 46 +++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/Source/kwsys/ProcessUNIX.c b/Source/kwsys/ProcessUNIX.c index 96e6ea720..f07d8eeae 100644 --- a/Source/kwsys/ProcessUNIX.c +++ b/Source/kwsys/ProcessUNIX.c @@ -103,6 +103,7 @@ static int kwsysProcessSetupOutputPipeFile(int* p, const char* name); static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout, kwsysProcessTime* timeoutTime); static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime, + double* userTimeout, kwsysProcessTimeNative* timeoutLength); static kwsysProcessTime kwsysProcessTimeGetCurrent(void); static double kwsysProcessTimeToDouble(kwsysProcessTime t); @@ -858,6 +859,22 @@ int kwsysProcess_WaitForData(kwsysProcess* cp, char** data, int* length, anyway). */ FD_ZERO(&cp->PipeSet); + /* Setup a timeout if required. */ + if(timeoutTime.tv_sec < 0) + { + timeout = 0; + } + else + { + timeout = &timeoutLength; + } + if(kwsysProcessGetTimeoutLeft(&timeoutTime, user?userTimeout:0, &timeoutLength)) + { + /* Timeout has already expired. */ + expired = 1; + break; + } + /* Add the pipe reading ends that are still open. */ max = -1; for(i=0; i < KWSYSPE_PIPE_COUNT; ++i) @@ -879,22 +896,6 @@ int kwsysProcess_WaitForData(kwsysProcess* cp, char** data, int* length, break; } - /* Setup a timeout if required. */ - if(timeoutTime.tv_sec < 0) - { - timeout = 0; - } - else - { - timeout = &timeoutLength; - } - if(kwsysProcessGetTimeoutLeft(&timeoutTime, &timeoutLength)) - { - /* Timeout has already expired. */ - expired = 1; - break; - } - /* Run select to block until data are available. Repeat call until it is not interrupted. */ while(((numReady = select(max+1, &cp->PipeSet, 0, 0, timeout)) < 0) && @@ -1468,8 +1469,9 @@ static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout, kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent(); kwsysProcessTime userTimeoutLength = kwsysProcessTimeFromDouble(*userTimeout); kwsysProcessTime userTimeoutTime = kwsysProcessTimeAdd(currentTime, - userTimeoutLength); - if(kwsysProcessTimeLess(userTimeoutTime, *timeoutTime)) + userTimeoutLength); + if(timeoutTime->tv_sec < 0 || + kwsysProcessTimeLess(userTimeoutTime, *timeoutTime)) { *timeoutTime = userTimeoutTime; return 1; @@ -1482,6 +1484,7 @@ static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout, /* Get the length of time before the given timeout time arrives. Returns 1 if the time has already arrived, and 0 otherwise. */ static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime, + double* userTimeout, kwsysProcessTimeNative* timeoutLength) { if(timeoutTime->tv_sec < 0) @@ -1495,6 +1498,13 @@ static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime, kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent(); kwsysProcessTime timeLeft = kwsysProcessTimeSubtract(*timeoutTime, currentTime); + if(timeLeft.tv_sec < 0 && userTimeout && *userTimeout <= 0) + { + /* Caller has explicitly requested a zero timeout. */ + timeLeft.tv_sec = 0; + timeLeft.tv_usec = 0; + } + if(timeLeft.tv_sec < 0) { /* Timeout has already expired. */