📄 file.c
字号:
int attempt_edit_display( char *fname, int update_type, int file_mode,
int bin_len )
{
register int rc;
WINDOW *win;
rc = edit_file( fname, file_mode, bin_len );
if (rc != ERROR) {
rc = initialize_window( );
if (rc != ERROR) {
win = g_status.current_window;
if (update_type == LOCAL) {
if (g_status.command != DefineGrep &&
g_status.command != DefineRegXGrep &&
g_status.command != RepeatGrep)
redraw_current_window( win );
show_file_count( g_status.file_count );
show_window_count( g_status.window_count );
show_avail_mem( );
} else if (update_type == GLOBAL)
redraw_screen( win );
if (win->file_info->new_file) {
g_status.command = AddLine;
insert_newline( win );
win->file_info->modified = FALSE;
}
}
}
return( rc );
}
void make_backup_fname( file_infos *file )
{
char name[MAX_COLS];
char *p;
int i;
int len;
if (file->new_file)
file->backed_up = TRUE;
else {
assert( strlen( file->file_name ) < MAX_COLS );
strcpy( name, file->file_name );
len = strlen( name );
for (i=len,p=name+len; i>=0; i--) {
if (*p == '.')
break;
else if (*p == '\\' || *p == ':') {
p = name + len;
break;
} else if (i == 0) {
p = name + len;
break;
}
--p;
}
assert( strlen( name ) < MAX_COLS );
strcpy( p, ".bak" );
strcpy( file->backup_fname, name );
}
}
int file_file( WINDOW *window )
{
if (save_file( window ) == OK)
finish( window );
return( OK );
}
int save_file( WINDOW *window )
{
char name[MAX_COLS];
register file_infos *file;
int rc;
line_list_ptr temp_ll;
entab_linebuff( );
if (un_copy_line( window->ll, window, TRUE ) == ERROR)
return( ERROR );
file = window->file_info;
if (file->modified == FALSE)
return( OK );
assert( strlen( file->file_name ) < MAX_COLS );
strcpy( name, file->file_name );
if (strlen( name ) == 0)
rc = save_as_file( window );
else {
rc = write_to_disk( window, name );
if (rc != ERROR) {
file->modified = FALSE;
file->new_file = FALSE;
}
}
if (rc == OK) {
temp_ll = window->file_info->line_list;
for (; temp_ll->len != EOF; temp_ll=temp_ll->next)
temp_ll->dirty = FALSE;
window->file_info->dirty = GLOBAL;
}
return( rc );
}
int save_backup( WINDOW *window )
{
return( write_to_disk( window, window->file_info->backup_fname ) );
}
int write_to_disk( WINDOW *window, char *fname )
{
char name[MAX_COLS];
char status_line[MAX_COLS+2];
char line_buff[(MAX_COLS+1)*2];
register file_infos *file;
int rc;
int prompt_line;
int fattr;
file = window->file_info;
prompt_line = window->bottom_line;
assert( strlen( fname ) < MAX_COLS );
strcpy( name, fname );
save_screen_line( 0, prompt_line, line_buff );
eol_clear( 0, prompt_line, g_display.message_color );
combine_strings( status_line, utils6, name, "'" );
s_output( status_line, prompt_line, 0, g_display.message_color );
if ((rc = hw_save( name, file, 1L, file->length, NOTMARKED )) == ERROR) {
if (ceh.flag != ERROR) {
if (get_fattr( name, &fattr ) == OK && fattr & READ_ONLY)
combine_strings( status_line, utils7a, name, utils7b );
else
combine_strings( status_line, utils8, name, "'" );
error( WARNING, prompt_line, status_line );
}
}
restore_screen_line( 0, prompt_line, line_buff );
return( rc );
}
int save_as_file( WINDOW *window )
{
char name[MAX_COLS];
char line_buff[(MAX_COLS+1)*2];
int prompt_line;
int rc;
int fattr;
register WINDOW *win;
line_list_ptr temp_ll;
win = window;
entab_linebuff( );
if (un_copy_line( win->ll, win, TRUE ) == ERROR)
return( ERROR );
prompt_line = win->bottom_line;
save_screen_line( 0, prompt_line, line_buff );
name[0] = '\0';
if ((rc = get_name( utils9, prompt_line, name,
g_display.message_color )) == OK && *name != '\0') {
rc = get_fattr( name, &fattr );
if (rc == OK) {
set_prompt( utils10, prompt_line );
if (get_yn( ) != A_YES || change_mode( name, prompt_line ) == ERROR)
rc = ERROR;
}
if (rc != ERROR)
rc = write_to_disk( win, name );
}
restore_screen_line( 0, prompt_line, line_buff );
return( rc );
}
int change_fattr( WINDOW *window )
{
char name[MAX_COLS];
char line_buff[(MAX_COLS+1)*2];
file_infos *file;
WINDOW *wp;
int prompt_line;
register int ok;
unsigned char fattr;
char *s;
int rc;
prompt_line = window->bottom_line;
save_screen_line( 0, prompt_line, line_buff );
name[0] = '\0';
if ((ok = get_name( utils14, prompt_line, name,
g_display.message_color )) == OK) {
if (*name != '\0') {
fattr = 0;
s = name;
while (ok = *s++) {
switch (ok) {
case 'a' :
case 'A' :
fattr |= ARCHIVE;
break;
case 's' :
case 'S' :
fattr |= SYSTEM;
break;
case 'h' :
case 'H' :
fattr |= HIDDEN;
break;
case 'r' :
case 'R' :
fattr |= READ_ONLY;
break;
default :
break;
}
}
file = window->file_info;
if (set_fattr( file->file_name, fattr ))
error( WARNING, prompt_line, utils15 );
else {
file->file_attrib = fattr;
for (wp=g_status.window_list; wp!=NULL; wp=wp->next) {
if (wp->file_info == file && wp->visible)
show_window_fname( wp );
}
}
}
rc = OK;
} else
rc = ERROR;
restore_screen_line( 0, prompt_line, line_buff );
return( rc );
}
int get_fattr( char far *fname, int *fattr )
{
int rc;
int attr;
ASSEMBLE {
push ds
mov dx, WORD PTR fname
mov ax, WORD PTR fname+2
mov ds, ax
mov ax, 0x4300
int 0x21
pop ds
jc an_error
xor ax, ax
jmp SHORT get_out
}
an_error:
ASSEMBLE {
xor cx, cx
}
get_out:
ASSEMBLE {
mov WORD PTR rc, ax
mov WORD PTR attr, cx
}
*fattr = attr;
if (ceh.flag == ERROR)
rc = ERROR;
return( rc );
}
int set_fattr( char far *fname, int fattr )
{
int rc;
ASSEMBLE {
push ds
mov dx, WORD PTR fname
mov ax, WORD PTR fname+2
mov ds, ax
mov cx, WORD PTR fattr
mov ax, 0x4301
int 0x21
pop ds
jc get_out
xor ax, ax
}
get_out:
ASSEMBLE {
mov WORD PTR rc, ax
}
if (ceh.flag == ERROR)
rc = ERROR;
return( rc );
}
int get_current_directory( char far *path, int drive )
{
int rc;
ASSEMBLE {
push si
push ds
mov dx, WORD PTR drive
mov si, WORD PTR path
mov ax, WORD PTR path+2
mov ds, ax
mov ah, 0x47
int 0x21
xor ax, ax
jnc no_error
mov ax, ERROR
}
no_error:
ASSEMBLE {
pop ds
pop si
mov WORD PTR rc, ax
}
if (ceh.flag == ERROR)
rc = ERROR;
return( rc );
}
int set_current_directory( char far *new_path )
{
int rc;
ASSEMBLE {
push ds
mov dx, WORD PTR new_path
mov ax, WORD PTR new_path+2
mov ds, ax
mov ah, 0x3b
int 0x21
xor ax, ax
jnc no_error
mov ax, ERROR
}
no_error:
ASSEMBLE {
pop ds
mov WORD PTR rc, ax
}
if (ceh.flag == ERROR)
rc = ERROR;
return( rc );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -