KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q38308: Spawn Will Not Pass Redirection to Child

Article: Q38308
Product(s): See article
Version(s): 5.10
Operating System(s): MS-DOS
Keyword(s): ENDUSER | SR# S880925-1 | mspl13_c
Last Modified: 12-DEC-1988

Question:

When using the program below to spawn sort.exe, it seems that "sort"
starts, but then sits there doing nothing. The behavior is similar to
starting "sort" from the DOS prompt without any arguments.

The following program demonstrates the problem:

#include <stdio.h>
#include <process.h>
main()
{
        char *args[6] ;
        args[0] = "sort" ;
        args[1] = "<" ;
        args[2] = "infile.dat" ; /* exists */
        args[3] = ">" ;         /* direct output to a disk file */
        args[4] = "outfile.dat" ;
        args[5] = NULL ;

        spawnvp (P_WAIT, "sort.exe", args) ;
}

Why doesn't this work correctly? What can I do about it?

Response:

Because COMMAND.COM, and NOT the EXEC loader, handles indirection,
this is the expected behavior. Child processes inherit the handles of
their parents; therefore, to redirect the input and output of the
child you first change the definitions of STDIN and STDOUT in the
parent process. The proper way to redirect input for a filter is
described starting on Page 441 of the "MS-DOS Encyclopedia," along with
a complete MASM example. Note: the dup and dup2 functions in the C
Version 5.10 run-time library are the same as the INT 21h functions 45h
and 46h, respectively.

There also are partial examples of this technique on Page 230 of the
"Microsoft C Version 5.10 Run-Time Library Reference" manual and on
Page 353 of "Advanced MS-DOS Programming" (by Duncan, published by
Microsoft Press).

It is possible to use freopen() to redefine STDIN and STDOUT; however,
doing so loses any redirection that may have been performed on the
parent process.

The easiest workaround is to use the system function to spawn a copy
of COMMAND.COM that runs SORT.EXE, as follows:

   system("sort.exe <infile.dat >outfile.dat");

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.