📄 file.c
字号:
value = stat(tmp_file, &buf); buf.st_mode &= ~07777; if ((value != -1) && (buf.st_mode != 0100000) && (buf.st_mode != 0)) { if (input_file) { sprintf(buffer, file_is_dir_msg, ae_basename(tmp_file)); file_error_menu[0].item_string = buffer; file_error_menu[1].item_string = continue_prompt; menu_op(file_error_menu); if (curr_buff->main_buffer) quit(""); } else { wprintw(com_win, file_is_dir_msg, tmp_file); wrefresh(com_win); } recv_file = FALSE; input_file = FALSE; return(FALSE); } if ((get_fd = open(tmp_file, O_RDONLY)) == -1) { wmove(com_win,0,0); wclrtoeol(com_win); if (input_file) { if ((get_fp= fopen(tmp_file,"w")) == NULL) { if ((errno == ENOTDIR) || (errno == ENOENT)) { file_error_menu[0].item_string = path_not_a_dir_msg; } else if ((errno == EACCES) || (errno == EROFS) || (errno == ETXTBSY) || (errno == EFAULT)) { sprintf(buffer, access_not_allowed_msg, ae_basename(in_file_name)); file_error_menu[0].item_string = buffer; } file_error_menu[1].item_string = continue_prompt; menu_op(file_error_menu); if (curr_buff->main_buffer) quit(""); recv_file = FALSE; input_file = FALSE; return(FALSE); } else { fclose(get_fp); unlink(tmp_file); wprintw(com_win, new_file_msg, tmp_file); } } else wprintw(com_win, cant_open_msg, tmp_file); wrefresh(com_win); recv_file = FALSE; input_file = FALSE; } else get_file(tmp_file); if (recv_file) /* got a file */ { close(get_fd); recv_file = FALSE; line_num = curr_buff->curr_line->line_number; curr_buff->scr_vert = tmp_vert; curr_buff->scr_horz = tmp_horz; if (input_file) curr_buff->curr_line = curr_buff->first_line; else curr_buff->curr_line = tmp_line; curr_buff->pointer = curr_buff->curr_line->line; midscreen(curr_buff->scr_vert, curr_buff->position); if (input_file) /* get the file on the command line */ { if (access(in_file_name, 2)) { if ((errno == ENOTDIR) || (errno == EACCES) || (errno == EROFS) || (errno == ETXTBSY) || (errno == EFAULT)) wprintw(com_win, ", %s", read_only_msg); } if (start_at_line != NULL) { line_num = atoi(start_at_line) - 1; move_rel("d", line_num); line_num = 0; start_at_line = NULL; } wrefresh(com_win); input_file = FALSE; } else { wmove(com_win, 0, 0); werase(com_win); wprintw(com_win, fin_read_file_msg, tmp_file); wrefresh(com_win); } in = 0; } } wrefresh(com_win); return(TRUE);}/* | get_file is called from two places: check_fp and sh_command | When called from check_fp, recv_file is set, and input_file may be | set, indicating this is a file to be edited. | When called from sh_command, it is reading from a pipe from a forked | process. */void get_file(file_name) /* read specified file into current buffer */char *file_name;{ int can_write; /* file mode allows write */ int can_read; /* file has at least one character */ int length; /* length of line read by read */ int append; /* should text be appended to current line */ struct text *temp_line; if (recv_file) /* if reading a file */ { wmove(com_win, 0, 0); wclrtoeol(com_win); wprintw(com_win, reading_file_msg, file_name); if ((can_write = access(file_name, 2))) /* check permission to write */ { if ((errno == ENOTDIR) || (errno == EACCES) || (errno == EROFS) || (errno == ETXTBSY) || (errno == EFAULT)) wprintw(com_win, read_only_msg); } wrefresh(com_win); } if (curr_buff->curr_line->line_length > 1) /* if current line is not blank */ { insert_line(FALSE); left(FALSE); append = FALSE; } else append = TRUE; can_read = FALSE; /* test if file has any characters */ while (((length = read(get_fd, in_string, 512)) != 0) && (length != -1)) { can_read = TRUE; /* if set file has at least 1 character */ get_line(length, in_string, &append); } if ((can_read) && (curr_buff->curr_line->line_length == 1)) { temp_line = curr_buff->curr_line->prev_line; temp_line->next_line = curr_buff->curr_line->next_line; if (temp_line->next_line != NULL) temp_line->next_line->prev_line = temp_line; if (curr_buff->journalling) remove_journ_line(curr_buff, curr_buff->curr_line); free(curr_buff->curr_line); curr_buff->curr_line = temp_line; curr_buff->num_of_lines--; } if (input_file) /* if this is the file to be edited display number of lines */ { wmove(com_win, 0, 0); wclrtoeol(com_win); wprintw(com_win, file_read_lines_msg, in_file_name, curr_buff->curr_line->line_number); wrefresh(com_win); } else curr_buff->changed = TRUE;}void get_line(length, in_string, append) /* read string and split into lines */int length; /* length of string read by read */char *in_string; /* string read by read */int *append; /* TRUE if must append more text to end of current line */{ char *str1; char *str2; int num; /* offset from start of string */ int char_count; /* length of new line (or added portion */ int temp_counter; /* temporary counter value */ struct text *tline; /* temporary pointer to new line */ int first_time; /* if TRUE, the first time through the loop */ static int last_char_cr = FALSE; /* set if the last character of a buffer was a carriage return */ str2 = in_string; num = 0; first_time = TRUE; while (num < length) { if (!first_time) { if (num < length) { str2++; num++; } } else first_time = FALSE; str1 = str2; char_count = 1; /* find end of line */ if (last_char_cr) { last_char_cr = FALSE; if (*str2 == '\n') { str1++; str2++; num++; } } if (text_only) { while ((*str2 != '\n') && (num < length)) { if (((*str2 == '\r') && (*(str2 + 1) == '\n')) || ((*str2 == '\r') && ((num + 1) == length))) { curr_buff->dos_file = TRUE; if ((num + 1) == length) last_char_cr = TRUE; } else char_count++; str2++; num++; } } else { while ((*str2 != '\n') && (num < length)) { char_count++; str2++; num++; } } if (!(*append)) /* if not append to current line, insert new one */ { tline = txtalloc(); /* allocate data structure for next line */ tline->line_number = curr_buff->curr_line->line_number + 1; tline->next_line = curr_buff->curr_line->next_line; tline->prev_line = curr_buff->curr_line; curr_buff->curr_line->next_line = tline; if (tline->next_line != NULL) tline->next_line->prev_line = tline; curr_buff->curr_line = tline; curr_buff->num_of_lines++; curr_buff->curr_line->line = curr_buff->pointer = xalloc(char_count); curr_buff->curr_line->line_length = char_count; curr_buff->curr_line->max_length = char_count; } else { curr_buff->pointer = resiz_line(char_count, curr_buff->curr_line, curr_buff->curr_line->line_length); curr_buff->curr_line->line_length += (char_count - 1); } for (temp_counter = 1; temp_counter < char_count; temp_counter++) { *curr_buff->pointer = *str1; curr_buff->pointer++; str1++; } if (((*str2 == '\n') || ((num == length) && (num < 512))) && (curr_buff->journalling)) write_journal(curr_buff, curr_buff->curr_line); *curr_buff->pointer = (char) NULL; *append = FALSE; curr_buff->curr_line->vert_len = (scanline(curr_buff->curr_line, curr_buff->curr_line->line_length) / COLS) + 1; if ((num == length) && (*str2 != '\n')) *append = TRUE; }}char *is_in_string(string, substring) /* a strstr() look-alike for systems without strstr() */char * string, *substring;{ char *full, *sub; for (sub = substring; (sub != NULL) && (*sub != (char)NULL); sub++) { for (full = string; (full != NULL) && (*full != (char)NULL); full++) { if (*sub == *full) return(full); } } return(NULL);}/* | handle names of the form "~/file", "~user/file", | "$HOME/foo", "~/$FOO", etc. */char *resolve_name(name)char *name;{ char long_buffer[1024]; char short_buffer[128]; char *buffer; char *slash; char *tmp; char *start_of_var; int offset; int index; int counter; struct passwd *user; char *name_buffer; name_buffer = (char *) malloc(strlen(name) + 1); strcpy(name_buffer, name); if (name_buffer[0] == '~') { if (name_buffer[1] == '/') { index = getuid(); user = (struct passwd *) getpwuid(index); slash = name_buffer + 1; } else { slash = strchr(name_buffer, '/'); if (slash == NULL) return(name_buffer); *slash = (char) NULL; user = (struct passwd *) getpwnam((name_buffer + 1)); *slash = '/'; } if (user == NULL) { return(name_buffer); } buffer = xalloc(strlen(user->pw_dir) + strlen(slash) + 1); strcpy(buffer, user->pw_dir); strcat(buffer, slash); } else buffer = name_buffer; if (is_in_string(buffer, "$")) { tmp = buffer; index = 0; while ((*tmp != (char) NULL) && (index < 1024)) { while ((*tmp != (char) NULL) && (*tmp != '$') && (index < 1024)) { long_buffer[index] = *tmp; tmp++; index++; } if ((*tmp == '$') && (index < 1024)) { counter = 0; start_of_var = tmp; tmp++; if (*tmp == '{') /* } */ /* bracketed variable name */ { tmp++; /* { */ while ((*tmp != (char) NULL) && (*tmp != '}') && (counter < 128)) { short_buffer[counter] = *tmp; counter++; tmp++; } /* { */ if (*tmp == '}') tmp++; } else { while ((*tmp != (char) NULL) && (*tmp != '/') && (*tmp != '$') && (counter < 128)) { short_buffer[counter] = *tmp; counter++; tmp++; } } short_buffer[counter] = (char) NULL; if ((slash = getenv(short_buffer)) != NULL) { offset = strlen(slash); if ((offset + index) < 1024) strcpy(&long_buffer[index], slash); index += offset; } else { while ((start_of_var != tmp) && (index < 1024)) { long_buffer[index] = *start_of_var; start_of_var++; index++; } } } } if (index == 1024) return(buffer); else long_buffer[index] = (char) NULL; if (name_buffer != buffer) free(buffer); buffer = xalloc(index + 1); strcpy(buffer, long_buffer); } return(buffer);}int write_file(file_name) /* write current buffer to specified file */char *file_name;{ char cr, lf; char *tmp_point; struct text *out_line; int lines, charac; int temp_pos; int write_flag; int can_write; int write_ret = 0; clr_cmd_line = TRUE; charac = lines = 0; aee_write_status = FALSE; if (strcmp(curr_buff->full_name, file_name)) { wmove(com_win, 0, 0); wclrtoeol(com_win); can_write = access(file_name, 2); if (can_write) { write_flag = FALSE; if (errno == ENOTDIR) wprintw(com_win, path_not_a_dir_msg); else if ((errno == EACCES) || (errno == EROFS) || (errno == ETXTBSY) || (errno == EFAULT)) wprintw(com_win, no_write_access_msg, file_name); else if (errno != ENOENT) { tmp_point = get_string(file_exists_prompt, TRUE); if ((toupper(*tmp_point) == toupper(*no_char)) || (*tmp_point == (char) NULL)) write_flag = FALSE; else write_flag = TRUE; free(tmp_point); } else write_flag = TRUE; } else { tmp_point = get_string(file_exists_prompt, TRUE); if ((toupper(*tmp_point) == toupper(*no_char)) || (*tmp_point == (char) NULL)) write_flag = FALSE; else write_flag = TRUE; free(tmp_point); } wrefresh(com_win); } else { wmove(com_win,0,0); wclrtoeol(com_win); can_write = access(file_name, 2); if (can_write) { write_flag = FALSE; if (errno == ENOTDIR) wprintw(com_win, path_not_a_dir_msg); else if ((errno == EACCES) || (errno == EROFS) || (errno == ETXTBSY) || (errno == EFAULT)) wprintw(com_win, no_write_access_msg, file_name); else write_flag = TRUE; } else { change = FALSE; write_flag = TRUE; } wrefresh(com_win); } if (write_flag) { if ((write_fp = fopen(file_name, "w")) == NULL) { wmove(com_win,0,0); wclrtoeol(com_win); wprintw(com_win, cant_creat_fil_msg, file_name); wrefresh(com_win); return(FALSE); } else { wmove(com_win,0,0); wclrtoeol(com_win); wprintw(com_win, writing_msg, file_name); wrefresh(com_win); cr = '\r'; lf = '\n'; out_line = curr_buff->first_line; while (out_line != NULL) { temp_pos = 1; tmp_point= out_line->line; while ((temp_pos < out_line->line_length) && (write_ret != EOF)) { write_ret = putc(*tmp_point, write_fp); tmp_point++; temp_pos++; } charac += out_line->line_length; out_line = out_line->next_line; if ((text_only) && (curr_buff->dos_file)) putc(cr, write_fp); putc(lf, write_fp); lines++; } fclose(write_fp); wmove(com_win,0,0); wclrtoeol(com_win); wprintw(com_win, file_written_msg, file_name,lines,charac); if (write_ret == EOF) { wstandout(com_win); wprintw(com_win, write_err_msg); wstandend(com_win); } wrefresh(com_win); aee_write_status = TRUE; return(TRUE); } } else return(FALSE);}int file_write_success(){ return (aee_write_status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -