'\"macro stdmacro
.if n .pH g3g.p2open @(#)p2open	40.9 of 10/27/89
.\" Copyright 1989 AT&T
'''	ident	"@(#)libgen:doc/p2open.3G	1.1"
.nr X
.if \nX=0 .ds x} p2open 3G "" "\&"
.if \nX=1 .ds x} p2open 3G ""
.if \nX=2 .ds x} p2open 3G "" "\&"
.if \nX=3 .ds x} p2open "" "" "\&"
.TH \*(x}
.SH "NAME"
\f4p2open\f1, \f4p2close\f1 \- open, close pipes to and from a command
.SH "SYNOPSIS"
\f4cc\f1
[\f2flag\fP \|.\|.\|.] \f2file\fP \|.\|.\|.
\f4\-lgen\f1
[\f2library\fP \|.\|.\|.]
.PP
\f4#include <libgen.h>\f1
.PP
\f4int p2open (const char \(**cmd, FILE \(**fp[2]);\f1
.PP
\f4int p2close (FILE \(**fp[2]);\f1
.SH DESCRIPTION
\f4p2open\fP forks and execs a shell running the command line
pointed to by \f2cmd\f1.
On return, \f4fp[0]\f1 points to a \f4FILE\fP
pointer to write the command's standard input and \f4fp[1]\f1 
points to a \f4FILE\fP pointer to read from the command's 
standard output.  In this way the program has control over the
input and output of the command. 
.PP
The function returns 0
if successful; otherwise it returns \-1.
.PP
\f4p2close\f1 is used to close the file pointers that
\f4p2open\fP opened.  It waits for the process to terminate
and returns the process status.
It returns 0 if successful; otherwise it returns \-1.
.SH EXAMPLES
.ft 4
.nf
#include <stdio.h>
#include <libgen.h>
.sp 0.5
main(argc,argv)
int argc;
char \(**\(**argv;
{
	FILE \(**fp[2];
	pid_t pid;
	char buf[16];
.sp 0.5
	pid=p2open("/usr/bin/cat", fp);
	if ( pid =\^= 0 ) {
		fprintf(stderr, "p2open failed\\n");
		exit(1);
	}
	write(fileno(fp[0]),"This is a test\\n", 16);
	if(read(fileno(fp[1]), buf, 16) <=0)
		fprintf(stderr, "p2open failed\\n");
	else
		write(1, buf, 16);
	(void)p2close(fp);
}
.fi
.ft 1
.SH "SEE ALSO"
\f4fclose\fP(3S), \f4popen\fP(3S), \f4setbuf\fP(3S).
.SH DIAGNOSTICS
A common problem is having too few file descriptors.
\f4p2close\f1 returns \-1 if the two file pointers are not
from the same \f4p2open\fP.
.SH NOTES
Buffered writes on
\f4fp[0]\f1
can make it appear that the command is not listening.
Judiciously placed \f4fflush\fP calls or unbuffering
\f4fp[0]\f1 can be a big help; see \f4fclose\fP(3S).
.PP
Many commands use buffered output when connected to a pipe.
That, too, can make it appear as if things are not working.
.PP
Usage is not the same as for
\f4popen\fP,
although it is closely related.
