popen函数实现捕获shell里命令的输出--外加自己的一个小接口函数



C语言里有个popen函数,用来捕获shell里命令的输出,这样C和shell也可以相互通信了。

自己的接口函数,可随便调用,很方便

static int get_cmd_ret(char *cmd,char *buf,int len)
{
        if(!cmd || !strlen(cmd))
                return -1; 
        if(len <= 0)
                return -1; 
        FILE   *stream;
        memset( buf, 0,len);
  //      printf("cmd--%s--\n",cmd);
//        printf("buf--%s--\n",buf);
        stream = popen(cmd, "r" );
        fread( buf, sizeof(char),len, stream);
        pclose(stream);
        return 1;
}

在网上看了个例子修改了一下,原文:http://blogold.chinaunix.net/u/16292/showart_393022.html


·  /* 
·  作用:演示popen用法,将源程序输出到argv[1]命名的文件中 
·  使用:argv[0] outputFile 
·  */  
· #include <sys/types.h>    
· #include <unistd.h>    
· #include <stdlib.h>    
· #include <stdio.h>    
· #include <string.h>  
· #define CMDLEN  1024  
· int main(int argc, char** argv)    
· {    
·     do  
·     {  
·       if ( 2 != argc)  
·       {  
·         printf("Usage:%s outputFile!\n",argv[0]);  
·         break;  
·       }  
·         
·       FILE   *stream;    
·       FILE    *wstream;  
·       char   buf[1024];   
·       char   myCmd[CMDLEN];  
·       memset(myCmd,0,CMDLEN);   
·       strcpy(myCmd,"cat ");  
·       strcat(myCmd,argv[0]);  
·       strcat(myCmd,".c");  
·       memset( buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中  
·       stream = popen( myCmd, "r" ); //将myCmd命令的输出 通过管道读取(“r”参数)到FILE* stream  
·       wstream = fopen( argv[1], "w+"); //新建一个可写的文件  
·       fread( buf, sizeof(char), sizeof(buf),  stream);  //将刚刚FILE* stream的数据流读取到buf中  
·       fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE    *wstream对应的流中,也是写到文件中  
·       pclose( stream );    
·       fclose( wstream );  
·     }while(0);  
·     return 0;  
·  }    


 Linux系统进程控制编程(五)——popen函数调用

摘要:本文介绍了popen函数的使用方法和行为机理,并给出实际的例子来辅助说明了popen函数的使用方法。
文件中还介绍了几个文件操作的函数,如fopen,fread,fwrite等
  
作者:zieckey (http://zieckey.cublog.cn)
    All Rights Reserved!

popen使用FIFO管道执行外部程序。

#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);

popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。

下面看一个例子:

/*******************************************************************************************
** Name:popen.c
**      This program is used to show the usage ofpopen() .
** Author:zieckey,(zieckey@yahoo.com.cn)
** Date:2007/9/30 11:47
** All rights reserved!
*******************************************************************************************/
#include <sys/types.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>

int main( void )  
{  
    FILE   *stream;  
    FILE    *wstream;
    char   buf[1024]; 
     
    memset( buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
    stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
    wstream = fopen( "test_popen.txt","w+"); //新建一个可写的文件

    fread( buf, sizeof(char), sizeof(buf),  stream); //将刚刚FILE* stream的数据流读取到buf中
    fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE    *wstream对应的流中,也是写到文件中
    
    pclose( stream );  
    fclose( wstream );
    
    return 0;
}   



[root@localhost src]# gcc popen.c
[root@localhost src]# ./a.out  
[root@localhost src]# cat test_popen.txt
总计 128
-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
-rwxr-xr-x 1 root root  542 09-30 00:00 child_fork.c
-rwxr-xr-x 1 root root  480 09-30 00:13 execve.c
-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
-rwxr-xr-x 1 root root  162 09-29 18:54 getpid.c
-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
-rwxr-xr-x 1 root root  443 09-30 00:55 system.c
-rwxr-xr-x 1 root root    0 09-30 11:51 test_popen.txt
-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值