🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.ibm.pc.programmer
Article #867

Re: Redirecting to/from spawnxx()ed program?

#867
From: ? pruss
Date: Fri, 06 Jul 1990 18:37
51 lines
2664 bytes
In article <49978@iuvax.cs.indiana.edu> bobmon@iuvax.cs.indiana.edu (RAMontante) writes:
>I believe that spawn...() doesn't start a command shell, which is what you
>need to do redirection.  There is another call, system() I think, which
>does start a command shell and feeds the arguments to it.  You could use
>that.  The arguments should be essentially a command line that is to be
>fed to command.com, but I don't have my manuals at hand to check any of this.
>
>Or you could spawn...() a command.com, and build an arg list for it that
>does what you need, but the system() call (in TC2.0) does all that for you.

No!  I agree that spawnxx() doesn't start a cmd shell;  however you
do NOT need a command shell to do redirection.  (Anything command.com
can do, in DOS 3+, a C program can do).  While it is true you can start
up command.com and feed correct arguments with '<' '>' or '>>' to it,
it is much simpler to simply redirect thru the redirect DOS call or
the dup2() function.  Below is a demo program that redirects the output
of command.com to a file.  (After arguing about the necessity of command.com,
I think I should have chosen a different program to run, but command.com
is probably the only DOS application that absolutely everyone has--or
4dos--or sh--or whatever).  If you change the spawn line, you can spawn
anything you like.

---TurboC (portable?) source---
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
/*
 * Demonstrate the use of dup() and dup2() functions to do i/o
 * redirection.  This will run $comspec with output redirected
 * to command.out
 */

#define REDIR_HANDLE  1  /* redirect stdout */

void main(void)
{
 int f_temp, f;
 char *com;
 f_temp=dup(REDIR_HANDLE);   /* REDIR_HANDLE==fileno(stdout);  a fileno() call is more portable */
 f=open("command.out",O_CREAT);
 dup2(f,REDIR_HANDLE);       /* stdout is now command.out */
/* change next 2 lines if you wish */
 com=getenv("COMSPEC");
 spawnl(P_WAIT,com,com,NULL);
/**/
 dup2(f_temp,REDIR_HANDLE);  /* restore old handle */
 close(f);
}
---demo src ends---
Technical Details
Message-ID: <597@ria.ccs.uwo.ca>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watadm1!ria!pruss
References: <49978@iuvax.cs.indiana.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watadm1!ria!pruss
From: pruss@ria.ccs.uwo.ca (? pruss)
Newsgroups: comp.sys.ibm.pc.programmer,alt.msdos.programmer
Subject: Re: Redirecting to/from spawnxx()ed program?
Summary: You don't need command.com.
Keywords: redirect stdin/stdout command.com shell
Message-ID: <597@ria.ccs.uwo.ca>
Date: 6 Jul 90 18:37:56 GMT
References: <49978@iuvax.cs.indiana.edu>
Followup-To: comp.sys.ibm.pc.programmer
Organization: Applied Mathematics, University of Western Ontario, London
Lines: 51