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 || _XOPEN_SOURCE || _BSD_SOURCE ||
19           _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
25       resulting 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.  The type
30       argument is a pointer to a null-terminated string  which  must  contain
31       either the letter 'r' for reading or the letter 'w' for writing.  Since
32       glibc 2.9, this argument can additionally include the letter 'e', which
33       causes  the close-on-exec flag (FD_CLOEXEC) to be set on the underlying
34       file descriptor; see the description of the O_CLOEXEC flag  in  open(2)
35       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
42       itself.  Conversely, reading from a "popened"  stream  reads  the  com‐
43       mand's standard output, and the command's standard input is the same as
44       that of the process that called popen().
45
46       Note that output popen() streams are fully 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       The popen() function returns NULL if the fork(2) or pipe(2) calls fail,
53       or if it cannot allocate memory.
54
55       The pclose() function returns -1 if wait4(2) returns an error, or  some
56       other error is detected.  In the event of an error, these functions set
57       errnro to indicate the cause of the error.
58

ERRORS

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

CONFORMING TO

68       POSIX.1-2001.
69
70       The 'e' value for type is a Linux extension.
71

BUGS

73       Since  the  standard  input  of a command opened for reading shares its
74       seek offset with the process  that  called  popen(),  if  the  original
75       process  has done a buffered read, the command's input position may not
76       be as expected.  Similarly, the output from a command opened for  writ‐
77       ing  may  become  intermingled  with that of the original process.  The
78       latter can be avoided by calling fflush(3) before popen().
79
80       Failure to execute the shell  is  indistinguishable  from  the  shell's
81       failure  to  execute command, or an immediate exit of the command.  The
82       only hint is an exit status of 127.
83

SEE ALSO

85       sh(1), fork(2),  pipe(2),  wait4(2),  fclose(3),  fflush(3),  fopen(3),
86       stdio(3), system(3)
87

COLOPHON

89       This  page  is  part of release 3.53 of the Linux man-pages project.  A
90       description of the project, and information about reporting  bugs,  can
91       be found at http://www.kernel.org/doc/man-pages/.
92
93
94
95GNU                               2013-04-19                          POPEN(3)
Impressum