Index: src/OFProcess.h ================================================================== --- src/OFProcess.h +++ src/OFProcess.h @@ -195,8 +195,15 @@ * This method needs to be called for some programs before data can be read, * since some programs don't start processing before the write direction is * closed. */ - (void)closeForWriting; + +/*! + * @brief Waits for the process to terminate and returns the exit status. + * + * If the process has already exited, this returns the exit status immediately. + */ +- (int)waitForTermination; @end OF_ASSUME_NONNULL_END Index: src/platform/posix/OFProcess.m ================================================================== --- src/platform/posix/OFProcess.m +++ src/platform/posix/OFProcess.m @@ -375,6 +375,19 @@ _pid = -1; _readPipe[0] = -1; [super close]; } + +- (int)waitForTermination +{ + if (_readPipe[0] == -1) + @throw [OFNotOpenException exceptionWithObject: self]; + + if (_pid != -1) { + waitpid(_pid, &_status, 0); + _pid = -1; + } + + return WEXITSTATUS(_status); +} @end Index: src/platform/windows/OFProcess.m ================================================================== --- src/platform/windows/OFProcess.m +++ src/platform/windows/OFProcess.m @@ -346,6 +346,28 @@ _process = INVALID_HANDLE_VALUE; _readPipe[0] = NULL; [super close]; } + +- (int)waitForTermination +{ + if (_readPipe[0] == NULL) + @throw [OFNotOpenException exceptionWithObject: self]; + + if (_process != INVALID_HANDLE_VALUE) { + DWORD exitCode; + + WaitForSingleObject(_process, INFINITE); + + if (GetExitCodeProcess(_process, &exitCode)) + _status = exitCode; + else + _status = GetLastError(); + + CloseHandle(_process); + _process = INVALID_HANDLE_VALUE; + } + + return _status; +} @end