1POPEN(3)                   Linux Programmer's Manual                  POPEN(3)
2
3
4

NAME

6       popen, pclose - pipe stream to or from a process
7

SYNOPSIS

9       #include <stdio.h>
10
11       FILE *popen(const char *command, const char *type);
12       int pclose(FILE *stream);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       popen(), pclose():
17           _POSIX_C_SOURCE >= 2
18               || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
19

DESCRIPTION

21       The  popen()  function opens a process by creating a pipe, forking, and
22       invoking the shell.  Since a pipe is by definition unidirectional,  the
23       type  argument  may  specify only reading or writing, not both; the re‐
24       sulting stream is correspondingly read-only or write-only.
25
26       The command argument is a pointer to a null-terminated string  contain‐
27       ing  a shell command line.  This command is passed to /bin/sh using the
28       -c flag; interpretation, if any, is performed by the shell.
29
30       The type argument is a pointer to a null-terminated string  which  must
31       contain  either  the letter 'r' for reading or the letter 'w' for writ‐
32       ing.  Since glibc 2.9, this argument can additionally include the  let‐
33       ter  'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on
34       the underlying file descriptor; see the description  of  the  O_CLOEXEC
35       flag in open(2) for reasons why this may be useful.
36
37       The  return  value  from popen() is a normal standard I/O stream in all
38       respects save  that  it  must  be  closed  with  pclose()  rather  than
39       fclose(3).   Writing  to  such a stream writes to the standard input of
40       the command; the command's standard output is the same as that  of  the
41       process  that called popen(), unless this is altered by the command it‐
42       self.  Conversely, reading from the stream reads the command's standard
43       output,  and  the  command's  standard input is the same as that of the
44       process that called popen().
45
46       Note that output popen() streams are block buffered by default.
47
48       The pclose() function waits for the associated process to terminate and
49       returns the exit status of the command as returned by wait4(2).
50

RETURN VALUE

52       popen():  on  success,  returns a pointer to an open stream that can be
53       used to read or write to the pipe; if  the  fork(2)  or  pipe(2)  calls
54       fail, or if the function cannot allocate memory, NULL is returned.
55
56       pclose():  on  success,  returns  the  exit  status  of the command; if
57       wait4(2) returns an error, or some other error is detected, -1  is  re‐
58       turned.
59
60       On failure, both functions set errno to indicate the error.
61

ERRORS

63       The popen() function does not set errno if memory allocation fails.  If
64       the underlying fork(2) or pipe(2) fails, errno is set to  indicate  the
65       error.   If  the  type  argument  is invalid, and this condition is de‐
66       tected, errno is set to EINVAL.
67
68       If pclose() cannot obtain the child status, errno is set to ECHILD.
69

ATTRIBUTES

71       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
72       tributes(7).
73
74       ┌────────────────────────────────────────────┬───────────────┬─────────┐
75Interface                                   Attribute     Value   
76       ├────────────────────────────────────────────┼───────────────┼─────────┤
77popen(), pclose()                           │ Thread safety │ MT-Safe │
78       └────────────────────────────────────────────┴───────────────┴─────────┘
79

CONFORMING TO

81       POSIX.1-2001, POSIX.1-2008.
82
83       The 'e' value for type is a Linux extension.
84

NOTES

86       Note: carefully read Caveats in system(3).
87

BUGS

89       Since  the  standard  input  of a command opened for reading shares its
90       seek offset with the process  that  called  popen(),  if  the  original
91       process  has done a buffered read, the command's input position may not
92       be as expected.  Similarly, the output from a command opened for  writ‐
93       ing  may  become  intermingled  with that of the original process.  The
94       latter can be avoided by calling fflush(3) before popen().
95
96       Failure to execute the shell  is  indistinguishable  from  the  shell's
97       failure  to  execute command, or an immediate exit of the command.  The
98       only hint is an exit status of 127.
99

SEE ALSO

101       sh(1), fork(2),  pipe(2),  wait4(2),  fclose(3),  fflush(3),  fopen(3),
102       stdio(3), system(3)
103

COLOPHON

105       This  page  is  part of release 5.13 of the Linux man-pages project.  A
106       description of the project, information about reporting bugs,  and  the
107       latest     version     of     this    page,    can    be    found    at
108       https://www.kernel.org/doc/man-pages/.
109
110
111
112GNU                               2021-03-22                          POPEN(3)
Impressum