99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/port/exec.c,v 1.33 2004/11/27 22:44:15 petere Exp $
12+ * $PostgreSQL: pgsql/src/port/exec.c,v 1.34 2004/12/20 17:40:59 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -486,30 +486,45 @@ pipe_read_line(char *cmd, char *line, int maxsize)
486486& si ,
487487& pi ))
488488{
489- DWORD bytesread = 0 ;
490-
491489/* Successfully started the process */
490+ char * lineptr ;
492491
493492ZeroMemory (line ,maxsize );
494493
495- /* Let's see if we can read */
496- if (WaitForSingleObject (childstdoutrddup ,10000 )!= WAIT_OBJECT_0 )
494+ /* Try to read at least one line from the pipe */
495+ /* This may require more than one wait/read attempt */
496+ for (lineptr = line ;lineptr < line + maxsize - 1 ; )
497497{
498- /* Got timeout */
499- CloseHandle (pi .hProcess );
500- CloseHandle (pi .hThread );
501- CloseHandle (childstdoutwr );
502- CloseHandle (childstdoutrddup );
503- return NULL ;
498+ DWORD bytesread = 0 ;
499+
500+ /* Let's see if we can read */
501+ if (WaitForSingleObject (childstdoutrddup ,10000 )!= WAIT_OBJECT_0 )
502+ break ;/* Timeout, but perhaps we got a line already */
503+
504+ if (!ReadFile (childstdoutrddup ,lineptr ,maxsize - (lineptr - line ),
505+ & bytesread ,NULL ))
506+ break ;/* Error, but perhaps we got a line already */
507+
508+ lineptr += strlen (lineptr );
509+
510+ if (!bytesread )
511+ break ;/* EOF */
512+
513+ if (strchr (line ,'\n' ))
514+ break ;/* One or more lines read */
504515}
505516
506- /* We try just once */
507- if (ReadFile (childstdoutrddup ,line ,maxsize ,& bytesread ,NULL )&&
508- bytesread > 0 )
517+ if (lineptr != line )
509518{
510- /* So we read some data */
511- int len = strlen (line );
512- retval = line ;
519+ /* OK, we read some data */
520+ int len ;
521+
522+ /* If we got more than one line, cut off after the first \n */
523+ lineptr = strchr (line ,'\n' );
524+ if (lineptr )
525+ * (lineptr + 1 )= '\0' ;
526+
527+ len = strlen (line );
513528
514529/*
515530 * If EOL is \r\n, convert to just \n. Because stdout is a
@@ -531,6 +546,8 @@ pipe_read_line(char *cmd, char *line, int maxsize)
531546 */
532547if (len == 0 || line [len - 1 ]!= '\n' )
533548strcat (line ,"\n" );
549+
550+ retval = line ;
534551}
535552
536553CloseHandle (pi .hProcess );