| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | redirect stdin and stdout |
Hello, All!
I want to redirect both stdin and stdout when spawning another program. I
have some examples using the OS/2 API calls, but I want to avoid that if it
is possible and only use the C library to stay portable. The program should
also compile on AIX and IRIX (at least I hope so).
This is just a small test program; the "production" thing will
have to redirect sockets.
I can write to the pipe that is connected to stdin of the child process,
but I don't get anything back from stdout.
Where is my error?
=== Cut ===
#define INCL_DOSPROCESS
#include
#include
#include
#include
#include
#include
#include
#include
#define STDIN 0
#define STDOUT 1
int savein,saveout,rc,status,arg;
int inpipe[2],outpipe[2];
static void redir_init(void) {
int rc;
savein=dup(STDIN); close(STDIN);
saveout=dup(STDOUT); close(STDOUT);
rc=pipe(inpipe); /* 0=r 1=w */
rc=pipe(outpipe);
rc=dup2(inpipe[0], STDIN); close(inpipe[0]);
rc=dup2(outpipe[1],STDOUT); close(outpipe[1]);
}
static void redir_restore(void) {
int rc;
close(STDIN); rc=dup2(savein, STDIN); close(savein);
close(STDOUT); rc=dup2(saveout,STDOUT); close(saveout);
}
static void redir_end(void) {
close(inpipe[1]);
close(outpipe[0]);
}
#ifdef TEST
int main(int argc, char **argv) {
char buf[1024];
if (argc<3)
return 1;
puts(argv[1]);
puts(argv[2]);
remove(argv[2]);
redir_init();
rc=spawnlp(P_NOWAIT,"upper.exe","upper.exe","hallo",(char
*)NULL);
redir_restore();
printf("pid=%d\n",rc);
/* write to inpipe[0] */
/* read from outpipe[1] */
arg=open(argv[1],O_RDONLY);
do {
rc=read(arg,buf,sizeof(buf));
if (rc>0)
write(inpipe[1],buf,rc);
} while (rc>0);
close(arg);
DosSleep(1000);
arg=open(argv[2],O_WRONLY|O_CREAT,S_IREAD|S_IWRITE);
do {
rc=read(outpipe[0],buf,sizeof(buf));
if (rc>0)
write(arg,buf,rc);
} while (rc>0);
close(arg);
redir_end();
rc=wait(&status);
printf("pid=%d, rc %d (%0x)\n",rc,status,status);
return 0;
}
#endif
=== Cut ===
"upper.exe" just outputs all its input in upper case and counts chars.
=== Cut ===
#include
#include
#include
int main(int argc, char **argv) {
char t[1024],*u=NULL;
int c=0;
if (argc>1)
puts(argv[1]);
do {
if (!(feof(stdin) || ferror(stdin)))
u=fgets(t,sizeof(t),stdin);
if (u) {
for ( ; *u; ++u) {
if (islower(*u))
putchar(toupper(*u));
else
putchar(*u);
++c;
}
}
} while (u);
return 0;
}
=== Cut ===
Tschau...Thomas
--- E3-32/1.11-32/2.50+
* Origin: Die TeX-Box +49-6034-1455 V.34 -930022 ISDN 24h (2:244/1130.42)SEEN-BY: 50/99 78/0 270/101 620/243 711/401 409 410 413 430 808 809 934 955 SEEN-BY: 712/407 515 517 628 713/888 800/1 7877/2809 @PATH: 244/1130 24/999 2/777 396/1 270/101 712/515 711/808 809 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™.