📄 journal.c
字号:
while (((ret_val = open(lock_file_name, (O_CREAT | O_EXCL), 0700)) == -1) && (counter < 3)) { sleep(1); counter++; } if (ret_val != -1) close(ret_val); return(ret_val);}/* | remove the lock file */void unlock_journal_fd(){ unlink(lock_file_name);}/* | free the memory used in the list of files being edited and their | journal files */void free_db_list(list)struct journal_db *list;{ if (list->next != NULL) free_db_list(list->next); free(list->journal_name); if (list->file_name != NULL) free(list->file_name); free(list);}/* | read the file ~/.aeeinfo (which contains the name of the file | being edited and the name of the journal file associated with it) */struct journal_db *read_journal_db(){ char buffer[4092]; char *tmp; int file_name_len; int journ_name_len; struct journal_db *top_of_list = NULL; struct journal_db *list = NULL; FILE *db_fp; if (lock_journal_fd() == -1) return(NULL); if (db_file_name == NULL) db_file_name = resolve_name("~/.aeeinfo"); db_fp = fopen(db_file_name, "r"); if (db_fp == NULL) { unlock_journal_fd(); return(NULL); } while ((tmp = fgets(buffer, 4092, db_fp)) != NULL) { if (list == NULL) top_of_list = list = (struct journal_db *) malloc(sizeof(struct journal_db)); else { list->next = (struct journal_db *) malloc(sizeof(struct journal_db)); list = list->next; } list->next = NULL; if ((*tmp == ' ') || (*tmp == '\t')) tmp = next_word(tmp); file_name_len = atoi(tmp); tmp = next_word(tmp); journ_name_len = atoi(tmp); if (file_name_len > 0) { tmp = next_word(tmp); list->file_name = (char *) malloc(file_name_len + 1); strncpy(list->file_name, tmp, file_name_len); list->file_name[file_name_len] = (char)NULL; } else list->file_name = NULL; tmp = next_word(tmp); list->journal_name = (char *) malloc(journ_name_len + 1); strncpy(list->journal_name, tmp, journ_name_len); list->journal_name[journ_name_len] = (char)NULL; } fclose(db_fp); return(top_of_list);}/* | Write the file ~/.aeeinfo . */void write_db_file(list)struct journal_db *list;{ struct journal_db *tmp; FILE *db_fp; db_fp = fopen(db_file_name, "w"); if (db_fp == NULL) return; for (tmp = list; (db_fp != NULL) && (tmp != NULL); tmp = tmp->next) { if (tmp->file_name != (char)NULL) { fprintf(db_fp, "%d %d %s %s\n", strlen(tmp->file_name), strlen(tmp->journal_name), tmp->file_name, tmp->journal_name); } else { fprintf(db_fp, "%d %d %s\n", 0, strlen(tmp->journal_name), tmp->journal_name); } } fclose(db_fp); unlock_journal_fd();}/* | add an entry to the file ~/.aeeinfo (a new file being edited and | its associated journal file) */void add_to_journal_db(buffer)struct bufr *buffer;{ struct journal_db *top_of_list; struct journal_db *list; top_of_list = list = read_journal_db(); if (list == (struct journal_db *) -1) /* could not open lock file */ return; if (list == NULL) { top_of_list = list = (struct journal_db *) malloc(sizeof(struct journal_db)); } else { /* | Could check for duplicate file being edited here. */ while (list->next != NULL) list = list->next; list->next = (struct journal_db *) malloc(sizeof(struct journal_db)); list = list->next; } list->next = NULL; list->journal_name = strdup(buffer->journal_file); if (*buffer->full_name != (char)NULL) list->file_name = strdup(buffer->full_name); else list->file_name = NULL; write_db_file(top_of_list); free_db_list(top_of_list);}/* | remove an entry from the file ~/.aeeinfo (the editor is being | exited and it is time to delete the journal file, so remove the | reference) */void rm_journal_db_entry(buffer)struct bufr *buffer;{ struct journal_db *top_of_list; struct journal_db *list; struct journal_db *tmp; int changed = FALSE; top_of_list = list = read_journal_db(); if (list == (struct journal_db *) NULL) /* could not open db file */ { return; } if ((!strcmp(buffer->journal_file, top_of_list->journal_name)) && ((top_of_list->file_name == NULL) || ((top_of_list->file_name != NULL) && (!strcmp(buffer->full_name, top_of_list->file_name))))) { top_of_list = list->next; tmp = list; changed = TRUE; } else { while (((list->next != NULL) && (!((list->next->file_name != NULL) && (!strcmp(buffer->full_name, list->next->file_name) && !strcmp(buffer->journal_file, list->next->journal_name))))) && (!((list->next->file_name == NULL) && !strcmp(buffer->journal_file, list->next->journal_name)))) { list = list->next; } if (list->next != NULL) { tmp = list->next; list->next = list->next->next; changed = TRUE; } } if (changed == TRUE) { if (top_of_list == NULL) /* no entries for file */ { unlink(db_file_name); unlock_journal_fd(); } else { write_db_file(top_of_list); } tmp->next = NULL; free_db_list(tmp); } else unlock_journal_fd(); if (top_of_list != NULL) free_db_list(top_of_list);}/* | check for the existence of a directory, and if it doesn't exist | create it (will recursively go through and create the directory | from the highest location where a directory does exist) */intcreate_dir(name)char *name;{ char *path; struct stat buf; int ret_val; ret_val = stat(name, &buf); if (ret_val == -1) { path = ae_dirname(name); ret_val = create_dir(path); if (ret_val != -1) ret_val = mkdir(name, 0700); free(path); } return(ret_val);}/* | determine a name for a journal file and make sure no other journal | file of that name already exists (if one does, create a name based | on the current date and time) */void journal_name(buffer, file_name)struct bufr *buffer;char *file_name;{ char *temp, *buff, *name; int count; if (*file_name == (char)NULL) file_name = "no_file_journal"; if ((journal_dir != NULL) && (*journal_dir != (char) NULL)) { name = ae_basename(file_name); buffer->journal_file = temp = xalloc(strlen(name) + 5 + strlen(journal_dir)); strcpy(temp, journal_dir); strcat(temp, "/"); strcat(temp, name); } if (buffer->journal_file == NULL) { buffer->journal_file = temp = xalloc(strlen(file_name) + 4); strcpy(temp, file_name); } buff = ae_basename(temp); temp = buff; if (strlen(temp) >= (MAX_NAME_LEN - 3)) { for (count = 1; count < (MAX_NAME_LEN - 3); count++) temp++; } else { while(*temp != (char) NULL) temp++; } copy_str(".rv", temp);}/* | create and initialize the journal file */void open_journal_for_write(buffer)struct bufr *buffer;{ int ret_val; int length; int counter; char *temp; struct tm *time_info; time_t t; if (((buffer->journ_fd = open(buffer->journal_file, (O_CREAT | O_EXCL | O_WRONLY), 0600)) == -1) && (errno == EEXIST)) { /* | file already exists */ buffer->journal_file = realloc(buffer->journal_file, (strlen(buffer->journal_file) + 17)); /* | find the last '/' */ temp = strrchr(buffer->journal_file, '/'); if (temp != NULL) temp++; else temp = buffer->journal_file; /* | create a file name based on the date, e.g., | 950913221206.rv | Not a guarantee of uniqueness, but certainly not | likely to be repeated. */ t = time(NULL); time_info = gmtime(&t); ret_val = strftime(temp, 16, "%y%m%d%H%M%S.rv", time_info); /* | For some extra paranoia, check this one, and append | a character (a-z) to get uniqueness. */ counter = 'a'; length = strlen (buffer->journal_file); while (((buffer->journ_fd = open(buffer->journal_file, (O_CREAT | O_EXCL | O_WRONLY), 0600)) == -1) && (errno == EEXIST) && (counter <= 'z')) { buffer->journal_file[length] = counter; buffer->journal_file[length + 1] = (char) NULL; counter++; } } if (buffer->journ_fd == -1) { wprintw(com_win, cant_opn_rcvr_fil_msg); buffer->journalling = FALSE; return; } add_to_journal_db(buffer); if (*buffer->full_name != (char) NULL) { ret_val = write(buffer->journ_fd, buffer->full_name, strlen(buffer->full_name)); } ret_val |= write(buffer->journ_fd, "\n", 1); journ_info_init(buffer, buffer->first_line);}/* | delete a journal file and delete its entry from the file ~/.aeeinfo */void remove_journal_file(buffer)struct bufr *buffer;{ close(buffer->journ_fd); unlink(buffer->journal_file); rm_journal_db_entry(buffer);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -