.TH popen() "" "" "STDIO Function (libc)"
.PC "Open a pipe"
.B "#include <stdio.h>"
\fBFILE *popen(\fIcommand, how\^\fB)\fR
\fBchar *\fIcommand, \fB*\fIhow\^\fB;\fR
.PP
.B popen()
opens a pipe.
It resembles the function
.BR fopen() ,
except that the opened object is a command line to the shell
.B sh
rather than a file.
.PP
The caller can
read the standard output of
.I command
when
.I how
is \fBr\fP,
or write to the standard input of
.I command
when
.I how
is \fBw\fP.
.B popen()
returns a pointer to a \fBFILE\fR structure that may be read or written.
.SH Example
This example is equivalent to the command
.DM
ls -l | mail me
.DE
where
.B me
is your login identifier.
.DM
#include <stdio.h>
main()
{
	FILE *ifp, *ofp;
	int c;
.DE
.DM
	if ((NULL == (ofp = popen("lmail me", "w"))) ||
	    (NULL == (ifp = popen("ls -l",     "r")))) {
	    	fprintf(stderr, "cannot popen\en");
		exit(1);
	}
.DE
.DM
	while (EOF != (c = fgetc(ifp)))
		fputc(c, ofp);
.DE
.DM
	pclose(ifp);
	pclose(ofp);
}
.DE
.SH Files
.B <stdio.h>
.SH "See Also"
.Xr "fclose()," fclose
.Xr "fopen()," fopen
.Xr "libc," libc
.Xr "pclose()," pclose
.Xr "pipe()," pipe.s
.Xr "sh," sh
.Xr "system()," system
.Xr "wait()" wait.s
.SH Diagnostics
.B popen()
returns NULL if the link to
.I command
could not be established.
