函数popen()
2016-06-09 12:00
639 查看
转载:http://blog.csdn.net/qian_f/article/details/8227435
函数说明
popen()函数通过创建一个管道,调用fork()产生一个子进程,执行一个shell以运行命令来开启一个进程。这个管道必须由pclose()函数关闭,而不是fclose()函数。pclose()函数关闭标准I/O流,等待命令执行结束,然后返回shell的终止状态。如果shell不能被执行,则pclose()返回的终止状态与shell已执行exit一样。type参数只能是读或者写中的一种,得到的返回值(标准I/O流)也具有和type相应的只读或只写类型。如果type是"r"则文件指针连接到command的标准输出;如果type是"w"则文件指针连接到command的标准输入。
command参数是一个指向以NULL结束的shell命令字符串的指针。这行命令将被传到bin/sh并使用-c标志,shell将执行这个命令。
popen()的返回值是个标准I/O流,必须由pclose来终止。前面提到这个流是单向的(只能用于读或写)。向这个流写内容相当于写入该命令的标准输入,命令的标准输出和调用popen()的进程相同;与之相反的,从流中读数据相当于读取命令的标准输出,命令的标准输入和调用popen()的进程相同。
返回值
如果调用fork()或pipe()失败,或者不能分配内存将返回NULL,否则返回标准I/O流。popen()没有为内存分配失败设置errno值。如果调用fork()或pipe()时出现错误,errno被设为相应的错误类型。如果type参数不合法,errno将返回EINVAL。用到popen()的时候,到网上找了找,发现网上好多对man帮助里的内容进行了解释,有部分解释内容如下:“popen 的返回值是个标准
I/O 流,必须由 pclose 来终止。前面提到这个流是单向的。所以向这个流写内容相当于写入该命令的标准输入;命令的标准输出和调用 popen 的进程相同。与之相反的,从流中读数据相当于读取命令的标准输出;命令的标准输入和调用 popen 的进程相同。”这话怎么读都别扭,理解起来就有些难了。先来看看popen的函数声明:FILE
* popen(const char* command, const char* type)
在使用这个函数的时候,将参数type写成“w”就出现了不能理解的地方(网上的一些程序参数type多是“r”)。看下面一个程序:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
FILE * fp;
char buf[40] = {0};
fp = popen(NULL, "w");
if(NULL == fp)
{
perror("popen error.\n");
return -1;
}
printf("Input command:");
fgets(buf, 40, stdin);
fputs(buf, fp);
pclose(fp);
return 0;
}
执行结果:
[root@localhost codetest]# ./a.out
sh: -c: option requires an argument
Input command:pwd
很明显出错了,但是根据那段翻译的内容,搞得自己很糊涂,后来问了老师,说是type参数为"w"的时候,popen的第一个参数为命令解释器,即bash/sh之类。修改后的程序如下:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
FILE * fp;
char buf[40] = {0};
fp = popen("bash", "w");
if(NULL == fp)
{
perror("popen error.\n");
return -1;
}
printf("Input command:");
fgets(buf, 40, stdin);
fputs(buf, fp);
pclose(fp);
return 0;
}
执行结果:
[root@localhost codetest]# ./a.out
Input command:pwd
/home/qian/codetest
这下就对了!
可是还是有疑问的,如果我直接在popen()的第一个参数写命令,也同样可以得到命令结果,程序如下:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
FILE * fp;
char buf[40] = {0};
fp = popen("pwd", "w");
if(NULL == fp)
{
perror("popen error.\n");
return -1;
}
pclose(fp);
return 0;
}
执行结果:
[root@localhost codetest]# ./a.out
/home/qian/codetest
关于这个参数的问题,一直是个疙瘩,如果谁知道的话,可以给个解答,感激不尽!
下面是type参数为“r”的一个程序:
[cpp] view
plain copy
#include <stdio.h>
int main()
{
FILE * fp;
char buf[20] = {0};
fp = popen("ls","r");
if(NULL == fp)
{
perror("popen error!\n");
return -1;
}
while(fgets(buf, 20, fp) != NULL)
{
printf("%s", buf);
}
pclose(fp);
return 0;
}
值得注意的是:一定要用pclose()来关闭文件指针,这是我老忘记的事。
下面给出一个网上给出的popen()和pclose()实现函数:
[cpp] view
plain copy
Figure 15.12. The popen and pclose functions
#include "apue.h"
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
/*
* Pointer to array allocated at run-time.
*/
static pid_t *childpid = NULL;
/*
* From our open_max(), Figure 2.16.
*/
static int maxfd;
FILE *
popen(const char *cmdstring, const char *type)
{
int i;
int pfd[2];
pid_t pid;
FILE *fp;
/* only allow "r" or "w" */
if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
errno = EINVAL; /* required by POSIX */
return(NULL);
}
if (childpid == NULL) { /* first time through */
/* allocate zeroed out array for child pids */
maxfd = open_max();
if ((childpid = calloc(maxfd, sizeof(pid_t))) == NULL)
return(NULL);
}
if (pipe(pfd) < 0)
return(NULL); /* errno set by pipe() */
if ((pid = fork()) < 0) {
return(NULL); /* errno set by fork() */
} else if (pid == 0) { /* child */
if (*type == 'r') {
close(pfd[0]);
if (pfd[1] != STDOUT_FILENO) {
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1]);
}
} else {
close(pfd[1]);
if (pfd[0] != STDIN_FILENO) {
dup2(pfd[0], STDIN_FILENO);
close(pfd[0]);
}
}
/* close all descriptors in childpid[] */
for (i = 0; i < maxfd; i++)
if (childpid[i] > 0)
close(i);
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
_exit(127);
}
/* parent continues... */
if (*type == 'r') {
close(pfd[1]);
if ((fp = fdopen(pfd[0], type)) == NULL)
return(NULL);
} else {
close(pfd[0]);
if ((fp = fdopen(pfd[1], type)) == NULL)
return(NULL);
}
childpid[fileno(fp)] = pid; /* remember child pid for this fd */
return(fp);
}
int
pclose(FILE *fp)
{
int fd, stat;
pid_t pid;
if (childpid == NULL) {
errno = EINVAL;
return(-1); /* popen() has never been called */
}
fd = fileno(fp);
if ((pid = childpid[fd]) == 0) {
errno = EINVAL;
return(-1); /* fp wasn't opened by popen() */
}
childpid[fd] = 0;
if (fclose(fp) == EOF)
return(-1);
while (waitpid(pid, &stat, 0) < 0)
if (errno != EINTR)
return(-1); /* error other than EINTR from waitpid() */
return(stat); /* return child's termination status */
}
相关文章推荐
- 服务器nginx+nodejs配置
- 详解Linux中将应用程序打包为Snap软件包格式的方法
- poj 3258River Hopscotch
- linux 下wine 和plyaonlinux 安装以及卸载 (linux下运行windows软件的神器)
- zookeeper入门-整理版
- CentOS 7 安装 postgreSQL 9.4
- CentOS 7 安装 postgreSQL 9.4
- IIPP迷你项目(三)“Stopwatch: The Game”
- IEEE802.11数据帧在Linux上的抓取
- vmware下虚拟机centos 桥接上网
- centos 下yum 安装nginx
- 网站常见的安全漏洞
- 搭建Nginx与tomcat整合,用Nginx代替apache
- Linux环境下字符设备驱动开发入门
- actionbar setDisplayOptions用法详解
- CentOS下安装Nginx
- 基础命令
- Web应用架构
- linux 查看软件版本号
- IMF Vmvare 中linux 根分区磁盘扩容 gparted 增加虚拟机磁盘空间