函数popen()

时间: 2023-07-10 admin IT培训

函数popen()

函数popen()

用到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.out 
sh: -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 */
}