popen()的使用心得

在编写SDwifi芯片时,用到shell。其中调用.sh程序时用到popen(),进行执行.sh程序。

其中把fopen与popen没注意。还出错。

open()函数原型如下:

        FILE *popen(const char *cmd,const char *type);

                                         返回值:若成功返回文件指针,出错则返回NULL

功能:创建一个管道,fork一个子进程,接着关闭管道的不使用端,子进程执行cmd指向的应用程序或者命令。

执行完该函数后父进程和子进程之间生成一条管道,函数返回值为FILE结构指针,该指针作为管道的一端,为父进程所拥有。子进程则拥有管道的另一端,该端口为子进程的stdin或者stdout。如果type=r,那么该管道的方向为:子进程的stdout到父进程的FILE指针;如果type=w,那么管道的方向为:父进程的FILE指针到子进程的stdin。

一个很挫的程序,但是自己写的,是一个接口,放一下:

#include "WIFI.h"
//#include "cJSON.c"
char buffer[1024] = {0};
char name[32] = {0};
char key[32] = {0};

/*1\void doit(char *text,cJSON **json) //jSON
{
//char *out;//cJSON *json;
//int flag = 0;
*json=cJSON_Parse(text);
}*/

cJSON *doit(char *text) //jSON
{
//char *out;//cJSON *json;
//int flag = 0;
return cJSON_Parse(text);
}
//shell脚本获得无线网络
void ScanGet() 
{
FILE * fp;
char buffer[1024];
fp=popen("/test/wifitest.sh","r");
fgets(buffer,sizeof(buffer),fp);
printf("%s",buffer);
pclose(fp);
}
//脚本检测无线链接******************************************************************
void checkwifi()
{
FILE * fp;
char buffer[1024];
fp=popen("/test/checkwlan0.sh","r");
while(fgets(buffer,sizeof(buffer),fp))
{
printf("%s",buffer);
memset(buffer,0,sizeof(buffer));
}
pclose(fp);
}

//*********************************************************************************
//配置获得的无线网络
void choice_wifilink()
{
FILE * fp1;
fp1=popen("/test/loadwifiready.sh","r");
pclose(fp1);


FILE * fp;
char buffer[1024];
fp=popen("/test/choice_wifilink.sh","r");
fgets(buffer,sizeof(buffer),fp);
printf("%s",buffer);
pclose(fp);
}
//链接网络操作,并判断结果返回
char * LinkWifidone(char* username,char* userkey) 
{
memset(name,0,32);
memset(key,0,32);
strcpy(name,username);
strcpy(key,userkey);

FILE * fp_name;
FILE * fp_key;
fp_name= fopen("/test/essidname","w+");printf("BBBB\n");
fp_key = fopen("/test/essidkey","w+");printf("CC:%dCC:%d\n",strlen(name),sizeof(key));
fwrite(name,strlen(name),1,fp_name);
fwrite(key,strlen(key),1,fp_key);
pclose(fp_name);
pclose(fp_key);
choice_wifilink();//配置获得的无线名及密码
//连接获得的无线
FILE * fp;
char buffer[1024];
fp=popen("/test/shell_linkwifi_done.sh","r");
if(fp == NULL)
{
return "NULL";
}
fgets(buffer,sizeof(buffer),fp);
printf("%s",buffer);
pclose(fp);
return CheckWifiLink();
}

//判断网络链接上与否-------未连接:NULL,连接:无线名
char* CheckWifiLink()
{
checkwifi();
int fd2 = 0;
char buffer[1024] = {0};
char buffer_name[64] ;
char wlan0name[64];
int checknum = 0; printf("11111\n");
int fd1=open("/test/checkwififile", O_RDWR|O_CREAT);
checknum = read(fd1,buffer,sizeof(buffer));printf("33333:%d\n",checknum);
if(checknum > 8)
{
fd2 = open("/test/essidname",O_RDWR);
checknum = read(fd2,buffer_name,sizeof(buffer_name));printf("55555:checknum is:%d\n",checknum);
//return buffer_name;
strcpy(wlan0name,buffer_name);
return wlan0name;
}
else
{
//strcpy(*str,"no_wifi_linked");
return "NULL";
}
}

//获得可连接的无线网络
//1\int Get_Essid_From(cJSON **root)
cJSON * Get_Essid_From()
{
ScanGet(); //获取前进行
//int fd = open("/test/wifidata",O_RDWR); 
//int read_fd = read(fd, buffer, 1024);
//close(fd);
//return buffer;
char wifiname[24][32] = {0};
char WIFIDATA[1024] = {0};
//char ONE_WIFI[128] = {0};
int i =0;
//cJSON *root;
//root = cJSON_CreateObject();//创建项目
strcat(WIFIDATA,"{\"WIFIessid\":[");
FILE  *stream;
stream = fopen("/test/wifidata","r+"); 
if(fgets(wifiname[i],32,stream) != NULL)
{
// printf("%s", wifiname[i]);
wifiname[i][strlen(wifiname[i])-1]=0;
strcat(WIFIDATA,"{\"ESSID\":\"");
strcat(WIFIDATA,wifiname[i]);
strcat(WIFIDATA,"\"}");
i++;
}
while(fgets(wifiname[i],32,stream) != NULL)
{
// printf("%s", wifiname[i]);
wifiname[i][strlen(wifiname[i])-1]=0;
strcat(WIFIDATA,",");
strcat(WIFIDATA,"{\"ESSID\":\"");
strcat(WIFIDATA,wifiname[i]);
strcat(WIFIDATA,"\"}");
i++;
}
strcat(WIFIDATA,"]}");
printf("%s\n", WIFIDATA);
cJSON *root;
//1\doit(WIFIDATA,root);
root = doit(WIFIDATA);
return root;
}


/*
int main()
{
cJSON *root;
Get_Essid_From(root);
Get_namekey("ylst-s","beacon506"); 
LinkWifidone();
//int fd = open("/root/zy/Shell/wifitest/data",O_RDWR); 
//int read_fd = read(fd, buffer, 1024);
//printf("%s\n",buffer);
}*/


网上的例子:

example:

 主程序popen.c如下所示:

[html]  view plain copy
  1. #include <unistd.h>  
  2. #include <sys/types.h>  
  3. #include <sys/wait.h>  
  4. #include <errno.h>  
  5. #include <stdio.h>  
  6. #include <stdlib.h>  
  7.   
  8. int main()  
  9. {  
  10.     char line[1024];  
  11.     FILE *fpin,*fpout;  
  12.       
  13.     fpin=fopen("in.txt","r");  
  14.       
  15.     fpout=popen("/my_fun","w");  
  16.       
  17.     while(fgets(line,1024,fpin)!=NULL)  
  18.         fputs(line,fpout);  
  19.           
  20.     pclose(fpout);  
  21.     fclose(fpin);  
  22.       
  23.     return 0;  
  24. }  


my_fun.c程序代码如下:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     char line[1024];  
  5.   
  6.     while(fgets(line,1024,stdin)!=NULL)  
  7.         fputs(line,stdout);  
  8.           
  9.     return 0;  
  10. }  


程序分析如下:首先popen()函数创建一条管道,方向为父进程的fpout到子进程的stdin,接着popen程序打开in.txt文本并一行一行地读取出来写到管道的fpout端。子进程则从stdin中读取从父进程发送过来的数据显示到stdout中。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值