xref: /csrg-svn/lib/libc/gen/popen.3 (revision 69193)
161111Sbostic.\" Copyright (c) 1991, 1993
261111Sbostic.\"	The Regents of the University of California.  All rights reserved.
320439Smckusick.\"
448879Scael.\" %sccs.include.redist.roff%
548879Scael.\"
6*69193Smckusick.\"     @(#)popen.3	8.2 (Berkeley) 05/03/95
748879Scael.\"
848879Scael.Dd
948879Scael.Dt POPEN 3
1048879Scael.Os
1148879Scael.Sh NAME
1248879Scael.Nm popen ,
1348879Scael.Nm pclose
1448879Scael.Nd process
1548879Scael.Tn I/O
1648879Scael.Sh SYNOPSIS
1748879Scael.Fd #include <stdio.h>
1848879Scael.Ft FILE *
1948879Scael.Fn popen "const char *command" "const char *type"
2048879Scael.Ft int
2148879Scael.Fn pclose "FILE *stream"
2248879Scael.Sh DESCRIPTION
2348879ScaelThe
2448879Scael.Fn popen
2548879Scaelfunction
2648879Scael.Dq opens
27*69193Smckusicka process by creating an IPC connection,
2848879Scaelforking,
2948879Scaeland invoking the shell.
30*69193SmckusickHistorically,
31*69193Smckusick.Nm popen
32*69193Smckusickwas implemented with a unidirectional pipe;
33*69193Smckusickhence many implementations of
34*69193Smckusick.Nm popen
35*69193Smckusickonly allow the
3648879Scael.Fa type
37*69193Smckusickargument to specify reading or writing, not both.
38*69193SmckusickSince
39*69193Smckusick.Nm popen
40*69193Smckusickis now implemented using sockets, the
41*69193Smckusick.Fa type
42*69193Smckusickmay request a bidirectional data flow.
43*69193SmckusickThe
44*69193Smckusick.Fa type
45*69193Smckusickargument is a pointer to a null-terminated string
46*69193Smckusickwhich must be
47*69193Smckusick.Ql r
48*69193Smckusickfor reading,
49*69193Smckusick.Ql w
50*69193Smckusickfor writing, or
51*69193Smckusick.Ql r+
52*69193Smckusickfor reading and writing.
5348879Scael.Pp
5448879ScaelThe
5548879Scael.Fa command
5648879Scaelargument is a pointer to a null-terminated string
5748879Scaelcontaining a shell command line.
5848879ScaelThis command is passed to
5948879Scael.Pa /bin/sh
6048879Scaelusing the
6148879Scael.Fl c
6248879Scaelflag; interpretation, if any, is performed by the shell.
6348879Scael.Pp
6448879ScaelThe return value from
6548879Scael.Fn popen
6648879Scaelis a normal standard
6748879Scael.Tn I/O
6848879Scaelstream in all respects
6948879Scaelsave that it must be closed with
7048879Scael.Fn pclose
7148879Scaelrather than
7248879Scael.Fn fclose .
7348879ScaelWriting to such a stream
7448879Scaelwrites to the standard input of the command;
7548879Scaelthe command's standard output is the same as that of the process that called
7648879Scael.Fn popen ,
7748879Scaelunless this is altered by the command itself.
7848879ScaelConversely, reading from a
7948879Scael.Dq popened
8048879Scaelstream reads the command's standard output, and
8148879Scaelthe command's standard input is the same as that of the process that called
8248879Scael.Fn popen .
8348879Scael.Pp
8448879ScaelNote that output
8548879Scael.Fn popen
8648879Scaelstreams are fully buffered by default.
8748879Scael.Pp
8848879ScaelThe
8948879Scael.Fn pclose
9048879Scaelfunction waits for the associated process to terminate
9148879Scaeland returns the exit status of the command
9248879Scaelas returned by
9348879Scael.Fn wait4 .
9448879Scael.Sh RETURN VALUE
9548879ScaelThe
9648879Scael.Fn popen
9748879Scaelfunction returns
9848879Scael.Dv NULL
9948879Scaelif the
100*69193Smckusick.Xr fork 2 ,
101*69193Smckusick.Xr pipe 2 ,
10248879Scaelor
103*69193Smckusick.Xr socketpair 2
10448879Scaelcalls fail,
10548879Scaelor if it cannot allocate memory.
10648879Scael.Pp
10748879ScaelThe
10848879Scael.Fn pclose
10948879Scaelfunction
11020439Smckusickreturns \-1 if
11148879Scael.Fa stream
11248879Scaelis not associated with a
11348879Scael.Dq popened
11448879Scaelcommand, if
11548879Scael.Fa stream
11648879Scaelalready
11748879Scael.Dq pclosed ,
11848879Scaelor if
11948879Scael.Xr wait4
12036310Sbosticreturns an error.
12148879Scael.Sh ERRORS
12248879ScaelThe
12348879Scael.Fn popen
12448879Scaelfunction does not reliably set
12548879Scael.Va errno .
12648879Scael.Sh SEE ALSO
12748879Scael.Xr fork 2 ,
12848879Scael.Xr sh 1 ,
12948879Scael.Xr pipe 2 ,
130*69193Smckusick.Xr socketpair 2 ,
13148879Scael.Xr wait4 2 ,
13248879Scael.Xr fflush 3 ,
13348879Scael.Xr fclose 3 ,
13448879Scael.Xr fopen 3 ,
13548879Scael.Xr stdio 3 ,
13648879Scael.Xr system 3
13748879Scael.Sh BUGS
13848879ScaelSince the standard input of a command opened for reading
13948879Scaelshares its seek offset with the process that called
14048879Scael.Fn popen ,
14148879Scaelif the original process has done a buffered read,
14248879Scaelthe command's input position may not be as expected.
14348879ScaelSimilarly, the output from a command opened for writing
14448879Scaelmay become intermingled with that of the original process.
14548879ScaelThe latter can be avoided by calling
14648879Scael.Xr fflush 3
14748879Scaelbefore
14848879Scael.Fn popen .
14948879Scael.Pp
15048879ScaelFailure to execute the shell
15148879Scaelis indistinguishable from the shell's failure to execute command,
15248879Scaelor an immediate exit of the command.
15348879ScaelThe only hint is an exit status of 127.
15448879Scael.Pp
15548879ScaelThe
15648879Scael.Fn popen
15748879Scaelargument
15820440Smckusickalways calls
15948879Scael.Xr sh ,
16020440Smckusicknever calls
16148879Scael.Xr csh .
16248879Scael.Sh HISTORY
16348879ScaelA
16448879Scael.Fn popen
16548879Scaeland a
16648879Scael.Fn pclose
16748879Scaelfunction appeared in
16848879Scael.At v7 .
169