int edit_entry (const char *datafile, long pos) { FILE *fp = NULL; char filename[PATH_MAX]; pid_t process_id = 0; int status = 0; struct stat sb; time_t modified_time = 0; vc_component *v = NULL; int ret_val = -1; /* retrieve the entry for editing */ fp = fopen (datafile, "r"); fseek (fp, pos, SEEK_SET); v = parse_vcard_file (fp); fclose (fp); fp = NULL; /* open a temp file for editing */ tmpnam (filename); fp = fopen (filename, "w"); /* dump the entry into the temp file for editing */ fprintf_vcard (fp, v); fclose (fp); fp = NULL; /* record when the file has been modified */ stat (filename, &sb); modified_time = sb.st_mtime; endwin (); process_id = fork (); if (process_id < 0) { /* could not fork... check errno */ } else if (0 == process_id) { /* child is running */ /* replace process image with the editor */ execlp (editor, editor_basename, filename, NULL); _exit (2); /* execlp failed */ } else { /* parent is running */ waitpid (process_id, &status, 0); } /* check if the temp file has been modified */ stat (filename, &sb); if (modified_time != sb.st_mtime) { /* need to change the datafile */ update_datafile (datafile, pos, filename); ret_val = EDIT_SUCCESSFUL; } else { ret_val = EDIT_ABORTED; } remove (filename); /* put everything back to normal */ refresh (); initscr (); keypad (stdscr, TRUE); /* enable keypad for use of arrow keys */ nonl (); /* tell curses not to do NL->CR/NL on output */ cbreak (); /* take input chars immediately */ noecho (); return ret_val; }
int main(int agv,char *agc[]) { char lstname[256],dataname[256]; if (agv==1) { printf("Need Argument\n"); return -1; } if (agc[1][0]=='x') { char filename[256]; int i; strcpy(lstname,agc[2]); strcpy(dataname,agc[2]); strcat(lstname,".lst"); strcat(dataname,".wdf"); if (!file_exist(lstname)) { printf("Can't open list file[.lst]\n"); return -1; } if (!file_exist(dataname)) { printf("Can't open data file[.wdf]\n"); return -1; } _mkdir(agc[2]); FileList list; int n; n=list.ReadListOnly(lstname); std::vector<FILE_LST> old_list; int old_number=read_lst(dataname,old_list); FILE *df; df=fopen(dataname,"rb"); for (i=0;i<n;i++) { FILE *f; int j; static char buffer[1024]; strcpy(filename,agc[2]); strcat(filename,"\\"); int n; n=list.IsFileExist(i,old_list,old_number); printf("%s [%x] ",list.GetName(i),list.GetID(i)); if (n<0) { printf(" Can't find\n"); continue; } strcat(filename,list.GetName(i)); char *path=strchr(filename,'\\'); path=strchr(path+1,'\\'); if (path) { path[0]=0; _mkdir(filename); path[0]='\\'; } f=fopen(filename,"wb"); fseek(df,old_list[n].offset,SEEK_SET); for (j=0;j<old_list[n].size-1024;j+=1024) { fread(buffer,1,1024,df); fwrite(buffer,1,1024,f); } j=old_list[n].size-j; fread(buffer,1,j,df); fwrite(buffer,1,j,f); fclose(f); printf(" Extracted\n"); } fclose(df); return 0; } strcpy(lstname,agc[1]); strcpy(dataname,agc[1]); strcat(lstname,".lst"); strcat(dataname,".wdf"); if (!file_exist(lstname)) { printf("Can't open list file[.lst]\n"); return -1; } if (file_exist(dataname)) { printf("Updating Data File...\n"); update_datafile(dataname,lstname); } else { printf("Creating New Data File...\n"); create_new(dataname,lstname); } return 0; }