xref: /illumos-gate/usr/src/cmd/refer/refer3.c (revision 2a8bcb4e)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include "refer..c"
16*11a8fa6cSceastha #define	move(x, y) close(y); dup(x); close(x);
17*11a8fa6cSceastha 
18*11a8fa6cSceastha extern void err();
197c478bd9Sstevel@tonic-gate 
20*11a8fa6cSceastha int
corout(char * in,char * out,char * rprog,char * arg,int outlen)21*11a8fa6cSceastha corout(char *in, char *out, char *rprog, char *arg, int outlen)
227c478bd9Sstevel@tonic-gate {
237c478bd9Sstevel@tonic-gate 	int pipev[2], fr1, fr2, fw1, fw2, n;
247c478bd9Sstevel@tonic-gate 	int status;
257c478bd9Sstevel@tonic-gate 
26*11a8fa6cSceastha 	pipe(pipev);
27*11a8fa6cSceastha 	fr1 = pipev[0];
287c478bd9Sstevel@tonic-gate 	fw1 = pipev[1];
29*11a8fa6cSceastha 	pipe(pipev);
30*11a8fa6cSceastha 	fr2 = pipev[0];
317c478bd9Sstevel@tonic-gate 	fw2 = pipev[1];
32*11a8fa6cSceastha 	if (fork() == 0) {
33*11a8fa6cSceastha 		close(fw1);
347c478bd9Sstevel@tonic-gate 		close(fr2);
357c478bd9Sstevel@tonic-gate 		move(fr1, 0);
367c478bd9Sstevel@tonic-gate 		move(fw2, 1);
377c478bd9Sstevel@tonic-gate 		execl(rprog, "deliv", arg, 0);
387c478bd9Sstevel@tonic-gate 		err("Can't run %s", rprog);
397c478bd9Sstevel@tonic-gate 	}
40*11a8fa6cSceastha 	close(fw2);
417c478bd9Sstevel@tonic-gate 	close(fr1);
42*11a8fa6cSceastha 	write(fw1, in, strlen(in));
437c478bd9Sstevel@tonic-gate 	close(fw1);
447c478bd9Sstevel@tonic-gate 	wait(&status);
457c478bd9Sstevel@tonic-gate 	n = read(fr2, out, outlen);
467c478bd9Sstevel@tonic-gate 	out[n] = 0;
477c478bd9Sstevel@tonic-gate 	close(fr2);
48*11a8fa6cSceastha 	return (n);
497c478bd9Sstevel@tonic-gate }
50