c语言 OR shell 写配置文件求助

821586284 2016-04-26 08:27:51
求大神们帮忙用shell或者C语言写个 写配置文件的程序, 配置文件放在/etc/wifi ,名称为wpa.conf
配置文件格式为:
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="***"
psk="***"
}
其中network格式不能改变,只能修改ssid,和psk, ssid和psk由上层应用传入,
第一次传入 ssid=S1 psk= num1, 则配置文件为:
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="S1"
psk="num1"
}
第二次传入 ssid=S2 psk=num2,则配置文件为:
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="S1"
psk="num1"
}
network={
ssid="S2"
psk="num2"
}
...全文
213 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gstatham 2016-05-18
  • 打赏
  • 举报
回复
int write_wifi_info_to_wpaconf() { char format_start[10]="network="; char format_end[5]="}"; // char format_name[64]="ctrl_interface=/var/run/wpa_supplicant"; char current_wifi_name[64]="ssid=\""; char current_wifi_psk[64]="psk=\""; char ch[2]="\n"; char ch1[2]="\""; char ch2[2]="{"; FILE *fp=NULL; if((fp = fopen(wifi_info_loc,"a+")) == NULL) //a+ write in the end of file w rewrite { NET_ERROR("open old wifi info file failed!!!\n"); return -1; } //memcpy(wifi_info_st->current_wifi.wifi_name,tmp,sizeof(tmp)); strcat(current_wifi_name,wifi_info_st->current_wifi.wifi_name); strcat(current_wifi_name,ch1); NET_INFO("[interface] current_wifi_name =====%s, total len=%d\n",current_wifi_name,strlen(current_wifi_name)); //memcpy(wifi_info_st->current_wifi.passwd,tmp1,sizeof(tmp1)); strcat(current_wifi_psk,wifi_info_st->current_wifi.passwd); strcat(current_wifi_psk,ch1); NET_INFO("[interface] current_wifi_psk =====%s,total len=%d\n",current_wifi_psk,strlen(current_wifi_psk)); // fwrite(format_name,strlen(format_name),sizeof(char),fp); //ctrl_interface=/var/run/wpa_supplicant // fwrite(ch,1,sizeof(char),fp); strcat(format_start,ch2); //printf("format=== %s\n",format_start); fwrite(format_start,strlen(format_start),sizeof(char),fp); //network={ fwrite(ch,1,sizeof(char),fp); fwrite(current_wifi_name,strlen(current_wifi_name),sizeof(char),fp); fwrite(ch,1,sizeof(char),fp); fwrite(current_wifi_psk,strlen(current_wifi_psk),sizeof(char),fp); fwrite(ch,1,sizeof(char),fp); fwrite(format_end,strlen(format_end),sizeof(char),fp);//} fwrite(ch,1,sizeof(char),fp); fclose(fp); return 0; }
niepangu 2016-04-28
  • 打赏
  • 举报
回复
也来帮你顶一下吧 我是菜鸟只能这样了,
  • 打赏
  • 举报
回复
把文件内容读出来,存到数组中,然后把新加的也存到数组,最后把数组存到文件。
LubinLew 2016-04-27
  • 打赏
  • 举报
回复
没时间, 搞了个简单的你可以参考一下,用法就是 program -s ssid -p psk

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

#define DEFAULT_COMMENT_SIGN "#"
#define CONFIG_FILE "/etc/wifi/wpa.conf"
#define DEFAULT_IF "ctrl_interface=/var/run/wpa_supplicant\n"
#define NET_CONFIG "network={\n    ssid=\"%s\"\n    psk=\"%s\"\n}\n\n"

#define COLRED "\033[1;31;40m"
#define COLGRN "\033[1;32;40m"
#define COLEND "\033[0m"

void show_help(char** argv);

int main(int argc, char** argv)
{
	//config options
	char* ssid = NULL;
	char* psk = NULL;
	char* remark = NULL;
	char* interface = NULL;
	FILE* fp = NULL;
	
	//getopt options
	int c;
	int option_index = 0;
	int digit_optind = 0;
	int this_option_optind;
	int delflg = 0;
	
	static struct option long_options[] = {
		{"ssid",   required_argument, 0, 's'},
		{"psk",    required_argument, 0, 'p'},
		{"show",   no_argument,       0, 'v'},
		{"remark", required_argument, 0, 'r'},
		{"help",   no_argument,       0, 'h'},
		{"if",     required_argument, 0, 'i'},
		{"delete", no_argument,       0, 'd'},
		{0,        0,                 0, 0  }
	};
	
	if (argc < 2) {
		show_help(argv);
	}
	
	while (1) {
		this_option_optind = optind ? optind : 1;
		c = getopt_long(argc, argv, "i:s:p:r:hvd", long_options, &option_index);
		if (c == -1)
			break;
		
		switch (c) {
		case 's':
			ssid = optarg;
			break;

		case 'p':
			psk = optarg;
			break;
	
		case 'r':
			remark = optarg;
			printf("set remark info : unfinished\n");
			goto END;
			break;
			
		case 'd':
			delflg = 1;
			printf("delete setting : unfinished\n");
			goto END;
			break;
			
		case 'i':
			interface = optarg;
			printf("change interface : unfinished\n");
			goto END;
			break;
			
		case 'h':
		default:
			show_help(argv);
		}
	}

	if ((NULL == ssid && NULL != psk) || (NULL != ssid && NULL == psk) || \
		(1 == delflg && (NULL == ssid|| NULL == psk))) {
		show_help(argv);
	}

	fp = fopen(CONFIG_FILE, "r");
	if (NULL == fp) {
		fp = fopen(CONFIG_FILE, "w");
		if (NULL == fp) {
			perror("fopen failed");
			goto END;
		}
		fprintf(fp, "%s\n", DEFAULT_IF);
	} else {
		fclose(fp);
		fp = fopen(CONFIG_FILE, "a");
	}
	
	fprintf(fp, NET_CONFIG, ssid, psk);
	fclose(fp);

END:
	exit(EXIT_SUCCESS);
}

void show_help(char** argv)
{
	printf(COLRED"Usage: %s [--delete] --ssid=SSID --psk=PSK [--remark=REMARK] [--if=NEWIF]"COLEND"\n", argv[0]);
	printf("add or del wifi info int the file %s\n\n", CONFIG_FILE);
	printf("Mandatory arguments to long options are mandatory for short options too.\n");
	printf(COLGRN"  -s, --ssid=SSID      "COLEND"service set identifier\n");
	printf(COLGRN"  -p, --pak=PSK        "COLEND"pre-shared key\n");
	printf(COLGRN"  -r, --remark=REMAKR  "COLEND"comment for the setting\n");
	printf(COLGRN"  -h, --help           "COLEND"show this help page and exit the program\n");
	printf(COLGRN"  -i, --if=INTERFACE   "COLEND"change the default wifi interface\n");
	printf(COLGRN"  -d, --delete         "COLEND"delete the specified setting\n");
	printf(COLGRN"  -v, --show           "COLEND"show all setting in the config file\n");
	printf("\nThanks for using this program!\n\n");
	exit(EXIT_SUCCESS);
}

821586284 2016-04-26
  • 打赏
  • 举报
回复
给自己顶一下

23,128

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧