POPEN(3) BSD Programmer's Manual POPEN(3) NNAAMMEE ppooppeenn, ppcclloossee - process I/O SSYYNNOOPPSSIISS ##iinncclluuddee <<ssttddiioo..hh>> _F_I_L_E _* ppooppeenn(_c_o_n_s_t _c_h_a_r _*_c_o_m_m_a_n_d, _c_o_n_s_t _c_h_a_r _*_t_y_p_e); _i_n_t ppcclloossee(_F_I_L_E _*_s_t_r_e_a_m); DDEESSCCRRIIPPTTIIOONN The ppooppeenn() function ``opens'' a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the _t_y_p_e argument may specify only reading or writing, not both; the result- ing stream is correspondingly read-only or write-only. The _c_o_m_m_a_n_d argument is a pointer to a null-terminated string containing a shell command line. This command is passed to _/_b_i_n_/_s_h using the --cc flag; interpretation, if any, is performed by the shell. The _m_o_d_e argu- ment is a pointer to a null-terminated string which must be either `r' for reading or `w' for writing. The return value from ppooppeenn() is a normal standard I/O stream in all re- spects save that it must be closed with ppcclloossee() rather than ffcclloossee(). Writing to such a stream writes to the standard input of the command; the command's standard output is the same as that of the process that called ppooppeenn(), unless this is altered by the command itself. Conversely, read- ing from a ``popened'' stream reads the command's standard output, and the command's standard input is the same as that of the process that called ppooppeenn(). Note that output ppooppeenn() streams are fully buffered by default. The ppcclloossee() function waits for the associated process to terminate and returns the exit status of the command as returned by wwaaiitt44(). RREETTUURRNN VVAALLUUEE The ppooppeenn() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The ppcclloossee() function returns -1 if _s_t_r_e_a_m is not associated with a ``popened'' command, if _s_t_r_e_a_m already ``pclosed'', or if wait4 returns an error. EERRRROORRSS The ppooppeenn() function does not reliably set _e_r_r_n_o. SSEEEE AALLSSOO fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3) BBUUGGSS Since the standard input of a command opened for reading shares its seek offset with the process that called ppooppeenn(), if the original process has done a buffered read, the command's input position may not be as expect- ed. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can be avoid- ed by calling fflush(3) before ppooppeenn(). Failure to execute the shell is indistinguishable from the shell's fail- ure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. The ppooppeenn() argument always calls sh, never calls csh. HHIISSTTOORRYY A ppooppeenn() and a ppcclloossee() function appeared in Version 7 AT&T UNIX. 4.4BSD June 4, 1993 2