Table header File |
#include <stdio.h> |
Defining functions |
FILE * Popen (const char * command,const char * type); |
Function description |
Popen () calls fork () to produce the child process and then calls/BIN/SH-C from the child process to execute the command of the parameter command. The parameter type can be read by using "R", and "W" represents the write. According to this type value, Popen () establishes a standard output device or standard input device that the pipeline connects to the child process, and then returns a file pointer. The process can then use this file pointer to read the output device of the child process or write to the standard input device of the child process. In addition, all functions that use the file pointer (file*) operation are also available, except for fclose ().
- If type is r, then the calling process reads the standard output of command.
- If Type is W, then the calling process writes to the command's standard input.
|
return value |
Returns the file pointer if successful, otherwise returns NULL, the reason for the error is in errno. |
Error code |
The einval parameter type is not valid. |
Precautions |
When writing programs with Suid/sgid permissions, try to avoid using popen (), Popen () will inherit environment variables and may cause system security problems through environment variables. |
Example |
#include <stdio.h>main () {FILE *fp;char BUFFER[80];FP = Popen ("cat/etc/passwd", "R"), fgets (buffer, sizeof (buffer ), FP);p rintf ("%s", buffer);p close (FP);} |
Perform |
root:x:0 0:root:/root:/bin/bash |
Reference:
Linux C Programming--popen functions
The Linux Popen () function is detailed