'\" te .\" Copyright (c) 1996, Sun Microsystems, Inc. All Rights Reserved. .\" Copyright 1989 AT&T .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] .TH P2OPEN 3GEN "Dec 29, 1996" .SH NAME p2open, p2close \- open, close pipes to and from a command .SH SYNOPSIS .LP .nf cc [ \fIflag\fR ... ] \fIfile\fR ... \fB-lgen\fR [ \fIlibrary\fR ... ] #include \fBint\fR \fBp2open\fR(\fBconst char *\fR\fIcmd\fR, \fBFILE *\fR\fIfp\fR[2]); .fi .LP .nf \fBint\fR \fBp2close\fR(\fBFILE *\fR\fIfp\fR[2]); .fi .SH DESCRIPTION .sp .LP The \fBp2open()\fRgfunction forks and execs a shell running the command line pointed to by \fIcmd\fR. On return, \fBfp[0]\fR points to a \fBFILE\fR pointer to write the command's standard input and \fBfp[1]\fR points to a \fBFILE\fR pointer to read from the command's standard output. In this way the program has control over the input and output of the command. .sp .LP The function returns \fB0\fR if successful; otherwise, it returns \fB\(mi1\fR\&. .sp .LP The \fBp2close()\fR function is used to close the file pointers that \fBp2open()\fR opened. It waits for the process to terminate and returns the process status. It returns \fB0\fR if successful; otherwise, it returns \fB\(mi1\fR\&. .SH RETURN VALUES .sp .LP A common problem is having too few file descriptors. The \fBp2close()\fR function returns \fB\(mi1\fR if the two file pointers are not from the same \fBp2open()\fR. .SH EXAMPLES .LP \fBExample 1 \fRExample of file descriptors. .sp .in +2 .nf #include #include main(argc,argv) int argc; char **argv; { FILE *fp[2]; pid_t pid; char buf[16]; pid=p2open("/usr/bin/cat", fp); if ( pid == \(mi1 ) { fprintf(stderr, "p2open failed\en"); exit(1); } write(fileno(fp[0]),"This is a test\en", 16); if(read(fileno(fp[1]), buf, 16) <=0) fprintf(stderr, "p2open failed\en"); else write(1, buf, 16); (void)p2close(fp); } .fi .in -2 .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS box; c | c l | l . ATTRIBUTE TYPE ATTRIBUTE VALUE _ MT-Level Unsafe .TE .SH SEE ALSO .sp .LP \fBfclose\fR(3C), \fBpopen\fR(3C), \fBsetbuf\fR(3C), \fBattributes\fR(5) .SH NOTES .sp .LP Buffered writes on \fBfp[0]\fR can make it appear that the command is not listening. Judiciously placed \fBfflush()\fR calls or unbuffering \fBfp[0]\fR can be a big help; see \fBfclose\fR(3C). .sp .LP Many commands use buffered output when connected to a pipe. That, too, can make it appear as if things are not working. .sp .LP Usage is not the same as for \fBpopen()\fR, although it is closely related.