Starts a subshell and feed it the string cmd_ to be executed.
The pipe is created and attached to the standard input or standard output of the subprocess, according to whether type_ is either "r" (read) or "w" (write). The other end of the pipe is returned to the calling code as a standard I/O stream, FILE, ready for buffered use with fprintf(), fscanf(), fgets, etc.
- See Also
- Fork
- Parameters
-
cmd_ | command to execute |
type_ | "w" for write pipe and "r" for read pipe |
- Returns
- pointer to a standard I/O stream. In case of error, NULL is returned with errno set to indicate the type of error encountered.
Definition at line 51 of file Pipe.cpp.
References ASSA::ASSAERR, close(), DL, EL, fd(), ASSA::Fork::getChildPID(), ASSA::Fork::IGNORE_STATUS, ASSA::Fork::isChild(), ASSA::Fork::KILL_ON_EXIT, m_child_pid, m_fp, ASSA::PIPE, and trace_with_mask.
55 #if !defined(WIN32) // not yet implemented
57 if (type_ !=
"r" && type_ !=
"w") {
58 EL((
ASSAERR,
"Wrong type \"%s\"\n", type_.c_str ()));
73 if (fd [1] != STDOUT_FILENO) {
74 dup2 (fd [1], STDOUT_FILENO);
80 if (fd [0] != STDIN_FILENO) {
81 dup2 (fd [0], STDIN_FILENO);
86 DL((
PIPE,
"Executing cmd: \"%s\"\n", cmd_.c_str ()));
87 execl (
"/bin/sh",
"sh",
"-c", cmd_.c_str (), (
char* ) 0);
94 if ((
m_fp = fdopen (fd [0], type_.c_str ())) == NULL) {
101 if ((
m_fp = fdopen (fd [1], type_.c_str ())) == NULL) {
int close()
Close the pipe.
Fork class is a simple wrapper around C library function fork().
FILE * m_fp
A standard I/O stream descriptor.
int fd() const
Get pipe's file descriptor.
Kill all childer on exit.
#define DL(X)
A macro for writing debug message to the Logger.
pid_t m_child_pid
Supbrocess' PID.
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Don't wait for child to complete.
#define EL(X)
A macro for writing error message to the Logger.