📄 prefer~1.cpp
字号:
if(new_name != NULL) buff_size=strlen(new_name); buff_size+=strlen(new_email) + sizeof("\t\"\" <>\n"); buff=(char*)malloc(buff_size); if(buff == NULL) return(-1); strcpy(buff,"\t\""); if(new_name != NULL) strcat(buff,new_name); strcat(buff,"\" <"); strcat(buff,new_email); strcat(buff,">\n");//setup to delete email if( (find_addr_book_start(file)) != 0) { free(buff); return(-1); } if(find_email_position(file,old_name,old_email, &file_ptr_write,&file_ptr_read) != 0) { syslog(LOG_MAIL | LOG_ERR,"find email position for delete failed."); free(buff); return(-1); } if( insert_line_in_file(file,buff,file_ptr_read,file_ptr_write) != 0) { syslog(LOG_MAIL | LOG_ERR,"error modifying email"); free(buff); return(-1); } free(buff); return(0);}////////////////////////////////////////////////////////////////////This function will point to the first email entry following// "address_book(main)"// return 0 if found// return -1 on errorint find_addr_book_start(FILE* file){ char* file_buff=NULL; size_t file_buff_size=0; ssize_t b_read=0; int return_value=0; rewind(file); do { b_read=getline(&file_buff,&file_buff_size,file); if(b_read <= 0 || file_buff == NULL) return_value=-1; } while(strcmp(file_buff,"address_book(main)\n") != 0 && return_value == 0); if(file_buff != NULL) free(file_buff); return(return_value);}////////////////////////////////////////////////////////////////////Returns file pointer to position where email should be inserted into// preference file.// Should have file pointer already pointing to first email entry.// name can be null// returns -1 on errorlong int find_next_email_order(FILE* file,char* name,char* email){ char* file_buff=NULL; size_t file_buff_size=0; ssize_t b_read=0; long int file_ptr=-1; status_type status=st_pending; addr_t addr; addr.name=NULL; addr.email=NULL; if(file == NULL || email == NULL) return(-1); do { file_ptr=ftell(file); b_read=getline(&file_buff,&file_buff_size,file); if(b_read <= 0 || file_buff == NULL) status=st_found; else { if(*file_buff == '\t') { if(fill_addr(&addr,file_buff) == 0) { if(name != NULL) { if(addr.name != NULL) { if(strcasecmp(addr.name,name) > 0) status=st_found; else if(strcasecmp(addr.name,name) == 0) { if(strcasecmp(addr.email,email) > 0) status=st_found; } } } else if(addr.name != NULL) status=st_found; else { if(strcasecmp(addr.email,email) > 0) status=st_found; } if(addr.name != NULL) free(addr.name); free(addr.email); } else status=st_error; } else status=st_found; } } while(status == st_pending); if(status == st_error) file_ptr=-1; return(file_ptr);}//////////////////////////////////////////////////////////////////int find_email_position(FILE* file, char* name, char* email, long int* file_ptr_start, long int* file_ptr_end){ char* file_buff=NULL; size_t file_buff_size=0; ssize_t b_read=0; status_type status=st_pending; addr_t addr; addr.name=NULL; addr.email=NULL; *file_ptr_start=-1; *file_ptr_end=-1; if(file == NULL || email == NULL) return(-1); do { *file_ptr_start=ftell(file); b_read=getline(&file_buff,&file_buff_size,file); if(b_read <= 0 || file_buff == NULL) status=st_error; else { if(*file_buff == '\t') { fill_addr(&addr,file_buff); if(name != NULL) { if(addr.name != NULL) { if(strcasecmp(addr.name,name) == 0) { if(strcasecmp(addr.email,email) == 0) status=st_found; } } } else if(addr.name == NULL) { if(strcasecmp(addr.email,email) == 0) status=st_found; } if(addr.name != NULL) { free(addr.name); addr.name=NULL; } if(addr.email != NULL) { free(addr.email); addr.email=NULL; } } else status=st_done; } } while(status == st_pending); if(status != st_found) { *file_ptr_start=-1; *file_ptr_end=-1; return(-1); } *file_ptr_end=ftell(file); return(0);}//////////////////////////////////////////////////////////////////char* get_default_hostname(void){#define HOSTNAMESIZE 80 char* file_buff=NULL; size_t file_buff_size=0; ssize_t b_read=0; FILE* default_pref_file=NULL; char* hostname=NULL; char* tmp_ptr=NULL; default_pref_file=fopen(PREFDIR "/default","r"); if(default_pref_file != NULL) { do { b_read=getline(&file_buff,&file_buff_size,default_pref_file); if(b_read <= 0 || file_buff == NULL) { if(file_buff != NULL) free(file_buff); fclose(default_pref_file); default_pref_file=NULL; } else if(strncasecmp(file_buff,"hostname",sizeof("hostname")-1) == 0) { tmp_ptr=strrpcbrk(file_buff," \t'\"\n"); if(tmp_ptr != NULL) *(tmp_ptr+1)='\0'; tmp_ptr=strchr(file_buff,'='); if(tmp_ptr != NULL) { tmp_ptr=strpcbrk(tmp_ptr+1," \t'\"\n"); if(tmp_ptr != NULL) { if( (hostname=strdup(tmp_ptr)) == NULL) { syslog(LOG_MAIL | LOG_ERR,"Out of memory (%s)", strerror(errno)); fclose(default_pref_file); default_pref_file=NULL; return(NULL); } } } } } while(hostname == NULL && default_pref_file != NULL); if(file_buff != NULL) free(file_buff); fclose(default_pref_file); default_pref_file=NULL; } if(hostname != NULL) return(hostname); hostname=(char*) malloc(HOSTNAMESIZE); if(hostname == NULL) { syslog(LOG_MAIL | LOG_ERR,"Out of memory(%s)",strerror(errno)); return(NULL); } if(gethostname(hostname,HOSTNAMESIZE) != 0) syslog(LOG_MAIL | LOG_ERR,"Hostname too big (%s)",strerror(errno)); return(hostname); }//////////////////////////////////////////////////////////////////int get_reply_to(void){ char* hostname=NULL; if(user_pref.reply_to.email != NULL) return(0); if(user_ses.loginname == NULL) return(-1); if(user_pref.reply_to.name == NULL && user_ses.name != NULL) { user_pref.reply_to.name=strdup(user_ses.name); if(user_pref.reply_to.name == NULL) { syslog(LOG_MAIL | LOG_ERR,"Out of memory (%s)",strerror(errno)); return(-1); } } if(user_pref.reply_to.email == NULL) { if( (hostname=get_default_hostname()) == NULL) { syslog(LOG_MAIL | LOG_ERR,"get hostname failed"); return(-1); } user_pref.reply_to.email=(char*) malloc(strlen(user_ses.loginname)+strlen(hostname)+2); if(user_pref.reply_to.email == NULL) { syslog(LOG_MAIL | LOG_ERR,"Out of memory (%s)",strerror(errno)); free(hostname); return(-1); } strcpy(user_pref.reply_to.email,user_ses.loginname); strcat(user_pref.reply_to.email,"@"); strcat(user_pref.reply_to.email,hostname); free(hostname); } return(0);}//////////////////////////////////////////////////////////////////int save_pref_entry(char* key, char* value){ FILE* pref_file=NULL; long int start_ptr=0; long int end_ptr=0; char* file_buff=NULL; size_t file_buff_size=0; ssize_t b_read=0; char* new_value=NULL; size_t key_size=0; int status=0; if(key == NULL || value == NULL) return(-1); new_value=(char*) malloc(strlen(key)+strlen(value)+sizeof("=\n")); if(new_value == NULL) { syslog(LOG_MAIL | LOG_ERR,"Out of memory (%s)",strerror(errno)); return(-1); } strcpy(new_value,key); strcat(new_value,"="); strcat(new_value,value); strcat(new_value,"\n"); pref_file=open_user_pref_file("r+",&status); if(pref_file == NULL && status == 0) pref_file=open_user_pref_file("w+",NULL); if(pref_file == NULL) { free(new_value); new_value=NULL; return(-1); } key_size=strlen(key); status=0; do { start_ptr=ftell(pref_file); b_read=getline(&file_buff, &file_buff_size, pref_file); end_ptr=ftell(pref_file); if(b_read >= 0 && file_buff != NULL) if( strncasecmp(key,file_buff,key_size) == 0) status=1; } while(b_read >= 0 && status == 0); if(file_buff != NULL) { free(file_buff); file_buff=NULL; } if(start_ptr == -1 || end_ptr == -1) { free(new_value); fclose(pref_file); syslog(LOG_MAIL | LOG_ERR,"ftell failed"); return(-1); } if(status == 1) { if( insert_line_in_file(pref_file,new_value,end_ptr,start_ptr) != 0) { free(new_value); fclose(pref_file); syslog(LOG_MAIL | LOG_ERR,"could not add line to pref file"); return(-1); } } else { if( fseek(pref_file,0,SEEK_END) != 0) { free(new_value); fclose(pref_file); syslog(LOG_MAIL | LOG_ERR,"fseek failed"); return(-1); } if( fprintf(pref_file,"%s",new_value) < 0) { free(new_value); fclose(pref_file); syslog(LOG_MAIL | LOG_ERR,"error writing to pref file %s", user_ses.loginname); return(-1); } } free(new_value); fclose(pref_file); return(0);}//////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -