diff --git a/Source/kwsys/Process.h.in b/Source/kwsys/Process.h.in index cc4529945..7fb681928 100644 --- a/Source/kwsys/Process.h.in +++ b/Source/kwsys/Process.h.in @@ -99,10 +99,11 @@ kwsysEXPORT void kwsysProcess_SetCommand(kwsysProcess* cp, kwsysEXPORT void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout); /** - * Set the working directory for the child process. The working directory can - * be absolute or relative to the current directory. + * Set the working directory for the child process. The working + * directory can be absolute or relative to the current directory. */ -kwsysEXPORT void kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir); +kwsysEXPORT void kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, + const char* dir); /** * Get the current state of the Process instance. Possible states are: diff --git a/Source/kwsys/ProcessUNIX.c b/Source/kwsys/ProcessUNIX.c index 96b830007..64abb4cac 100644 --- a/Source/kwsys/ProcessUNIX.c +++ b/Source/kwsys/ProcessUNIX.c @@ -167,6 +167,7 @@ void kwsysProcess_Delete(kwsysProcess* cp) /* Free memory. */ kwsysProcess_SetCommand(cp, 0); + kwsysProcess_SetWorkingDirectory(cp, 0); free(cp); } @@ -227,7 +228,7 @@ void kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir) } if(dir) { - cp->WorkingDirectory = (char*) malloc(strlen(dir) + 1); + cp->WorkingDirectory = (char*)malloc(strlen(dir) + 1); strcpy(cp->WorkingDirectory, dir); } } @@ -345,6 +346,20 @@ void kwsysProcess_Execute(kwsysProcess* cp) /* Restore all default signal handlers. */ kwsysProcessRestoreDefaultSignalHandlers(); + /* Change to the working directory specified, if any. */ + if(cp->WorkingDirectory) + { + /* Some platforms specify that the chdir call may be + interrupted. Repeat the call until it finishes. */ + int r; + while(((r = chdir(cp->WorkingDirectory)) < 0) && (errno == EINTR)); + if(r < 0) + { + /* Failure. Report error to parent and terminate. */ + kwsysProcessChildErrorExit(cp); + } + } + /* Execute the real process. If successful, this does not return. */ execvp(cp->Command[0], cp->Command);