TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Rob Hamerling
from: Thomas Seeling
date: 1997-01-10 21:55:50
subject: `temp` redirect

Hallo, Rob!

*** Am 07. Januar 1997 um 22:31 schrieb Rob Hamerling an All:

 RH> From my own program I want to spawn() another program but don't want to
 RH> see its stdout (and stderr) output. I understand that with freopen() it is
 RH> possible to have output redirected. Should I use "NUL" as
filename to dump
 RH> the output? Can I use freopen("NUL",...) for stdout and stderr
 RH> simultaneously? After returning from the spawned program I want to resume
 RH> normal stdout handling as it was before the spawn(). How is that possible?
 RH> Should I close the file pointer I got from freopen()?

A simple way is to use system() and give a redirection command with it. Of
course you then have to make sure that you escape the "dangerous"
characters like &, ^ etc. so that if a user provides parameters he
cannot do any destructive actions (imagine your spawned program takes
arguments, and a user might send "x & del C:\* C:\os2\*" to
it -- in this case your program gets "x" as argument, plus the
shell gifts you with some more hard disk space).

sprintf(cmd,"%s %s >nul 2>nul",command,arguments);
rc=system(cmd);

If you want to catch the spawned program's output, and perhaps even send
input to its stdin, use something like this (this is a working sample from
my WWW server, where it is used for CGI scripts). It uses some functions
specific to emx+gcc, like _getext(), but you will manage this :-)

=== Cut ===

PID startpgm(int flags, char *prg, char *arg, char *env,
             HFILE *phr, HFILE *phw) {
  HFILE       OpipeR,OpipeW, IpipeR,IpipeW,
              SaveI,SaveO,SaveE, NewI,NewO,NewE;
  RESULTCODES ChildRC = {0};
  APIRET      rc;
  char       *myarg=arg;
  PSZ         myenv=(PSZ)"PATH_INFO=/var/spool/test\0\0";
  char        LoadError[MAXPATHLEN+1] = {'\0'},*ext;
  int         doshell=FALSE;

  if (env)
    myenv=(PSZ)env;

  ext=_getext(prg);
  if (!ext || (stricmp(ext,".exe")!=0 &&
stricmp(ext,".com")!=0)) {
    pathos2(prg);
    ext=1+myarg+sprintf(myarg,"%s",os2shell);
    ext+=sprintf(ext,"%s %s ",os2call,prg);
    *--ext='\0';
    log_http("exec %s %s %s",
      os2shell,os2call,prg);
    doshell=TRUE;
  }

  SaveI= -1;
  SaveO= -1;
  SaveE= -1;
  NewI = HF_STDIN;
  NewO = HF_STDOUT;
  NewE = HF_STDERR;

  rc = DosDupHandle(HF_STDOUT, &SaveO);
  rc = DosDupHandle(HF_STDERR, &SaveE);
  rc = DosCreatePipe(&OpipeR, &OpipeW, PIPE_SIZE);
  rc = DosDupHandle(OpipeW, &NewO);
  rc = DosDupHandle(OpipeW, &NewE);

  rc = DosDupHandle(HF_STDIN,  &SaveI);
  rc = DosCreatePipe(&IpipeR, &IpipeW, PIPE_SIZE);
  rc = DosDupHandle(IpipeR, &NewI);

  rc = DosExecPgm(LoadError,sizeof(LoadError),
                  flags,(PSZ)myarg,myenv,&ChildRC,
                  doshell ? (PSZ) os2shell : (PSZ)prg);

  rc = DosClose(OpipeW);
  rc = DosDupHandle(SaveO, &NewO);
  rc = DosDupHandle(SaveE, &NewE);
  rc = DosClose(SaveO);

  rc = DosClose(IpipeR);
  rc = DosDupHandle(SaveI, &NewI);
  rc = DosClose(SaveI);

  *phr=OpipeR;
  *phw=IpipeW;
  return ChildRC.codeTerminate;
}

/* you use it this way */
      if (access(runcgi,0)==0) {
         HFILE       rhandle=-1,whandle=-1;
         RESULTCODES ChildRC;
         APIRET      rc=0;
         PID         pid;
         ULONG       tmp=0,wr=0;
         char        datebuff[DATE_SIZE];
         int         nooutput=TRUE,first=TRUE;

         log_http("cgi spawn it");
         pid=startpgm(EXEC_ASYNCRESULT,runcgi,cgiargv,cgienv,
                      &rhandle,&whandle);

/* here you can use rhandle to read from the program's stdout
   and whandle to write to its stdin */

         DosClose(rhandle);
         DosClose(whandle);
      }
      DosWaitChild(DCWA_PROCESS,DCWW_WAIT,&ChildRC,&tmp,pid);
      log_http("cgi DosWaitChild PID %lu",tmp);
      if (nooutput) {
       log_http("cgi no output from cgi");
       outerror(mysock,http1,err_badcgi);
       log_http("204 no cgi output");
       diderr=TRUE;
     }
     if (ChildRC.codeResult) {
       log_http("cgi reason=%lu rc=%lu",
       ChildRC.codeTerminate,ChildRC.codeResult);
       lg=-1;
     }
=== Cut ===


Tschau...Thomas

--- E3-32/1.11-32/2.51+
* Origin: Die TeX-Box +49-6034-930021 V.34 -930022 ISDN 24h (2:244/1130.42)
SEEN-BY: 50/99 54/99 270/101 620/243 625/0 160 711/401 409 410 413 430 808
SEEN-BY: 711/809 934 955 712/311 407 505 506 517 623 624 704 841 713/317
SEEN-BY: 800/1
@PATH: 244/1130 1634 24/888 396/1 270/101 712/624 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.