Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!vrdxhq!BMS-AT!stuart From: stuart@BMS-AT.UUCP (Stuart D. Gathman) Newsgroups: net.unix Subject: Re: Dissimilar Program Interaction? Message-ID: <234@BMS-AT.UUCP> Date: Fri, 3-Oct-86 16:20:38 EDT Article-I.D.: BMS-AT.234 Posted: Fri Oct 3 16:20:38 1986 Date-Received: Tue, 7-Oct-86 20:05:24 EDT References: <19300054@uiucdcsb> Organization: Business Management Systems, Inc., Fairfax, VA Lines: 60 Summary: Help with pipes In article <19300054@uiucdcsb>, kadie@uiucdcsb.cs.uiuc.edu writes: > Looking through the UNIX library I found a function that > does almost what I want, namely popen. As in: > FILE *pstrm; > pstrm = popen("cat >response","w") > This will start cat running such that the executive can > make stream writes (fprint) to it. The problem is > that I want to both fscan and fprint with each subprocesses. > An alternative function is the pipe command. It allows > read and write. Unfortunately I don't know how to > connect the pipe(s) to standard input and standard output > of the sub-program. /* coming right up . . . */ int twoway_pipe(p,cmd) int *p; char *cmd; { int pr[2], pw[2], pid; if (pipe(pr)) return -1; if (pipe(pw)) { close(pr[0]); close(pr[1]); return -1; } pid = fork(); if (pid<0) return -1; if (pid) { /* parent */ close(pr[1]); close(pw[0]); p[0] = pr[0]; p[1] = pw[1]; return 0; } close(0); close(1); dup(pw[0]); dup(pr[1]); /* define stdin & stdout */ /* you might want to close other (all>2) files here */ close(pr[0]); close(pw[1]); /* close original pipe fd's */ close(pr[1]); close(pw[0]); execlp("/bin/sh","sh","-c",cmd,0); return -1; } > Also this does not permit fscan and fprint. { FILE *in, *out; int p[2]; if (twoway_pipe(p,"mycommand")) perror("makepipe"); rdstrm = fdopen(p[0],"r"); /* make fd's into streams */ wrstrm = fdopen(p[1],"w"); . . . } P.S. I can't stand fscan. I can't stand null terminated strings either. Fortunately 'C' doesn't force you to use them. -- Stuart D. Gathman <..!seismo!{vrdxhq|dgis}!BMS-AT!stuart>