1POPEN(3) 386BSD Programmer's Manual POPEN(3) 2 3NNAAMMEE 4 ppooppeenn, ppcclloossee - process I/O 5 6SSYYNNOOPPSSIISS 7 ##iinncclluuddee <<ssttddiioo..hh>> 8 9 _F_I_L_E * 10 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) 11 12 _i_n_t 13 ppcclloossee(_F_I_L_E *_s_t_r_e_a_m) 14 15DDEESSCCRRIIPPTTIIOONN 16 The ppooppeenn() function ``opens'' a process by creating a pipe, forking, and 17 invoking the shell. Since a pipe is by definition unidirectional, the 18 _t_y_p_e argument may specify only reading or writing, not both; the 19 resulting stream is correspondingly read-only or write-only. 20 21 The _c_o_m_m_a_n_d argument is a pointer to a null-terminated string containing 22 a shell command line. This command is passed to /_b_i_n/_s_h using the --cc 23 flag; interpretation, if any, is performed by the shell. The _m_o_d_e 24 argument is a pointer to a null-terminated string which must be either 25 `r' for reading or `w' for writing. 26 27 The return value from ppooppeenn() is a normal standard I/O stream in all 28 respects save that it must be closed with ppcclloossee() rather than ffcclloossee(). 29 Writing to such a stream writes to the standard input of the command; the 30 command's standard output is the same as that of the process that called 31 ppooppeenn(), unless this is altered by the command itself. Conversely, 32 reading from a ``popened'' stream reads the command's standard output, 33 and the command's standard input is the same as that of the process that 34 called ppooppeenn(). 35 36 Note that output ppooppeenn() streams are fully buffered by default. 37 38 The ppcclloossee() function waits for the associated process to terminate and 39 returns the exit status of the command as returned by wwaaiitt44(). 40 41RREETTUURRNN VVAALLUUEE 42 The ppooppeenn() function returns NULL if the fork(2) or pipe(2) calls fail, 43 or if it cannot allocate memory. 44 45 The ppcclloossee() function returns -1 if _s_t_r_e_a_m is not associated with a 46 ``popened'' command, if _s_t_r_e_a_m already ``pclosed'', or if wait4 returns 47 an error. 48 49EERRRROORRSS 50 The ppooppeenn() function does not reliably set _e_r_r_n_o. 51 52SSEEEE AALLSSOO 53 fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), 54 stdio(3), system(3) 55 56BBUUGGSS 57 Since the standard input of a command opened for reading shares its seek 58 offset with the process that called ppooppeenn(), if the original process has 59 done a buffered read, the command's input position may not be as 60 expected. Similarly, the output from a command opened for writing may 61 become intermingled with that of the original process. The latter can be 62 avoided by calling fflush(3) before ppooppeenn(). 63 64 Failure to execute the shell is indistinguishable from the shell's 65 failure to execute command, or an immediate exit of the command. The 66 only hint is an exit status of 127. 67 68 The ppooppeenn() argument always calls sh, never calls csh. 69 70HHIISSTTOORRYY 71 A ppooppeenn() and a ppcclloossee() function appeared in Version 7 AT&T UNIX. 72 73BSD Experimental April 30, 1991 2 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133