manual page first distributed with 4.2BSD
[unix-history] / usr / src / lib / libc / gen / popen.3
CommitLineData
80a05115 1.\" @(#)popen.3 6.1 (Berkeley) %G%
fa528602 2.\"
80a05115 3.TH POPEN 3 ""
fa528602
KM
4.AT 3
5.SH NAME
6popen, pclose \- initiate I/O to/from a process
7.SH SYNOPSIS
8.B #include <stdio.h>
9.PP
10.SM
11.B FILE
12.B *popen(command, type)
13.br
14.B char *command, *type;
15.PP
16.B pclose(stream)
17.br
18.SM
19.B FILE
20.B *stream;
21.SH DESCRIPTION
22The arguments to
23.I popen
fd07beb4
KM
24are pointers to null-terminated strings containing respectively a
25shell command line and an I/O mode, either "r" for reading or "w" for
26writing. It creates a pipe between the calling process and
27the command to be executed. The value returned is a stream pointer that
fa528602
KM
28can be used (as appropriate) to write to the standard input
29of the command or read from its standard output.
30.PP
31A stream opened by
32.I popen
33should be closed by
fd07beb4 34.IR pclose ,
fa528602
KM
35which waits for the associated process to terminate
36and returns the exit status of the command.
37.PP
fd07beb4
KM
38Because open files are shared, a type "r" command may be used as an input
39filter, and a type "w" as an output filter.
fa528602
KM
40.SH "SEE ALSO"
41pipe(2),
fd07beb4
KM
42fopen(3S),
43fclose(3S),
fa528602 44system(3),
fd07beb4
KM
45wait(2),
46sh(1)
fa528602
KM
47.SH DIAGNOSTICS
48.I Popen
fd07beb4 49returns a null pointer if files or processes cannot be created, or the shell
fa528602
KM
50cannot be accessed.
51.PP
52.I Pclose
53returns \-1 if
54.I stream
55is not associated with a `popened' command.
56.SH BUGS
57Buffered reading before opening an input filter
58may leave the standard input of that filter mispositioned.
59Similar problems with an output filter may be
fd07beb4 60forestalled by careful buffer flushing, for instance, with
fa528602
KM
61.I fflush,
62see
80a05115 63.IR fclose (3S).
fd07beb4
KM
64.LP
65.I Popen
66always calls
67.IR sh ,
68never calls
69.IR csh .