void get_results (FILE * fp, const char *query_string, const char *misc_field, const int all_emails, int *searched, query_result * results, int *rc) { vc_component *v = NULL; char *s_result = NULL; char *email = NULL; char *name = NULL; char *misc = NULL; vc_component *fn = NULL; query_result * r = NULL; r = results; *rc = 0; *searched = 0; for (v = parse_vcard_file (fp); NULL != v; v = parse_vcard_file (fp)) { (*searched)++; fn = vc_get_next_by_name (v, VC_FORMATTED_NAME); name = vc_get_value (fn); misc = get_misc_value (v, misc_field); if (all_emails) { for (fn = vc_get_next_by_name (v, VC_EMAIL); NULL != fn; fn = vc_get_next_by_name(fn, VC_EMAIL)) { email = vc_get_value (fn); r = add_result(query_string, name, misc, email, r, rc); } } else { email = vc_get_preferred_email (v); r = add_result(query_string, name, misc, email, r, rc); } free (misc); vc_delete_deep (v); v = NULL; } }
static void update_datafile (const char *datafile, long pos, const char *new_entry_filename) { FILE *tfp = NULL; FILE *dfp = NULL; FILE *efp = NULL; char tmp_datafile[PATH_MAX]; int ch = 0; long i = 0; int rc = 0; strcpy (tmp_datafile, datafile); strcat (tmp_datafile, ".tmp"); dfp = fopen (datafile, "r"); tfp = fopen (tmp_datafile, "w"); /* copy the datafile into the temp file but only upto where to change */ i = 0; ch = getc (dfp); while (ch != EOF && i < pos) { putc (ch, tfp); ch = getc (dfp); i++; } efp = fopen (new_entry_filename, "r"); /* copy the new entry into the temp file */ putc ('\n', tfp); ch = getc (efp); while (ch != EOF) { putc (ch, tfp); ch = getc (efp); } fclose (efp); /* entry file no longer needed */ /* move to just after the changed entry position */ fseek (dfp, pos, SEEK_SET); parse_vcard_file (dfp); /* copy the rest of the datafile over to the temp file */ ch = getc (dfp); while (ch != EOF) { putc (ch, tfp); ch = getc (dfp); } fclose (dfp); /* datafile no longer needed */ fclose (tfp); /* temp file no longer needed */ /* replace the datafile with the temp datafile */ rename (tmp_datafile, datafile); if (-1 == rc) { fprintf (stderr, "error with rename...\n"); perror ("rolo"); } }
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 argc, char *argv[]) { vc_component *v = NULL; fpos_t *fpos = NULL; long pos = 0; FILE *fp = NULL; ITEM *it = NULL; int entry_number = 0; bool done = FALSE; int win_state = WINDOW_INDEX; int command = 0; set_defaults (); process_command_line_args (argc, argv); /* * process_environment_variables(); * process_configuration_file(); */ signal (SIGINT, finish); /* catch interrupt for exiting */ signal (SIGWINCH, resize); /* catch interrupt for resizing */ 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 (); init_index (data_path); set_index_help_fcn (show_index_help); init_view (); set_view_help_fcn (show_view_help); init_edit (); set_edit_help_fcn (show_edit_help); init_help (); while (!done) { switch (win_state) { case WINDOW_INDEX: /*------------------- display the index -------------------*/ display_index (); command = process_index_commands (); switch (command) { case INDEX_COMMAND_VIEW: win_state = WINDOW_VIEW; break; case INDEX_COMMAND_RAW_VIEW: win_state = WINDOW_RAW_VIEW; break; case INDEX_COMMAND_EDIT: win_state = WINDOW_EDIT; break; case INDEX_COMMAND_ADD: win_state = WINDOW_ADD; break; case INDEX_COMMAND_DELETE: win_state = WINDOW_DELETE; break; case INDEX_COMMAND_QUIT: done = TRUE; break; default: break; } break; case WINDOW_RAW_VIEW: /*------------------------------------------------- view the currently selected item with the pager -------------------------------------------------*/ it = get_current_item (); /* only display if there is an item that is selected */ if (NULL == it) { v = NULL; } else { fpos = (fpos_t *) item_userptr (it); fp = fopen (data_path, "r"); fsetpos (fp, fpos); v = parse_vcard_file (fp); fclose (fp); } if (v != NULL) { raw_view (v); vc_delete_deep (v); v = NULL; } win_state = WINDOW_INDEX; break; case WINDOW_VIEW: /*---------------------------------- view the currently selected item ----------------------------------*/ it = get_current_item (); /* only display if there is an item that is selected */ if (NULL == it) { v = NULL; } else { fpos = (fpos_t *) item_userptr (it); fp = fopen (data_path, "r"); fsetpos (fp, fpos); v = parse_vcard_file (fp); fclose (fp); } if (v != NULL) { entry_number = get_entry_number (it); view_vcard (entry_number, v); command = process_view_commands (); switch (command) { case VIEW_COMMAND_EDIT: win_state = WINDOW_EDIT; break; case VIEW_COMMAND_INDEX: win_state = WINDOW_INDEX; break; case VIEW_COMMAND_PREVIOUS: select_previous_item (); win_state = WINDOW_VIEW; break; case VIEW_COMMAND_NEXT: select_next_item (); win_state = WINDOW_VIEW; break; default: break; } } else { win_state = WINDOW_INDEX; } vc_delete_deep (v); v = NULL; break; case WINDOW_EDIT: /*-------------- edit a vcard --------------*/ it = get_current_item (); /* only display if there is an item that is selected */ if (NULL != it) { fpos = (fpos_t *) item_userptr (it); fp = fopen (data_path, "r"); fsetpos (fp, fpos); pos = ftell (fp); fclose (fp); fp = NULL; if (EDIT_SUCCESSFUL == edit_entry (data_path, pos)) { refresh_index (); } } win_state = WINDOW_INDEX; break; case WINDOW_ADD: if (ADD_SUCCESSFUL == add_entry (data_path)) { refresh_index (); } win_state = WINDOW_INDEX; break; case WINDOW_DELETE: it = get_current_item (); /* only delete if there is an item that is selected */ if (NULL != it) { fpos = (fpos_t *) item_userptr (it); fp = fopen (data_path, "r"); fsetpos (fp, fpos); pos = ftell (fp); fclose (fp); fp = NULL; if (DELETE_SUCCESSFUL == delete_entry (data_path, pos)) { refresh_index (); } } win_state = WINDOW_INDEX; break; default: break; } } finish (0); exit (EXIT_SUCCESS); return (0); }