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
13       int pclose(FILE *stream);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       popen(), pclose():
18           _POSIX_C_SOURCE >= 2
19               || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
20

DESCRIPTION

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

RETURN VALUE

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

ERRORS

65       The popen() function does not set errno if memory allocation fails.  If
66       the  underlying  fork(2)  or pipe(2) fails, errno is set appropriately.
67       If the type argument is invalid, and this condition is detected,  errno
68       is set to EINVAL.
69
70       If pclose() cannot obtain the child status, errno is set to ECHILD.
71

ATTRIBUTES

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

CONFORMING TO

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

NOTES

88       Note: carefully read Caveats in system(3).
89

BUGS

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

SEE ALSO

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

COLOPHON

107       This page is part of release 5.10 of the Linux  man-pages  project.   A
108       description  of  the project, information about reporting bugs, and the
109       latest    version    of    this    page,    can     be     found     at
110       https://www.kernel.org/doc/man-pages/.
111
112
113
114GNU                               2017-09-15                          POPEN(3)
Impressum