📄 callbacks.c
字号:
{ gchar ch=event->keyval; //g_print("key code %3d pressed .\n",ch); if(ch==13||ch==10){ if(gtk_notebook_get_current_page(GTK_NOTEBOOK(data)))return FALSE; if(is_pppoe_on())return FALSE ; on_connect(NULL,NULL); return TRUE; } if(ch==-1||ch==27||ch==-97)gtk_main_quit(); if(ch=='&'||ch=='*'||ch==';'||ch=='>'||ch=='<'||ch=='|'||ch=='?')return TRUE; return FALSE ;}void combox_entry_changed(GtkEntry *entry,gpointer data){/* gchar *text,*p; text = (gchar *) gtk_entry_get_text (entry); p = text; while(*p){ char ch= *p; if(ch=='&'||ch=='*'||ch==';'||ch=='>'||ch=='<'||ch=='|'||ch=='?'){ *p=0; break ; } p++; } g_print("god:%s\n",text); gtk_entry_set_text(entry,text);*/}void on_windowcancel_btn_clicked(GtkWidget *widget, gpointer user_data){ if(is_pppoe_on())dis_connect(); g_print("quit for cancled !\n"); gtk_main_quit();}void load_interfaces(){ char *line=NULL; gchar **inter; char *fpath="/proc/net/dev"; size_t len=0; FILE *file; ///at the begin ,I want a argument .now I use a global variable instead#define shows interface_combo if((file=fopen(fpath,"r"))==NULL){ g_print("Can't fopen %s .\nWill not work.\n",fpath); } while(getline(&line,&len,file)!=-1){ char * l = g_strstrip(line); if (g_strrstr (l, "eth")) { inter= g_strsplit(l,":",0); if(shows)gtk_combo_box_append_text(GTK_COMBO_BOX(shows),*inter); //g_print("%s\n",*inter); } } fclose(file); if(shows){ gtk_combo_box_set_active (GTK_COMBO_BOX(shows),0); g_print("%s is actived.\n",gtk_combo_box_get_active_text(GTK_COMBO_BOX(shows))); }#undef shows}//void update_void load_profile(GtkWidget *shows){ DIR *dir=NULL; struct dirent *dirs=NULL; int i=0; debug(if(!shows)g_print("adslname_edit is NULL!")); if(!(dir=opendir(dpath))) { g_print("Can't opendir %s : %s .\n",dpath,strerror(errno)); exit(1); } while((dirs=readdir(dir))){ if(dirs->d_type==8){ /* g_print("\ndirs->d_ino:%ld\ndirs->d_off:%ld\n" "dirs->d_reclen:%d\ndirs->d_type:%d\ndirs->d_name:%s\n\n" ,dirs->d_ino,dirs->d_off,dirs->d_reclen,dirs->d_type,dirs->d_name); */ if(shows){ gtk_combo_box_append_text(GTK_COMBO_BOX(shows),dirs->d_name); i++; } //g_print("before strcmp\n"); //g_print("after strcmp\n"); } } gtk_combo_box_set_active (GTK_COMBO_BOX(shows),i>0?i-1:0); closedir(dir); debug(g_print("load_profile():exit .\n"));}void combox_changed(GtkComboBox *widget, gpointer user_data){ load_active_pro(GTK_WIDGET(widget));}void ckb_handler(GtkToggleButton *togglebutton,Iwgt * data){ gchar *cmd[]={shell_path"/scripts/defroute","gw",NULL,NULL}; if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togglebutton))) { switch(data->i){ case 0 : my_exec_str(shell_path"/scripts/share on",1); break; case 1 : gtk_entry_set_editable (GTK_ENTRY(data->gwt),TRUE); cmd[2] = (gchar*)gtk_entry_get_text(GTK_ENTRY(data->gwt)); if(cmd[2][0]){ if(!my_exec(cmd,0))g_print("Set gateway failure !\n"); }else my_exec_str(shell_path"/scripts/defroute gw ppp",1); break; default: break; } g_print("button %d: %s selected .\n",data->i,gtk_button_get_label( GTK_BUTTON(togglebutton))); } else{ switch(data->i){ case 0 : my_exec_str(shell_path"/scripts/share off",1); break; case 1: gtk_entry_set_editable (GTK_ENTRY(data->gwt),FALSE); break; default: break ; } g_print("button %d :%s canceled .\n",data->i,gtk_button_get_label( GTK_BUTTON(togglebutton))); }}void entry_handler(GtkWidget *entry,GtkWidget *data){ gchar *cmd[]={shell_path"/scripts/defroute","gw",NULL,NULL}; int id = 0; cmd[2] = (gchar*)gtk_entry_get_text(GTK_ENTRY(entry)); if(cmd[2][0]) id = my_exec(cmd,0); else g_print("Nothing be done .\n"); if(!id)g_print("Set gateway failure !\n");}void do_ok(GtkWidget *widget,GtkWidget *data){ gtk_widget_hide(data);}void do_rect(GtkWidget *widget,GtkWidget *data){ gtk_widget_hide(data); on_connect(NULL,NULL);}void load_active_pro(GtkWidget *widget){ char *file=NULL; char *rfile=NULL; FILE *f=NULL; //def.username=gtk_entry_get_text(GTK_ENTRY(username_edit)); //def.passwd=gtk_entry_get_text(GTK_ENTRY(password_edit)); if(widget)file = gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget)); rfile = malloc(strlen(file)+strlen(dpath)+4); if(!rfile){ g_print("load_active_pro:Malloc():%s !\n",strerror(errno)); exit(255); } sprintf(rfile,"%s/%s",dpath,file); g_print("load_active_pro()->Default file :%s .\n",rfile); f=fopen(rfile,"r"); if(!f){ g_print("Can't fopen %s :%s\n",rfile,strerror(errno));// return; } else def.adslname = file; def.username = get_username_from(rfile); if(def.username) def.passwd = get_passwd_of(def.username); if(def.username){ gtk_entry_set_text(GTK_ENTRY(username_edit),def.username); //gtk_entry_set_invisible_char(GTK_ENTRY(password_edit),'f'); } else{ def.username=""; def.passwd=""; gtk_entry_set_text(GTK_ENTRY(username_edit),""); } if(!def.passwd)def.passwd=""; gtk_entry_set_text(GTK_ENTRY(password_edit),def.passwd); //g_print("username:%s,passwd:%s\n",def.username,def.passwd); if(rfile)free(rfile); if(f)fclose(f); g_print("I'm OK in load_pro_active .\n");}char * get_username_from(char *profile){ FILE * filer = fopen(profile,"r"); ssize_t read; char * line = NULL; size_t len = 0; static char user[64]={0};//my username have more than it . if(!filer){ fprintf(stderr,"get_username_from:fopen:%s .\n",strerror(errno)); return NULL; } while((read = getline(&line,&len,filer))!=-1) { if (g_strrstr (line, "user")) { gchar ** str_array; g_print(line); str_array = g_strsplit(line,"\"",0); debug(g_print(str_array[1])); strcpy(user,str_array[1]?str_array[1]:"ERROR!"); fclose(filer); g_strfreev(str_array); debug(g_print("user:%s\n",user)); free(line); return user; } } if(line)free(line); if(filer)fclose(filer); //g_print("I did it 2.\n"); return NULL;}char *get_passwd_of(char *username){ ssize_t read;// be_root(); size_t len =0; char * line = NULL; static char passwd[32] = {0}; if(!username){ g_print("username is NULL !\n"); return NULL; } FILE * filer = fopen("/etc/ppp/pap-secrets","r"); if(!filer){ fprintf(stderr,"get_passwd_of:%s .\n",strerror(errno)); return NULL; } while(filer&&(read = getline(&line,&len,filer))!=-1) { if(read >1) { //gchar * l = line; //fprintf(stderr,"%s\n",l); gchar ** str_array; str_array = g_strsplit(line,"\"",0); gchar * user_str = str_array[1]; //fprintf(stderr,"username: %s:%s\n",username,user_str); if(user_str&&!strcmp(username,user_str)) { //fprintf(stderr,"password:%s\n\n",str_array[3]); strcpy(passwd,str_array[3]); //fclose(filer); //str_array[3]; } g_strfreev(str_array); } } if(line)free(line); if(filer)fclose(filer); return passwd; //return NULL;}void save_profile(char *pw,char *unm,char *adsl){ FILE *f1=NULL; char *str=NULL; str = malloc(strlen(adsl)+strlen(dpath)+64); if(!str){ g_print("save_profile:malloc :%s .\n",strerror(errno)); return ; } sprintf(str,"%s/%s",dpath,adsl); if(access(str,F_OK) ==0){ sprintf(str,"/bin/sed -i 's=\".*\"$=\"%s\"=g' %s/%s",unm,dpath,adsl); g_print("%s\n",str); my_exec_str(str,0); } else{ f1=fopen(str,"w+"); if(!f1)g_print("Can't fopen %s .\n",str); else{ fprintf(f1,"noipdefault\n"); fprintf(f1,"defaultroute\n");#ifndef magic_linux fprintf(f1,"replacedefaultroute\n"); ///pppd in magiclinux don't accept this option#endif fprintf(f1,"hide-password\n"); fprintf(f1,"noauth\n"); fprintf(f1,"persist\n"); fprintf(f1,"plugin rp-pppoe.so %s\n", gtk_combo_box_get_active_text(GTK_COMBO_BOX(interface_combo))); fprintf(f1,"usepeerdns\n"); fprintf(f1,"user \"%s\"\n",unm); fprintf(f1,"logfile %s\n",logfile); gtk_combo_box_append_text(GTK_COMBO_BOX(adslname_edit),adsl); } if(f1)fclose(f1); if(chown(str,0,(getgrnam("dip"))->gr_gid)==-1)///do what pppoeconf do in debian linux . g_print("line : %d :save_profile():chown(): %s .\n",__LINE__,strerror(errno)); } //system(str); //fprintf(f1," //g_print("save_profile runed.\n");}void write_password_file(char * username, char * password,char *file){ gboolean found = FALSE; char *line=NULL; size_t len=0; ssize_t read;// GList * list = NULL; umask(0077); char *tmpfile = "/tmp/.pppd.secrets"; FILE * filer = fopen(file,"r"); FILE * filew = fopen(tmpfile,"w"); while((read = getline(&line,&len,filer))!=-1) { if(read >1){ gchar * l = line;// fprintf(stderr,"%s\n",l); gchar ** str_array; str_array = g_strsplit(l,"\"",0); gchar * user_str = str_array[1]; //fprintf(stderr,"username: %s\n",user_str); if(user_str&&!strcmp(username,user_str)){ found = TRUE; char * tmpline; tmpline = malloc(100); if(!tmpline){ g_print("write_password_file:%d:malloc():%s .\n",__LINE__,strerror(errno)); free(line); return ;/// go without work be done. } bzero(tmpline,100); sprintf(tmpline,"\"%s\" * \"%s\"\n",username,password); fprintf(filew,"%s",tmpline); free(tmpline); } else fprintf(filew,"%s",line); } } if(line)free(line); if(!found){ char * tmpline; tmpline = malloc(100); if(!tmpline){ g_print("write_password_file:%d:malloc():%s .\n",__LINE__,strerror(errno)); return ; } bzero(tmpline,100); sprintf(tmpline,"\"%s\" * \"%s\"\n",username,password); fprintf(filew,"%s",tmpline); free(tmpline); } fclose(filer); fclose(filew); unlink(file); rename(tmpfile,file);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -