@@ -116,15 +116,15 @@ { self = [super init]; @try { #ifndef _WIN32 - if (pipe(readPipe) != 0 || pipe(writePipe) != 0) + if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0) @throw [OFInitializationFailedException exceptionWithClass: [self class]]; - switch ((pid = fork())) { + switch ((_pid = fork())) { case 0:; OFString **objects = [arguments objects]; size_t i, count = [arguments count]; char **argv; @@ -149,25 +149,25 @@ environ = [self OF_environmentForDictionary: environment]; #endif } - close(readPipe[0]); - close(writePipe[1]); - dup2(writePipe[0], 0); - dup2(readPipe[1], 1); + close(_readPipe[0]); + close(_writePipe[1]); + dup2(_writePipe[0], 0); + dup2(_readPipe[1], 1); execvp([program cStringWithEncoding: OF_STRING_ENCODING_NATIVE], argv); @throw [OFInitializationFailedException exceptionWithClass: [self class]]; case -1: @throw [OFInitializationFailedException exceptionWithClass: [self class]]; default: - close(readPipe[1]); - close(writePipe[0]); + close(_readPipe[1]); + close(_writePipe[0]); break; } #else SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi; @@ -181,32 +181,33 @@ sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; - if (!CreatePipe(&readPipe[0], &readPipe[1], &sa, 0)) + if (!CreatePipe(&_readPipe[0], &_readPipe[1], &sa, 0)) + @throw [OFInitializationFailedException + exceptionWithClass: [self class]]; + + if (!SetHandleInformation(_readPipe[0], HANDLE_FLAG_INHERIT, 0)) @throw [OFInitializationFailedException exceptionWithClass: [self class]]; - if (!SetHandleInformation(readPipe[0], HANDLE_FLAG_INHERIT, 0)) + if (!CreatePipe(&_writePipe[0], &_writePipe[1], &sa, 0)) @throw [OFInitializationFailedException exceptionWithClass: [self class]]; - if (!CreatePipe(&writePipe[0], &writePipe[1], &sa, 0)) - @throw [OFInitializationFailedException - exceptionWithClass: [self class]]; - - if (!SetHandleInformation(writePipe[1], HANDLE_FLAG_INHERIT, 0)) + if (!SetHandleInformation(_writePipe[1], + HANDLE_FLAG_INHERIT, 0)) @throw [OFInitializationFailedException exceptionWithClass: [self class]]; memset(&pi, 0, sizeof(pi)); memset(&si, 0, sizeof(si)); si.cb = sizeof(si); - si.hStdInput = writePipe[0]; - si.hStdOutput = readPipe[1]; + si.hStdInput = _writePipe[0]; + si.hStdOutput = _readPipe[1]; si.hStdError = GetStdHandle(STD_ERROR_HANDLE); si.dwFlags |= STARTF_USESTDHANDLES; pool = objc_autoreleasePoolPush(); @@ -261,23 +262,30 @@ [self freeMemory: argumentsCopy]; } objc_autoreleasePoolPop(pool); - process = pi.hProcess; + _process = pi.hProcess; CloseHandle(pi.hThread); - CloseHandle(readPipe[1]); - CloseHandle(writePipe[0]); + CloseHandle(_readPipe[1]); + CloseHandle(_writePipe[0]); #endif } @catch (id e) { [self release]; @throw e; } return self; } + +- (void)dealloc +{ + [self close]; + + [super dealloc]; +} #ifndef _WIN32 - (char**)OF_environmentForDictionary: (OFDictionary*)environment { OFEnumerator *keyEnumerator, *objectEnumerator; @@ -352,17 +360,17 @@ #endif - (BOOL)lowlevelIsAtEndOfStream { #ifndef _WIN32 - if (readPipe[0] == -1) + if (_readPipe[0] == -1) #else - if (readPipe[0] == NULL) + if (_readPipe[0] == NULL) #endif return YES; - return atEndOfStream; + return _atEndOfStream; } - (size_t)lowlevelReadIntoBuffer: (void*)buffer length: (size_t)length { @@ -371,17 +379,17 @@ #else DWORD ret; #endif #ifndef _WIN32 - if (readPipe[0] == -1 || atEndOfStream || - (ret = read(readPipe[0], buffer, length)) < 0) { + if (_readPipe[0] == -1 || _atEndOfStream || + (ret = read(_readPipe[0], buffer, length)) < 0) { #else - if (readPipe[0] == NULL || atEndOfStream || - !ReadFile(readPipe[0], buffer, length, &ret, NULL)) { + if (_readPipe[0] == NULL || _atEndOfStream || + !ReadFile(_readPipe[0], buffer, length, &ret, NULL)) { if (GetLastError() == ERROR_BROKEN_PIPE) { - atEndOfStream = YES; + _atEndOfStream = YES; return 0; } #endif @throw [OFReadFailedException exceptionWithClass: [self class] @@ -388,103 +396,96 @@ stream: self requestedLength: length]; } if (ret == 0) - atEndOfStream = YES; + _atEndOfStream = YES; return ret; } - (void)lowlevelWriteBuffer: (const void*)buffer length: (size_t)length { #ifndef _WIN32 - if (writePipe[1] == -1 || atEndOfStream || - write(writePipe[1], buffer, length) < length) + if (_writePipe[1] == -1 || _atEndOfStream || + write(_writePipe[1], buffer, length) < length) #else DWORD ret; - if (writePipe[1] == NULL || atEndOfStream || - !WriteFile(writePipe[1], buffer, length, &ret, NULL) || + if (_writePipe[1] == NULL || _atEndOfStream || + !WriteFile(_writePipe[1], buffer, length, &ret, NULL) || ret < length) #endif @throw [OFWriteFailedException exceptionWithClass: [self class] stream: self requestedLength: length]; } -- (void)dealloc -{ - [self close]; - - [super dealloc]; -} - - (int)fileDescriptorForReading { #ifndef _WIN32 - return readPipe[0]; + return _readPipe[0]; #else [self doesNotRecognizeSelector: _cmd]; abort(); #endif } - (int)fileDescriptorForWriting { #ifndef _WIN32 - return writePipe[1]; + return _writePipe[1]; #else [self doesNotRecognizeSelector: _cmd]; abort(); #endif } - (void)closeForWriting { #ifndef _WIN32 - if (writePipe[1] != -1) - close(writePipe[1]); + if (_writePipe[1] != -1) + close(_writePipe[1]); - writePipe[1] = -1; + _writePipe[1] = -1; #else - if (writePipe[1] != NULL) - CloseHandle(writePipe[1]); + if (_writePipe[1] != NULL) + CloseHandle(_writePipe[1]); - writePipe[1] = NULL; + _writePipe[1] = NULL; #endif } - (void)close { #ifndef _WIN32 - if (readPipe[0] != -1) - close(readPipe[0]); - if (writePipe[1] != -1) - close(writePipe[1]); - - if (pid != -1) { - kill(pid, SIGKILL); - waitpid(pid, &status, WNOHANG); - } - - pid = -1; - readPipe[0] = -1; - writePipe[1] = -1; -#else - if (readPipe[0] != NULL) - CloseHandle(readPipe[0]); - if (writePipe[1] != NULL) - CloseHandle(writePipe[1]); - - if (process != INVALID_HANDLE_VALUE) { - TerminateProcess(process, 0); - CloseHandle(process); - } - - process = INVALID_HANDLE_VALUE; - readPipe[0] = NULL; - writePipe[1] = NULL; + if (_readPipe[0] != -1) + close(_readPipe[0]); + if (_writePipe[1] != -1) + close(_writePipe[1]); + + if (_pid != -1) { + kill(_pid, SIGKILL); + waitpid(_pid, &_status, WNOHANG); + } + + _pid = -1; + _readPipe[0] = -1; + _writePipe[1] = -1; +#else + if (_readPipe[0] != NULL) + CloseHandle(_readPipe[0]); + if (_writePipe[1] != NULL) + CloseHandle(_writePipe[1]); + + if (_process != INVALID_HANDLE_VALUE) { + TerminateProcess(_process, 0); + CloseHandle(_process); + } + + _process = INVALID_HANDLE_VALUE; + _readPipe[0] = NULL; + _writePipe[1] = NULL; #endif } @end