函数popen()
来源:互联网 发布:才真旺姆解释双修 知乎 编辑:程序博客网 时间:2024/05/21 08:39
转载: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”)。看下面一个程序:
- #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.outsh: -c: option requires an argument
Input command:pwd
很明显出错了,但是根据那段翻译的内容,搞得自己很糊涂,后来问了老师,说是type参数为"w"的时候,popen的第一个参数为命令解释器,即bash/sh之类。修改后的程序如下:
- #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()的第一个参数写命令,也同样可以得到命令结果,程序如下:
- #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”的一个程序:
- #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()实现函数:
- 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 */
- }
- popen 函数
- popen函数
- popen函数
- popen()函数
- popen函数
- 函数popen()
- Popen函数
- popen函数
- 函数popen
- 函数popen()
- popen 函数
- popen函数
- popen函数
- linux popen函数
- popen函数的介绍!
- 自己实现popen函数
- unp pipe popen函数
- popen函数详解
- java创建多线程实现的方式
- 2015级C++第16周程序阅读 文件操作
- 文章标题
- 爸第一次来大学
- Python学习笔记 —— 字典
- 函数popen()
- HDU4006
- DrawerLayout的入门
- 服务器nginx+nodejs配置
- iOS应用发布篇一(开发者账号申请)
- 一个32岁入门的70后程序员给我的启示
- matlab 可视化 —— axis
- Python 各种测试框架简介(一):doctest
- Jenkins distributed builds - master/slave