2 .\" Copyright (c) 1996, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" 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.
5 .\" 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.
6 .\" 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]
7 .TH P2OPEN 3GEN "Dec 29, 1996"
9 p2open, p2close \- open, close pipes to and from a command
13 cc [ \fIflag\fR ... ] \fIfile\fR ... \fB-lgen\fR [ \fIlibrary\fR ... ]
16 \fBint\fR \fBp2open\fR(\fBconst char *\fR\fIcmd\fR, \fBFILE *\fR\fIfp\fR[2]);
21 \fBint\fR \fBp2close\fR(\fBFILE *\fR\fIfp\fR[2]);
27 The \fBp2open()\fRgfunction forks and execs a shell running the command line
28 pointed to by \fIcmd\fR. On return, \fBfp[0]\fR points to a \fBFILE\fR pointer
29 to write the command's standard input and \fBfp[1]\fR points to a \fBFILE\fR
30 pointer to read from the command's standard output. In this way the program
31 has control over the input and output of the command.
34 The function returns \fB0\fR if successful; otherwise, it returns
38 The \fBp2close()\fR function is used to close the file pointers that
39 \fBp2open()\fR opened. It waits for the process to terminate and returns the
40 process status. It returns \fB0\fR if successful; otherwise, it returns
45 A common problem is having too few file descriptors. The \fBp2close()\fR
46 function returns \fB\(mi1\fR if the two file pointers are not from the same
50 \fBExample 1 \fRExample of file descriptors.
65 pid=p2open("/usr/bin/cat", fp);
67 fprintf(stderr, "p2open failed\en");
70 write(fileno(fp[0]),"This is a test\en", 16);
71 if(read(fileno(fp[1]), buf, 16) <=0)
72 fprintf(stderr, "p2open failed\en");
83 See \fBattributes\fR(5) for descriptions of the following attributes:
91 ATTRIBUTE TYPE ATTRIBUTE VALUE
99 \fBfclose\fR(3C), \fBpopen\fR(3C), \fBsetbuf\fR(3C), \fBattributes\fR(5)
103 Buffered writes on \fBfp[0]\fR can make it appear that the command is not
104 listening. Judiciously placed \fBfflush()\fR calls or unbuffering \fBfp[0]\fR
105 can be a big help; see \fBfclose\fR(3C).
108 Many commands use buffered output when connected to a pipe. That, too, can make
109 it appear as if things are not working.
112 Usage is not the same as for \fBpopen()\fR, although it is closely related.