36a37,38
>     int have_stdin, have_stdout;
>     int stdin_fd, stdout_fd;
69c71
<     
---
>      
152,153c154,157
<     if ( (stdin_orig  = _dup(_fileno(stdin ))) == -1  ||
<          (stdout_orig = _dup(_fileno(stdout))) == -1 ) 
---
> 
>     have_stdin = have_stdout = 1;
> 
>     if ( _fileno(stdin) == -1 ) 
155,156c159,160
<         db_syserror ("can't duplicate stdin/stdout");
< 	return (dbDriver *) NULL;
---
>         have_stdin = 0;
>         stdin_fd = 0; 
157a162,164
>     else
>     {
>         stdin_fd = _fileno(stdin);
159,160c166,174
<     if ( _dup2(p1[0], _fileno(stdin)) != 0 ||
<          _dup2(p2[1], _fileno(stdout)) != 0 ) 
---
>         if ( (stdin_orig  = _dup(_fileno(stdin ))) == -1  ) 
>         {
>             db_syserror ("can't duplicate stdin");
> 	    return (dbDriver *) NULL;
> 	}
>     
>     }
> 
>     if ( _dup2(p1[0], stdin_fd) != 0 )
162,163c176,200
<         db_syserror ("can't duplicate pipes");
< 	return (dbDriver *) NULL;
---
>         db_syserror ("can't duplicate pipe");
>         return (dbDriver *) NULL;
>     }
> 
>     if ( _fileno(stdout) == -1 ) 
>     {
>         have_stdout = 0;
>         stdout_fd = 1; 
>     }
>     else
>     {
>         stdout_fd = _fileno(stdout);
> 
>         if ( (stdout_orig  = _dup(_fileno(stdout ))) == -1  ) 
>         {
>             db_syserror ("can't duplicate stdout");
> 	    return (dbDriver *) NULL;
> 	}
>     
>     }
> 
>     if ( _dup2(p2[1], stdout_fd) != 0 ) 
>     {
>         db_syserror ("can't duplicate pipe");
>         return (dbDriver *) NULL;
179c216
< 
---
>     
199,201c236,237
<     /* Reset stdin/stdout for module */
<     if ( _dup2(stdin_orig, _fileno(stdin)) != 0 ||
<          _dup2(stdout_orig, _fileno(stdout)) != 0 ) 
---
>     /* Reset stdin/stdout for module and close duplicates */
>     if ( have_stdin )
203,204c239,244
<         db_syserror ("can't reset stdin/stdout");
< 	return (dbDriver *) NULL;
---
>         if ( _dup2(stdin_orig, _fileno(stdin)) != 0 ) 
>         {
>             db_syserror ("can't reset stdin");
> 	    return (dbDriver *) NULL;
>         }
>         close ( stdin_orig );
207,209c247,256
<     /* close duplicates */
<     close ( stdin_orig );
<     close ( stdout_orig );
---
> 
>     if ( have_stdout )
>     {
>         if ( _dup2(stdout_orig, _fileno(stdout)) != 0 ) 
>         {
>             db_syserror ("can't reset stdout");
> 	    return (dbDriver *) NULL;
>         }
>         close ( stdout_orig );
>     }