@@ -354,17 +354,11 @@ find_other_exec(const char *argv0, const char *target,
354
354
355
355
356
356
/*
357
- * The runtime library's popen() on win32 does not work when being
358
- * called from a service when running on windows <= 2000, because
359
- * there is no stdin/stdout/stderr.
360
- *
361
- * Executing a command in a pipe and reading the first line from it
362
- * is all we need.
357
+ * Execute a command in a pipe and read the first line from it.
363
358
*/
364
359
static char *
365
360
pipe_read_line (char * cmd , char * line , int maxsize )
366
361
{
367
- #ifndef WIN32
368
362
FILE * pgver ;
369
363
370
364
/* flush output buffers in case popen does not... */
@@ -393,130 +387,6 @@ pipe_read_line(char *cmd, char *line, int maxsize)
393
387
return NULL ;
394
388
395
389
return line ;
396
- #else /* WIN32 */
397
-
398
- SECURITY_ATTRIBUTES sattr ;
399
- HANDLE childstdoutrd ,
400
- childstdoutwr ,
401
- childstdoutrddup ;
402
- PROCESS_INFORMATION pi ;
403
- STARTUPINFO si ;
404
- char * retval = NULL ;
405
-
406
- sattr .nLength = sizeof (SECURITY_ATTRIBUTES );
407
- sattr .bInheritHandle = TRUE;
408
- sattr .lpSecurityDescriptor = NULL ;
409
-
410
- if (!CreatePipe (& childstdoutrd , & childstdoutwr , & sattr , 0 ))
411
- return NULL ;
412
-
413
- if (!DuplicateHandle (GetCurrentProcess (),
414
- childstdoutrd ,
415
- GetCurrentProcess (),
416
- & childstdoutrddup ,
417
- 0 ,
418
- FALSE,
419
- DUPLICATE_SAME_ACCESS ))
420
- {
421
- CloseHandle (childstdoutrd );
422
- CloseHandle (childstdoutwr );
423
- return NULL ;
424
- }
425
-
426
- CloseHandle (childstdoutrd );
427
-
428
- ZeroMemory (& pi , sizeof (pi ));
429
- ZeroMemory (& si , sizeof (si ));
430
- si .cb = sizeof (si );
431
- si .dwFlags = STARTF_USESTDHANDLES ;
432
- si .hStdError = childstdoutwr ;
433
- si .hStdOutput = childstdoutwr ;
434
- si .hStdInput = INVALID_HANDLE_VALUE ;
435
-
436
- if (CreateProcess (NULL ,
437
- cmd ,
438
- NULL ,
439
- NULL ,
440
- TRUE,
441
- 0 ,
442
- NULL ,
443
- NULL ,
444
- & si ,
445
- & pi ))
446
- {
447
- /* Successfully started the process */
448
- char * lineptr ;
449
-
450
- ZeroMemory (line , maxsize );
451
-
452
- /* Try to read at least one line from the pipe */
453
- /* This may require more than one wait/read attempt */
454
- for (lineptr = line ; lineptr < line + maxsize - 1 ;)
455
- {
456
- DWORD bytesread = 0 ;
457
-
458
- /* Let's see if we can read */
459
- if (WaitForSingleObject (childstdoutrddup , 10000 ) != WAIT_OBJECT_0 )
460
- break ; /* Timeout, but perhaps we got a line already */
461
-
462
- if (!ReadFile (childstdoutrddup , lineptr , maxsize - (lineptr - line ),
463
- & bytesread , NULL ))
464
- break ; /* Error, but perhaps we got a line already */
465
-
466
- lineptr += strlen (lineptr );
467
-
468
- if (!bytesread )
469
- break ; /* EOF */
470
-
471
- if (strchr (line , '\n' ))
472
- break ; /* One or more lines read */
473
- }
474
-
475
- if (lineptr != line )
476
- {
477
- /* OK, we read some data */
478
- int len ;
479
-
480
- /* If we got more than one line, cut off after the first \n */
481
- lineptr = strchr (line , '\n' );
482
- if (lineptr )
483
- * (lineptr + 1 ) = '\0' ;
484
-
485
- len = strlen (line );
486
-
487
- /*
488
- * If EOL is \r\n, convert to just \n. Because stdout is a
489
- * text-mode stream, the \n output by the child process is
490
- * received as \r\n, so we convert it to \n. The server main.c
491
- * sets setvbuf(stdout, NULL, _IONBF, 0) which has the effect of
492
- * disabling \n to \r\n expansion for stdout.
493
- */
494
- if (len >= 2 && line [len - 2 ] == '\r' && line [len - 1 ] == '\n' )
495
- {
496
- line [len - 2 ] = '\n' ;
497
- line [len - 1 ] = '\0' ;
498
- len -- ;
499
- }
500
-
501
- /*
502
- * We emulate fgets() behaviour. So if there is no newline at the
503
- * end, we add one...
504
- */
505
- if (len == 0 || line [len - 1 ] != '\n' )
506
- strcat (line , "\n" );
507
-
508
- retval = line ;
509
- }
510
-
511
- CloseHandle (pi .hProcess );
512
- CloseHandle (pi .hThread );
513
- }
514
-
515
- CloseHandle (childstdoutwr );
516
- CloseHandle (childstdoutrddup );
517
-
518
- return retval ;
519
- #endif /* WIN32 */
520
390
}
521
391
522
392
0 commit comments