Popen, Pclose-pipe stream to or from a process
FILE *popen (const char *command, const char *type);
int Pclose (FILE *stream);
Describe
The Popen () function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument could specify only reading or writing, not both; The resulting stream is correspondingly read-only or write-only.
The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to/bin/sh using THE-C flag; Interpretation, if any, was performed by the shell. The type argument is a pointer to a null-terminated string Whichi must contain either the letter ' R ' for reading or the LE Tte ' W ' for writing. Since glibc 2.9, this argument can additionally include the letter ' E ', Whichi causes the CLOSE-ON-EXEC flag (fd_cloexec) t O is set on the underlying file descriptor; See the description of the O_CLOEXEC flag in open for resons what this is may usefull.
The return value from Popen () was a normal standard I/O stream in all respects save that it must being closed with pclose () RA Ther than fclose (). 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, which call Popen (), unless this is altered by the command itself. Conversely, reading from a "popened" stream reads the command ' s standard output, and the command's standard input Is the same as, the process the called Popen ().
Note that output popen () streams is fully buffered by default.
The Pclose () function waits for the associated process to terminate and returns the exit status of the Comman as returned by WAIT4.
return value
The Popen () function returns NULL if the fork or pipe calls fail, or if it cannot allocate memory.
The Pclose () function returns-1 if WAIT4 returns an error, or some other error is detected. In the event of a error, these functions set errno to indicate the cause of the error.
BUGS
Since the standard input of a command opened for reading shares it seek offset with the process that called popen (), If the original process has done a buffered read, the command's input position may not be as expected. Similarly, the output from a command opened for writing may become intermingled with that of the original process. The latter can avoided by calling Fflush (3) before Popen ().
Popen&pclose Pipeline Operation Shell Command