2 Copyright © 1995-2003, The AROS Development Team. All rights reserved.
5 ANSI C function popen().
8 #include <utility/tagitem.h>
19 /*****************************************************************************
31 "opens" a process by creating a pipe, spawning a new process and invoking
35 command - Pointer to a null terminated string containing the command
36 to be executed by the shell.
38 mode - Since a pipe is unidirectional, mode can be only one of
40 r: Open for reading. After popen() returns, the stream can
41 be used to read from it, as if it were a normal file stream,
42 in order to get the command's output.
44 w: Open for writing. After popen() returns, the stream can
45 be used to write to it, as if it were a normal file stream,
46 in order to provide the command with some input.
49 A pointer to a FILE handle or NULL in case of an error. When NULL
50 is returned, then errno is set to indicate the error.
53 This function must not be used in a shared library or
54 in a threaded application.
61 fclose(), fread(), fwrite(), pipe(), pclose()
65 ******************************************************************************/
69 if (!mode
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != '\0')
71 errno
= !mode
? EFAULT
: EINVAL
;
75 if (pipe(pipefds
) == 0)
77 int fdtopass
= (mode
[0] == 'r');
79 struct TagItem tags
[] =
82 { SYS_Output
, NULL
},
83 { SYS_Error
, SYS_DupStream
},
84 { SYS_Asynch
, TRUE
},
85 { NP_StackSize
, Cli()->cli_DefaultStack
* CLI_DEFAULTSTACK_UNIT
},
89 tags
[fdtopass
].ti_Data
= (IPTR
)__getfdesc(pipefds
[fdtopass
])->fcb
->fh
;
90 tags
[1 - fdtopass
].ti_Data
= SYS_DupStream
;
92 if (SystemTagList(command
, tags
) != -1)
94 /* Little trick to deallocate memory which otherwise wouldn't get deallocated */
95 __getfdesc(pipefds
[fdtopass
])->fcb
->fh
= NULL
;
96 close(pipefds
[fdtopass
]);
98 return fdopen(pipefds
[1 - fdtopass
], NULL
);