ObjFW  Diff

Differences From Artifact [8542470fa0]:

To Artifact [ef90137219]:


114
115
116
117
118
119
120
121

122
123
124
125

126
127
128
129
130
131
132
114
115
116
117
118
119
120

121
122
123
124

125
126
127
128
129
130
131
132







-
+



-
+







	arguments: (OFArray*)arguments
      environment: (OFDictionary*)environment
{
	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;

			argv = [self allocMemoryWithSize: sizeof(char*)
						   count: count + 2];
147
148
149
150
151
152
153
154
155
156
157




158
159
160
161
162
163
164
165
166
167
168


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185



186


187
188
189
190

191
192
193
194
195
196
197
198


199
200
201
202
203
204
205
206
207


208
209
210
211
212
213
214
147
148
149
150
151
152
153




154
155
156
157
158
159
160
161
162
163
164
165
166


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

189
190
191
192
193

194
195
196
197





198
199
200
201
202
203
204
205
206


207
208
209
210
211
212
213
214
215







-
-
-
-
+
+
+
+









-
-
+
+

















+
+
+
-
+
+



-
+



-
-
-
-
-
+
+







-
-
+
+







				    OF_environmentForDictionary: environment];
#else
				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;
		STARTUPINFOW si;
		void *pool;
		OFMutableString *argumentsString;
		OFEnumerator *enumerator;
		OFString *argument;
		uint16_t *argumentsCopy;
		size_t length;

		sa.nLength = sizeof(sa);
		sa.bInheritHandle = TRUE;
		sa.lpSecurityDescriptor = NULL;

		if (!CreatePipe(&_readPipe[0], &_readPipe[1], &sa, 0))
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];
		if (!CreatePipe(&readPipe[0], &readPipe[1], &sa, 0))

		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();

		argumentsString =
		    [OFMutableString stringWithString: programName];
259
260
261
262
263
264
265
266

267
268
269
270


271
272
273
274
275
276
277
278







279
280
281
282
283
284
285
260
261
262
263
264
265
266

267
268
269


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293







-
+


-
-
+
+








+
+
+
+
+
+
+







				    exceptionWithClass: [self class]];
		} @finally {
			[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;
	char **envp;
	size_t i, count;
350
351
352
353
354
355
356
357

358
359

360
361
362
363

364
365
366
367
368
369
370
371
372
373
374
375
376
377


378
379
380


381
382

383
384
385
386
387
388
389
390
391
392
393

394
395
396
397
398
399
400
401
402
403


404
405
406
407
408


409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

427
428
429
430
431
432
433
434
435
436

437
438
439
440
441
442
443
444
445
446
447


448
449

450
451
452


453
454

455
456
457
458
459
460
461
462
463
464




465
466
467
468



469
470
471
472
473



474
475
476
477
478




479
480
481
482



483
484
485
486
487



488
489
490
358
359
360
361
362
363
364

365
366

367
368
369
370

371
372
373
374
375
376
377
378
379
380
381
382
383


384
385
386


387
388
389

390
391
392
393
394
395
396
397
398
399
400

401
402
403
404
405
406
407
408
409


410
411
412
413
414


415
416
417
418
419
420
421
422
423







424
425
426

427
428
429
430
431
432
433
434
435
436

437
438
439
440
441
442
443
444
445
446


447
448
449

450
451


452
453
454

455
456
457
458
459
460
461




462
463
464
465
466



467
468
469
470
471



472
473
474
475




476
477
478
479
480



481
482
483
484
485



486
487
488
489
490
491







-
+

-
+



-
+












-
-
+
+

-
-
+
+

-
+










-
+








-
-
+
+



-
-
+
+







-
-
-
-
-
-
-



-
+









-
+









-
-
+
+

-
+

-
-
+
+

-
+






-
-
-
-
+
+
+
+

-
-
-
+
+
+


-
-
-
+
+
+

-
-
-
-
+
+
+
+

-
-
-
+
+
+


-
-
-
+
+
+



	return [env items];
}
#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
{
#ifndef _WIN32
	ssize_t ret;
#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]
							  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 (_readPipe[0] != -1)
		close(_readPipe[0]);
	if (_writePipe[1] != -1)
		close(_writePipe[1]);

	if (pid != -1) {
		kill(pid, SIGKILL);
		waitpid(pid, &status, WNOHANG);
	if (_pid != -1) {
		kill(_pid, SIGKILL);
		waitpid(_pid, &_status, WNOHANG);
	}

	pid = -1;
	readPipe[0] = -1;
	writePipe[1] = -1;
	_pid = -1;
	_readPipe[0] = -1;
	_writePipe[1] = -1;
#else
	if (readPipe[0] != NULL)
		CloseHandle(readPipe[0]);
	if (writePipe[1] != NULL)
		CloseHandle(writePipe[1]);
	if (_readPipe[0] != NULL)
		CloseHandle(_readPipe[0]);
	if (_writePipe[1] != NULL)
		CloseHandle(_writePipe[1]);

	if (process != INVALID_HANDLE_VALUE) {
		TerminateProcess(process, 0);
		CloseHandle(process);
	if (_process != INVALID_HANDLE_VALUE) {
		TerminateProcess(_process, 0);
		CloseHandle(_process);
	}

	process = INVALID_HANDLE_VALUE;
	readPipe[0] = NULL;
	writePipe[1] = NULL;
	_process = INVALID_HANDLE_VALUE;
	_readPipe[0] = NULL;
	_writePipe[1] = NULL;
#endif
}
@end