在编写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如下所示:
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- char line[1024];
- FILE *fpin,*fpout;
- fpin=fopen("in.txt","r");
- fpout=popen("/my_fun","w");
- while(fgets(line,1024,fpin)!=NULL)
- fputs(line,fpout);
- pclose(fpout);
- fclose(fpin);
- return 0;
- }
my_fun.c程序代码如下:
- #include <stdio.h>
- int main()
- {
- char line[1024];
- while(fgets(line,1024,stdin)!=NULL)
- fputs(line,stdout);
- return 0;
- }
程序分析如下:首先popen()函数创建一条管道,方向为父进程的fpout到子进程的stdin,接着popen程序打开in.txt文本并一行一行地读取出来写到管道的fpout端。子进程则从stdin中读取从父进程发送过来的数据显示到stdout中。